0

Goal:
When you start the android app, the button should not be displayed after 5 seconds.

Problem:
The code doesn't work and what part am I missing?

Info:
*Im new in android
*The code is inspired from this page Android - Hide button during an onClick action

Thank you!

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button button2 = (Button) findViewById(R.id.btn_test);
        button2.setVisibility(GONE);

        new Thread(new Runnable() {
            @Override
            public void run() {

                try
                {
                    //dummy delay for 5 second
                    Thread.sleep(5000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }


                runOnUiThread(new Runnable() { //resetting the visibility of the button
                    @Override
                    public void run() {

                        //manipulating UI components from outside of the UI Thread require a call to runOnUiThread
                        button2.setVisibility(VISIBLE);
                    }
                });
            }
        }).start();


    }

}
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145

2 Answers2

0

This can be achieved in a simpler way. If your needed sequence is: Start the app -> Display a button -> Wait 5 seconds -> Hide the button

final Button button2 = (Button) findViewById(R.id.btn_test);

    button2.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!isDestroyed() && !isFinishing()) {
                button2.setVisibility(View.GONE);
            }
        }
    },5000);

Otherwise, if you should display the button after 5 seconds after app launch, then just set button's visibility to GONE in your layout and change button2.setVisibility(View.GONE) to button2.setVisibility(View.VISIBLE) inside post delayed action

Yurii Kyrylchuk
  • 809
  • 7
  • 16
  • @What'sUP I'm pretty sure it works. I've double checked it right now in my sample app. Can you please post your complete activity & layout code, please? – Yurii Kyrylchuk Mar 26 '18 at 21:41
0

You need to set a listener to start your command, onCreate is the creation.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Button button2 = (Button) findViewById(R.id.btn_test);


    button2.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
       button2.setVisibility(GONE);
    new Thread(new Runnable() {
        @Override
        public void run() {

            try
            {
                //dummy delay for 5 second
                Thread.sleep(5000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }


            runOnUiThread(new Runnable() { //resetting the visibility of the button
                @Override
                public void run() {

                    //manipulating UI components from outside of the UI Thread require a call to runOnUiThread
                    button2.setVisibility(VISIBLE);
                }
            });
        }
    }).start();
    }

this code will hide the button AFTER onClick, start the thread, and after 5 seconds it will appear again.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167