0

I've used simple_list_item_2 in my code to create listview.Everything is working alright, but i want to change the background color of the layout.Is it possible? Can this layout also be customized like user defined XML?

user232803
  • 365
  • 6
  • 19

2 Answers2

1

Just make your own simple_list_item_2 like this, and use it instead

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
   <!-- This is your background color-->
   android:background="#DC143C"

   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:minHeight="?android:attr/listPreferredItemHeight"
   android:mode="twoLine"
   android:paddingStart="?android:attr/listPreferredItemPaddingStart"
   android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
      <TextView android:id="@android:id/text1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="8dip"
             android:textAppearance="?android:attr/textAppearanceListItem" />
      <TextView android:id="@android:id/text2"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_below="@android:id/text1"
             android:layout_alignStart="@android:id/text1"
             android:textAppearance="?android:attr/textAppearanceListItemSecondary" />
 </TwoLineListItem>

(Taken from the android source code)

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
1

IMO you cannot , but you can create another layout with the same code, and then replace the background of the simple_list_item_2 and then change it with the color you want, and then instead of call simple_list_item_2 call your new layout and it will work.

EDIT*

Following the answer of @JonasCz, you can edit it and a sample code to replace it would be like :

ListView l = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Ubuntu", "Android", "iPhone",
    "Windows", "Ubuntu", "Android", "iPhone", "Windows" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.your_custom_simple_list_item_2, values); //removed the android. 
viewContainer = findViewById(R.id.undobar);
l.setAdapter(adapter);

The code is from AndroidListView/article.html

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148