tags:

views:

32

answers:

2

I would like the main layout file (main.xml) respond to the main activity. I would like a TextView inside the layout file only to show if a variable, located in the main activity, equals a certain number, like 0. If the variable is 1 or 2, I want it to show something else.

What's the easiest way to perform this? I'm not really sure if this is even possible!

+1  A: 

Not in the declarative way. You can only achieve it in the code (main activity).

This is quite simple, for example:

TextView textViewToResponse = findViewById(R.id.textviewtoresponse);

public void setVaraible(int value) {
    variable = value;
    if (variable==0){
        textViewToResponse.setVisibility(GONE);
    }
    else{
        // do something else
    }
}

I assumed the variable you have is called Variable above.

xandy
Thanks! I see that you can simply use "setVisibility" to perform it. Very simple. Thanks again.
Jack Love
How would I make it so the height becomes 0px if the visibility is "gone"?
Jack Love
Three modes in visibility:http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29
xandy
Oh, nevermind. I had set it to "INVISIBLE", not "GONE".
Jack Love
+1  A: 
if (vaiable==0){
   textViewToResponse.setVisibility(true);
}else{
   textViewToResponse.setVisibility(false);
    }
}
Phobos
This works too!
Jack Love