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?
Asked
Active
Viewed 1,437 times
0
-
2IMO 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. – Skizo-ozᴉʞS ツ Feb 19 '15 at 11:00
-
Just provide your custom row layout. – Phantômaxx Feb 19 '15 at 11:02
-
Thanks for your response. I'm creating another layout – user232803 Feb 19 '15 at 11:26
-
http://stackoverflow.com/questions/16062569/how-to-construct-and-display-the-info-in-simple-list-item-2 this may help – Driss Bounouar Feb 19 '15 at 12:25
2 Answers
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
-
-
I'm getting error in this line as my current min is 11.Is there anything else to replace this? android:textAppearance="?android:attr/textAppearanceListItem" – user232803 Feb 19 '15 at 12:59
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