Search code examples
javaandroidtextviewscrollviewvisibility

How to set a textView to visible after an onClick event and Activity change?


I have three passages in my scrollview that need to each become visible after an onclick event on one of three buttons.

I have currently set them to all invisible. And since I cannot get it to work, I am only trying it out with one of the passages.

Because of this I created a private textview constant for only the first passage. But after I pass the intent to switch the activity, I also try to turn the view on that package to visible.

I have included my MainActivity.java and the xml file I used to set invisible.

package com.example.threebuttons;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView passage1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        passage1 = findViewById(R.id.passage_1);
    }

    public void launchPassageOne(View view) {
        passage1.setVisibility(view.VISIBLE);
        Intent intent = new Intent(this, PassageActivity.class);
        startActivity(intent) ;
    }

    public void launchPassageTwo(View view) {
        Intent intent = new Intent(this, PassageActivity.class);
        startActivity(intent) ;
    }

    public void launchPassageThree(View view) {
        Intent intent = new Intent(this, PassageActivity.class);
        startActivity(intent) ;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".PassageActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/passage_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage1"
                android:visibility="invisible"/>

            <EditText
                android:id="@+id/passage_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage2"
                android:visibility="invisible"/>

            <EditText
                android:id="@+id/passage_3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:text="@string/passage3"
                android:visibility="invisible"/>

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

My program just crashes. And I cannot find any error messages.

How can I make the packages visible whenever I want the activity to change? There are three passages that I want to each become visible for the respective button, then turn invisible if the back button is pressed.


Solution

  • It seams the three views are in the started activity. And so you can't change their visibility because they haven't been created.

    Add this before you start the activity intent.putExtra("passageNum", 1) Then call startActivity(intent)

    In PassageAactivity onCreate do the following :

    If (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1)
        passage1.setVisibility(View.VISIBLE)
    

    And so on for the other views