简体   繁体   中英

How to hide or show element on Button click on Next Activity

I was curious. Consider I have One SecondActivity with ProgressBar which layout file is

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

and In MainActivity I have Two Buttons

<Button
    android:id="@+id/Button 1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/Button 2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Both Button when clicked will open SecondActivity .

Now I would Like to show progress bar when button A is clicked but when Button B is clicked, Make the progress Bar invisible in Second Activity.

Below is diagrams illustrating above method that I would like to get.

Screenshot:

You just have to pass some extras along with the intent while launching your second activity.

When button A is clicked, start the second activity like this.

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("SHOW_PROGRESS", true);
startActivity(intent);

And when the button B is clicked, launch the second activity without passing any extra in your intent like the following.

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

Now from your SecondActivity receive the intent and show the ProgressBar based on the value found from your extras.

boolean showProgressBar = getIntent().getBooleanExtra("SHOW_PROGRESS", false);

if(showProgressBar) progressBar.show();

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM