Lisek
Lisek

Reputation: 783

ListView/Button not visible in LinearLayout

Whatever I do, it is just not working. Here is a layout code which I am trying to implement into another layout which I will explain later. Here is the snippet of the fine Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:padding="8dip" >  
<ListView android:id="@+id/in"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_gravity="bottom"
    android:layout_weight="1" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <EditText android:id="@+id/edit_text_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="bottom" />
    <Button android:id="@+id/action_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/action_send" />
</LinearLayout>  
</LinearLayout>

I want to use this layout to put it into another LinearLayout so I can split the screen on the given Fragment. Here is what I was trying to achieve:

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

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
    android:background="#192832">
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.2"
    android:background="#193222"
    android:gravity="bottom" >

<ListView android:id="@+id/in"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_gravity="bottom" /> 
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <EditText android:id="@+id/edit_text_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="bottom" />
    <Button android:id="@+id/action_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/action_send" />
</LinearLayout>   
</LinearLayout>
</LinearLayout>

As you can see, it is actually splitted but the ListView is covering or even making the EditText and Button to disappear. What changes should I make to get it all done? I was trying experiment with android:layout_weight but with no succes. Upon adding android:layout_weight="1" to the ListView section, I just loose it. I do not want to use RelativeLayout but if there is no solution here, I will go with that.

Upvotes: 0

Views: 1299

Answers (2)

Mike Ortiz
Mike Ortiz

Reputation: 4041

You will need a RelativeLayout to align the EditText and Button with the bottom. I changed your second inner LinearLayout into a RelativeLayout. Then, I aligned the EditText and Button with the bottom of the parent view. And your ListView is aligned above your EditText and Button. Here are my changes:

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

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="0dp" 
    android:layout_weight="0.8"
    android:background="#192832" />

<RelativeLayout android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="0dp"
    android:layout_weight="1.2" 
    android:background="#193222"
    android:gravity="bottom">

    <ListView android:layout_above="@+id/edit_text_and_button"
        android:id="@+id/in" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" 
        android:layout_gravity="bottom" />

    <LinearLayout android:id="@id/edit_text_and_button"
        android:layout_alignParentBottom="true" 
        android:orientation="horizontal"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">

        <EditText android:id="@+id/edit_text_out"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:layout_gravity="bottom" />

        <Button android:id="@+id/action_send" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/action_send" />

    </LinearLayout>

</RelativeLayout>

</LinearLayout>

Upvotes: 1

Nathan Walters
Nathan Walters

Reputation: 4126

Your problem is that the ListView's height is set to match_parent, this pushes the LinearLayout below it offscreen. To fix this, you can change the weight of the LinearLayout on the bottom to 0 and the weight of the ListView on top to one, that way it will first calculate the size of the bar on the bottom, and then the ListView will expand to fill the remaining space. This code seems to achieve what you want:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.8"
    android:background="#fff" >
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1.2"
    android:background="#fff"
    android:gravity="bottom"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/in"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0" >

        <EditText
            android:id="@+id/edit_text_out"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1" />

        <Button
            android:id="@+id/action_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/action_send"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Related Questions