I have a layout displayed on a button click.I want to hide that layout after 10 seconds.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mVolHandler = new Handler();
mVolRunnable = new Runnable() {
public void run() {
mVolLayout.setVisibility(View.GONE);
}
};
}
private OnTouchListener mVolPlusOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.postDelayed(mVolRunnable, 10000);
}
}
Make use of
Handler&Runnable.You can delay a Runnable using
postDelayedof Handler.Now inside onButtonClick event you have to tell Handler to run a runnable after X milli seconds:
If you want to cancel this then you have to use
mHandler.removeCallbacks(mRunnable);Update (According to edited question)
You just need to remove callbacks from
HandlerusingremoveCallbacks()So just update your code inside
onTouchmethod like this :