JackD.
JackD.

Reputation: 33

boolean expression not working

I have this code below:

boolean isInvisible = false;

public void onLoveButtonClicked(View view) {
    TextView textView = (TextView) findViewById(R.id.TextView);
    if (isInvisible){
        textView.setVisibility(View.VISIBLE);
    } else {
        textView.setVisibility(View.INVISIBLE);
    }
}

and

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:id="@+id/TextView"
    android:layout_below="@+id/Button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:visibility="invisible"/>

When I run the app and I press the button, the text shows up, but when I press again, it does nothing.

EDIT: boolean isInvisible = false;

Upvotes: 1

Views: 97

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Use textView.getVisibility() to toggle Visibility of TextView as:

if (textView.getVisibility() != View.VISIBLE){
        textView.setVisibility(View.VISIBLE);            
    } else {
        textView.setVisibility(View.INVISIBLE);            
    }

Upvotes: 3

Jan
Jan

Reputation: 13858

Maybe toggle your variable isInvisible as well?

 boolean isInvisible;

 public void onLoveButtonClicked(View view) {
    TextView textView = (TextView) findViewById(R.id.TextView);
    if (isInvisible){
        textView.setVisibility(View.VISIBLE);            
    } else {
        textView.setVisibility(View.INVISIBLE);            
    }
    isInvisible = !isInvisible;
}

Upvotes: 0

Related Questions