-1

Goal:
When you start the app, The Button should be visible after 5 seconds.

In order words, Start the app -> Hide a button -> Wait 5 seconds -> Display the button

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

Info:
*Im new in android, This what i try

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

Any help will be appreciated thank you

xml androidmanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jfdimarzio.labb3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jfdimarzio.labb3.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:visibility="visible"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />

</android.support.constraint.ConstraintLayout>
Swati Garg
  • 995
  • 1
  • 10
  • 21
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145

3 Answers3

2

Seems like you missing setContentView.. fix that first..

setContentView(R.layout.main_activity);

Don't use Thread sleep. use handler post delayed instead..

Handler handler = new Handler();
button2.setVisibility(View.GONE);
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        button2.setVisibility(View.VISIBLE);
    }
}
// run runnable after 5 seconds
handler.postDelayed(runnable, 5000);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • seems like, you just read it from my answer dude, at least don't pretend like u didn't – Pavneet_Singh Apr 01 '18 at 07:02
  • @What'sUP, use the layout of main activity of yours, not certainly main_activity. It is where you you wrote xml code for the button – Harpreet Singh Apr 01 '18 at 07:42
  • I retrieve a error message "E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jfdimarzio.labb3, PID: 3065 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jfdimarzio.labb3/com.jfdimarzio.labb3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setVisibility(int)' on a null object reference" – HelloWorld1 Apr 01 '18 at 07:50
  • @What'sUP check my answer below. – Raj Suvariya Apr 01 '18 at 08:14
  • Thank you guys for your help! – HelloWorld1 Apr 01 '18 at 08:22
0

So simple you animate without action animation. Just set duration, and after ending animation, the listener will display it

buttonvVew.setVisibility(View.GONE);

buttonView.animate()
      .setDuration(5000)
      .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
          super.onAnimationEnd(animation);
          buttonView.setVisibility(View.visible);
        }
});
  • E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jfdimarzio.labb3, PID: 3064 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jfdimarzio.labb3/com.jfdimarzio.labb3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setVisibility(int)' on a null object reference – HelloWorld1 Apr 01 '18 at 07:27
  • View buttonView = findViewById(R.id.button); It should be View not button, and View.visible -> View.VISIBLE – Daniyar Kayirbolatov Apr 01 '18 at 07:46
  • I get another error message " --------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jfdimarzio.labb3, PID: 2627 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jfdimarzio.labb3/com.jfdimarzio.labb3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setVisibility(int)' on a null object reference" – HelloWorld1 Apr 01 '18 at 08:02
  • @What'sUP check my answer below. – Raj Suvariya Apr 01 '18 at 08:14
  • Thank you guys for your help! – HelloWorld1 Apr 01 '18 at 08:22
0

Try this

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main.xml);
        Button button2 = (Button) findViewById(R.id.btn_test);
        button2.setVisibility(GONE);
        button2.postDelayed(new Runnable() {
            public void run() {
                button2.setVisibility(View.VISIBLE);
            }
        }, 5000);
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Raj Suvariya
  • 1,592
  • 3
  • 14
  • 36