Moko
Moko

Reputation: 451

Invisible components still take up space

So, I have this one activity that looks like this:

https://i.sstatic.net/I70b2.jpg

As you can see, it has a button, a list and a button.

If certain conditions are met, I want to hide both buttons just show the list, but as you can see in the next image, the buttons are still taking up space:

https://i.sstatic.net/6dSha.jpg

So my question, what can I do to enlarge the list to take that space out?

Here is my layout for the buttons and the list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="registrarAsistencia"
        android:text="Registrar Asistencia" />

    <ListView
        android:id="@+id/listaAlumnos"
        android:layout_width="fill_parent"
        android:layout_height="376dp"
        android:layout_weight="2.29" />

    <Button
        android:id="@+id/btnChecarBoxes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="seleccionarTodosNinguno"
        android:text="Seleccionar / Deseleccionar todo" />
</LinearLayout>

And here it is the layout for the list's contents:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:orientation="vertical" >

      <TextView
          android:id="@+id/rowTextView"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:padding="10dp"
          android:textSize="16sp" 
          android:textStyle="bold"/>

      <TextView
          android:id="@+id/rowTextView2"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:padding="10dp"
          android:textSize="16sp" 
          android:textStyle="italic"/>

  </LinearLayout>

  <CheckBox
      android:id="@+id/CheckBox01"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:focusable="false"
      android:padding="10dp" />
</RelativeLayout>

Upvotes: 23

Views: 17234

Answers (4)

Amirouche
Amirouche

Reputation: 3756

In the java code

yourView.setVisibility(View.GONE);

and in the XML code

android:visibility="gone"

Thier are 3 state

  1. visible: normal
  2. invisible : takes space and invisible
  3. gone : no space and no visibility

Upvotes: 3

Gokul Sreenivasan
Gokul Sreenivasan

Reputation: 479

for hiding your widget

   yourEditText.setVisibility(View.GONE)

for visbility of your widget

   yourEditText.setVisibility(View.VISIBLE)

Upvotes: 0

Androiderson
Androiderson

Reputation: 17083

Use

android:layout_height="0dp"
android:layout_weight="1"

In addition to @CommonsWare's answer

You should use a layout that fits well in all screen sizes. If you give you layout a fixed height like android:layout_height="376dp" it will look good only on the device (or emulator) you're testing on. What you have to do is to make sure that your listview takes up all (and only) the space left by your buttons.

Check out this article and this answer to better understand layout weights.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006614

Do not make the buttons View.INVISIBLE. Make the buttons View.GONE. There are three visibility states:

  • visible (normal)
  • invisible (pixels not drawn, but still takes up space)
  • gone (not included in rendering)

Upvotes: 80

Related Questions