22

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

https://i.stack.imgur.com/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.stack.imgur.com/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>
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Moko
  • 441
  • 4
  • 9
  • 15

4 Answers4

79

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)
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

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
Amirouche Zeggagh
  • 3,428
  • 1
  • 25
  • 22
1

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.

Community
  • 1
  • 1
Androiderson
  • 16,865
  • 6
  • 62
  • 72
0

for hiding your widget

   yourEditText.setVisibility(View.GONE)

for visbility of your widget

   yourEditText.setVisibility(View.VISIBLE)