Jacob
Jacob

Reputation: 1225

android layout hide/show views

I have a linear vertical layout as below. I need in my application to switch the Button and the TextView. To hide button and show text then change etc. If I use setVisibility(View.INVISIBLE) for the button it disappears from the screen but it still hold the place. How can I switch those elements without remove them completely?

        <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:textSize="30sp"
        android:text="Chronometer" />

    <Button
        android:id="@+id/stopButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stop_button" />


    <TextView
        android:id="@+id/wrongCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:textSize="30sp"
        android:text="" />

Upvotes: 8

Views: 17934

Answers (3)

M D
M D

Reputation: 47807

You should used setVisibility(View.GONE) instead of setVisibility(View.INVISIBLE).

For more information go to: http://developer.android.com/reference/android/view/View.html

Upvotes: 3

Hamid Shatu
Hamid Shatu

Reputation: 9700

Suppose your created button is as follows...

Button button = (Button) findViewById(R.id.stopButton);

When you want to hide that Button write this...

button.setVisibility(View.GONE);

And when you want to show that button again then write...

button.setVisibility(View.VISIBLE);

Upvotes: 10

yuva ツ
yuva ツ

Reputation: 3703

use-

button.setVisibility(View.GONE);

Upvotes: 25

Related Questions