Reputation: 2746
I am trying to force the background color for certain ListView items in my app to be different than all the others. The examples here on Stack Overflow and elsewhere are pretty straightforward. But for some reason the background color changes only as far as the width of the top TextView. Notice in the attached pic there are two TextView items on each row.
I decided to log the width of the list view item and oddly it turns out it has a width of 0. Unexpected, of course, but maybe at the time of the logging (execution of getView) it has not yet been expanded to their full with.
I am setting the background color in the getView() call, so its code is below. The xml for the item is below that.
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
if(arg1 == null)
{
LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem_assessments, arg2, false);
}
TextView title = (TextView)arg1.findViewById(R.id.tvTitle);
TextView date = (TextView)arg1.findViewById(R.id.tvDate);
if (arg0 <= m_list.size())
{
Attrs attrs = m_list.get(arg0);
title.setText(attrs.m_sTitle);
date.setText(attrs.m_sDetail);
if (Character.isDigit(attrs.m_sActivationCount.charAt(0)))
{
if (!attrs.m_sActivationCount.equals("0"))
{
Log.i("Adapter", " Width of list item view is: " + arg1.getWidth());
arg1.setBackgroundColor(Color.YELLOW);
}
else
Log.i("Adapter", "Width of list item view is: " + arg1.getWidth());
}
else
arg1.setTag("");
return arg1;
}
Here is the xml resource for the ListView items:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:text="Assessment title"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_alignLeft="@+id/tvTitle"
android:background="@color/LightBlue"
android:text="date or author" />
</RelativeLayout>
Notice I'm using match_parent for the list item width so seems it would stretch out fully, and certainly not be 0. Nor since it is not wrap_content should it extends only as far as the widest TextView.
To be clear, I need the background color, Yellow, in my test, to extend all the way to the right. Any help greatly appreciated.
UPDATE
I have been able to log some accurate width info by moving the code into the OnItemClickListener event. The width of the entire ListView is 1312, but the width of the ListView items are indeed too short, and vary as the yellow backgrounds reveal. The first item is 696. It is clear now that the layout is not stretching things out to match the parent. How to force it to do that? It seems to be behaving as if I am using match_content instead of match_parent for the layout_width. Trying now to verify at run-time (at inflate time) the layout_width.
Upvotes: 0
Views: 489
Reputation:
The problem is in the width of your TextViews as they are set to wrap_content. Change them to
android:match_parent
Upvotes: 1