1

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.

iknow
  • 8,358
  • 12
  • 41
  • 68
joe
  • 93
  • 2
  • 12
  • which activity you want random visibility of text ? inside Second activity or MainActivity? – chand mohd Jul 22 '20 at 19:21
  • Please try to give proper example. What is the criteria to change the visibility of the text in the SecondActivtiy? – Vishist Varugeese Jul 22 '20 at 19:54
  • @chandmohd I want to change the visibility inside Second activity between the two textview. – joe Jul 23 '20 at 06:49
  • @VishistVarugeese the flow will be as follows Main Activity layout --> SecondActivity layout : textView "A" visible --> Main Activity layout --> SecondActivity layout: textView: "B" visible --> Main Activity layout -->... The criteria to change visibility is only the time that passes and how many times the main activity was called. – joe Jul 23 '20 at 08:37

1 Answers1

1

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);
}
iknow
  • 8,358
  • 12
  • 41
  • 68