0

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.

2
  • 1
    You need your width set to 0 for those items using weight not on your ListView
    – codeMagic
    Nov 1, 2016 at 14:51
  • Couple other quick notes. You don't need orientation in ` horizontal layout as it is the default. Also, it's better to not set the weightSum unless you have a reason to and let the device do it for you.
    – codeMagic
    Nov 1, 2016 at 14:57

2 Answers 2

0

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>
0

If you want to use weight to set your height of TextView, then you should do this :

<TextView
    android:id="@+id/txt_sector"
    android:layout_width="wrap_content"
    android:layout_height="0dp"   // Change is done here
    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" />`

Similarly if you are looking to set width, you can use width="0dp" and wieght="1".

Not the answer you're looking for? Browse other questions tagged or ask your own question.