Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="" />
like image 576
Jacob Avatar asked Feb 18 '14 05:02

Jacob


2 Answers

use-

button.setVisibility(View.GONE);
like image 156
yuva ツ Avatar answered Jan 04 '23 12:01

yuva ツ


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);
like image 36
Hamid Shatu Avatar answered Jan 04 '23 11:01

Hamid Shatu