D-D D-D - 1 month ago 17
Android Question

LinearLayout Weights not working inside Listview

I have searched and found that many people have reported this issue.
These are some of the links that I have gone throught :



and some others as well

These links have suggested the following solutions


  1. Replacing mInflater.inflate(resID, parent, false); with mInflater.inflate(resID, null, false);

  2. Making list view as
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    I even tried experimenting with fill_parent but it didnt work.

  3. Listview inside another linear layout

  4. Row xml inside a Relative layout and withing that a Linear layout

  5. For Horizontal orientation make android:layout_width="0dp". When I do this, the listview is not poplutated. It remains blank



I am not able find anything for this particular problem in android documentation either.

My list view:

android:id="@+id/lst_stocks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/row_heading"
android:layout_marginTop="@dimen/margin_5"


My other xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="horizontal"
android:weightSum="3">


<TextView
android:id="@+id/txt_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="@dimen/margin_10"
android:text="Rating"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark" />

<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxEms="10"
android:padding="@dimen/margin_10"
android:singleLine="true"
android:text="Companay"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
/>

<TextView
android:id="@+id/txt_sector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxEms="10"
android:padding="@dimen/margin_10"
android:singleLine="true"
android:text="Sector"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>


I am using this xml file in other places as well, where it works perfectly fine.


  • So what am I missing. Why are the Linear layout weights not working?

  • Guidelines for using LinearLayouts inside Listviews effectively



P.S. I have run the files on two separate mobiles and not on a emulator.

Answer

Make TextView layout_width as 0dp.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:orientation="horizontal"
    android:weightSum="3">


    <TextView
    android:id="@+id/txt_rating"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="@dimen/margin_10"
    android:text="Rating"
    android:textAlignment="center"
    android:textColor="@color/colorPrimaryDark" />

    <TextView
    android:id="@+id/txt_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ellipsize="end"
    android:maxEms="10"
     android:padding="@dimen/margin_10"
    android:singleLine="true"
    android:text="Companay"
    android:textAlignment="center"
     android:textColor="@color/colorPrimaryDark"
    />

  <TextView
  android:id="@+id/txt_sector"
   android:layout_width="0dp"
  android:layout_height="wrap_content"
   android:layout_weight="1"
  android:ellipsize="end"
  android:maxEms="10"
  android:padding="@dimen/margin_10"
  android:singleLine="true"
  android:text="Sector"
  android:textAlignment="center"
  android:textColor="@color/colorPrimaryDark" />
  </LinearLayout>
Comments