user987362
user987362

Reputation: 377

Hide A Layout After 10 Seconds In 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);
    }
}

Upvotes: 14

Views: 23235

Answers (5)

Andro Selva
Andro Selva

Reputation: 54322

You can use postDelayed:

val delay = 3000L // 3 seconds
view.postDelayed({ view.visibility = View.GONE }, delay)

Upvotes: 1

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

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);

Upvotes: 41

Raghav Chopra
Raghav Chopra

Reputation: 527

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    pd=ProgressDialog.show(this,"","Loading, Please wait .. ",true);

    setTitle("set title");
    final Handler uiThreadCallback=new Handler();
    final Runnable runInUIThread= new Runnable(){
        public void run(){
            fnDraw();
            pd.dismiss();

        }
    }; 
    new Thread(){
        @Override public void run(){
            uiThreadCallback.post(runInUIThread);
        }
    }.start();
}
public void fnDraw(){
    setContentView(R.layout.define ur xml if any);
    i1=(ImageView)findViewById(R.id.i1);
    t1=(TextView)findViewById(R.id.t1);
    t2=(TextView)findViewById(R.id.t2);

    timerRegister=new Timer();
    lIteration=0;
    checkTime=new TimerTask(){
        public void run(){
            if(lIteration==1){
                timerRegister.cancel();
                uiDrawThreadCallback.post(runInUIDrawThread);
            }
            lIteration++;
            return;
        }
    };
    timerRegister.scheduleAtFixedRate(checkTime,0,10000);
}
final Handler uiDrawThreadCallback=new Handler();
final Runnable runInUIDrawThread= new Runnable(){
    @Override
    public void run(){
        fnDrawAgain();
    }
};
public void fnDrawAgain(){
    Intent intent=new Intent(this,new class you want to open.class);
    startActivity(intent);
}

}

try it m sure it gonna work in ur on create screen

Upvotes: 0

Kiran Babu
Kiran Babu

Reputation: 1893

Use Handler to hide layout after 10 seconds. Use Post delayed method

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69218

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.

Upvotes: 2

Related Questions