Stack Overflow Asked on November 29, 2021
I have an app that switches between two different activities (Main Activity with a simple text and Second Activity with two TextViews) in Android with different layouts every 10 seconds.
I want in Second Activity at the first time display only the first textView A, then return to the MainActivity and again lead to the Second Activity but display only the second textView B.
activity_second.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="255dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_purple"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="A"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="65sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="B"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="65sp" />
</LinearLayout>
</LinearLayout>
And java code that switches between two Activities provided bellow.
Second_activity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 10000);
........
}
My problem is how to change the visibility of the two textViews for every time that switches between two activities.
To do it You can just pass a variable (if there are only 2 TextViews it can be boolean) to second activity. If it is true
show 1st TextView if it is false
show 2nd TextView.
To pass value with intent use this:
Boolean value = true; //set this if You want to show 1st or 2nd textbox
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("key", value);
startActivity(i);
To retrieve the value:
Bundle extras = getIntent().getExtras();
if (extras != null) {
Boolean value = extras.getBoolean("key");
//The key argument here must match that used in the other activity
}
When You have that value
just set visibility of TextViews:
if (value)
{
textView1.setVisibility(TextView.VISIBLE);
textView2.setVisibility(TextView.GONE);
}
else
{
textView1.setVisibility(TextView.GONE);
textView2.setVisibility(TextView.VISIBLE);
}
Answered by iknow on November 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP