Search code examples
androidbackgroundandroid-listfragment

Android ListFragment: background under each item


I'd like to create a ListFragment in Android, and a background for each element, like this: Example of my ListFragment

I'll want to set the width of theese background's from the code of my ListAdapter. I thought to create the list element's layout something like this:

<RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 xmlns:android="http://schemas.android.com/apk/res/android">

 <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="40dp"
    android:id="@+id/surveyListRow_upperLayout"
    android:layout_marginTop="7dp"
    android:layout_marginBottom="7dp">

    <RelativeLayout
        android:layout_marginLeft="23dp"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/surveyListRow_ringLayout"
        android:background="#FFFFFF"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/textColor"
        android:gravity="center"
        android:id="@+id/surveyListRow_surveyName"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="55dp"
        android:layout_marginRight="105dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:gravity="center"
        android:id="@+id/surveyListRow_surveyPrize"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

 </RelativeLayout>
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="57dp"
    android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:layout_weight="30"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="70"/>
 </LinearLayout>
</RelativeLayout>

And I'll set the weight property of the LinearLayout's children, to set the background width.

My problem is, when the text in the width is too long (as you can see in the third element). Then the background isn't scales to the bottom of the element.

Can you help me, why? I've tried almost everything I can imagine.

Thanks!


Solution

  • Try to add this to your TextView

     <TextView
        ...
        android:ellipsize="end"
        android:ems="2"
        android:maxLines="2" />
    

    This don't let your textview has more than two lines and put an ellipsis on the end.

    Maybe this would be a solution.