Hide / show views

I have a linear vertical layout as shown below. I need to switch Button and TextView in my application. To hide the button and show the text, change it, etc. If I use setVisibility (View.INVISIBLE) for the button, it disappears from the screen, but it still holds the place. How can I switch these items without deleting 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="" />
+4
source share
3 answers

use -

button.setVisibility(View.GONE);
+16
source

Suppose your created button looks like this:

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

If you want to hide this button, write this ...

button.setVisibility(View.GONE);

And if you want to display this button again, write ...

button.setVisibility(View.VISIBLE);
+8
source

You should use setVisibility(View.GONE)instead setVisibility(View.INVISIBLE).

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

+3
source

Source: https://habr.com/ru/post/1527426/


All Articles