Hide A Layout After 10 Seconds In Android?

前端 未结 6 393
旧巷少年郎
旧巷少年郎 2021-02-04 07:30

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(         


        
相关标签:
6条回答
  • 2021-02-04 08:11

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

    0 讨论(0)
  • 2021-02-04 08:14

    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);
    
    0 讨论(0)
  • 2021-02-04 08:24
        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

    0 讨论(0)
  • 2021-02-04 08:28

    The following piece of code hides your layout in 3 secs. You can change the time by changing the constant I have provided here for the delay.

             private void HideLayout() {
    
         final View view=volume_control_layout;
         view.postDelayed(new Runnable() {
    
            public void run() {
                if(!volume_controller.isPressed())
                {
                view.setVisibility(View.INVISIBLE);
                }
                else
                {
                    HideLayout();
                }
            }
        }, 3000); // (3000 == 3secs)
    
        }
    
    0 讨论(0)
  • 2021-02-04 08:30

    none of those above are good solutions. Imagine that the view is not visible after 10s (eg: it was scrolled down) so the Visibility.GONE will not work on it. I am still looking for a solutions

    0 讨论(0)
  • 2021-02-04 08:36

    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.

    0 讨论(0)
提交回复
热议问题