Someone Somewhere
Someone Somewhere

Reputation: 23787

Need to prevent Listview Row from always being 100% width of Listview

Goal

I want the Listview, in an activity, to fill the width of the screen (so that the listview scroller appears against the edge of the screen).

I then want the listview Row to not fill 100% of the width. For example, I want it just 400dp wide.

Problem

The Listview Row is ALWAYS taking up 100% of the listview's width - even though the width is supposed to be 400dp.

XML of the Main Listview

<?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="fill_parent"
    android:background="@drawable/background_image">

    <LinearLayout
            android:id="@+id/Envelope"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/prompt"
            android:layout_marginTop="10dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:gravity="center_horizontal">

        <ListView android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:layout_marginBottom="15dip"
            android:divider="#00FFFFFF"
            android:dividerHeight="7dip"
            android:cacheColorHint="#00000000"
            android:drawSelectorOnTop="false"
            android:background="#00FFFFFF"
            android:scrollbarStyle="outsideOverlay" />

    </LinearLayout>

</RelativeLayout>

XML of the Listview's Row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/row"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical">

            <TextView android:id="@+id/label1"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:textSize="16sp"
                      android:layout_gravity="left|center_vertical"
                      android:paddingLeft="20dip"             
                      android:paddingRight="10dip" />

            <TextView android:id="@+id/label2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="0"
                      android:textSize="16sp"
                      android:layout_gravity="right|center_vertical"
                      android:paddingLeft="10dip"             
                      android:paddingRight="10dip"/>

            <CheckBox android:id="@+id/checkbox" 
                      android:layout_width="34dip" 
                      android:layout_height="34dip"
                      android:layout_gravity="right|center_vertical"
                      android:duplicateParentState="true"
                      android:button="@drawable/checkbox_states"/>
        </LinearLayout>

</LinearLayout>

The result with that XML is that the "row" linearlayout fills 100% of the width of the listview even though I would expect only 400dp to be used. I've spent 4 hours on trying to get it right /frustrated

Upvotes: 2

Views: 657

Answers (2)

Manishika
Manishika

Reputation: 5564

Instead of setting android:layout_width="400dp"

Try using weightSum and weight in the following way:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:background="@drawable/bg"
        android:orientation="horizontal"
         >

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

            <TextView android:id="@+id/textEmp_id"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textSize="16sp"
                          android:layout_gravity="left|center_vertical"
                          android:paddingLeft="20dip"             
                          android:paddingRight="10dip" />

                <TextView android:id="@+id/label2"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textSize="16sp"
                          android:layout_gravity="right|center_vertical"
                          android:paddingLeft="10dip"             
                          android:paddingRight="10dip"/>

                <CheckBox android:id="@+id/checkbox" 
                          android:layout_width="34dip" 
                          android:layout_height="34dip"
                          android:layout_gravity="right|center_vertical"
                          android:duplicateParentState="true"
                          android:button="@drawable/checkbox_states"/>


        </LinearLayout>
         <View
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                />

    </LinearLayout>

Upvotes: 2

Mikael Olsson
Mikael Olsson

Reputation: 415

I recommend you to set the ListView Row to match it's parent. You can't always assume the available width is 400dp. Also set a left and right margin to get some space.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="match_parent"
    android:layout_marginLeft="10dp"      
    android:layout_marginRight="10dp"

Upvotes: 1

Related Questions