Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide A Layout After 10 Seconds In Android?

Tags:

android

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);
    }
}
like image 881
user987362 Avatar asked Nov 18 '11 04:11

user987362


3 Answers

Make use of Handler & Runnable.

You can delay a Runnable using postDelayed of Handler.

Runnable mRunnable;
Handler mHandler=new Handler();

mRunnable=new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                yourLayoutObject.setVisibility(View.INVISIBLE); //If you want just hide the View. But it will retain space occupied by the View.
                yourLayoutObject.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View    
            }
        };

Now inside onButtonClick event you have to tell Handler to run a runnable after X milli seconds:

mHandler.postDelayed(mRunnable,10*1000);

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 Handler using removeCallbacks()

So just update your code inside onTouch method like this :

mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.removeCallbacks(mVolRunnable);
mVolHandler.postDelayed(mVolRunnable, 10000);
like image 188
Kartik Domadiya Avatar answered Nov 06 '22 14:11

Kartik Domadiya


You can use an Animation started when you click the button, with 10 seconds duration that fades out the layout and probably sets its visibility to GONE at the end.

like image 37
Diego Torres Milano Avatar answered Nov 06 '22 13:11

Diego Torres Milano


You can use postDelayed:

val delay = 3000L // 3 seconds
view.postDelayed({ view.visibility = View.GONE }, delay)
like image 1
Andro Selva Avatar answered Nov 06 '22 13:11

Andro Selva