Android: how to set text color for list items in AlertDialog correctly

I have an AlertDialog in my application. It contains a list of custom views with TextView widgets inside. Everything works fine on Android 2.x. AlertDialog is created with a white list and black text. But when I run my application on Android 3.x devices, all TextView black and the background of the list is also black. Therefore, I cannot see the text until I touch one of the elements.

Here is the definition of the TextView from the layout file:

 <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:textAppearance="?android:attr/textAppearanceSmallInverse" /> 

I thought using textAppearanceSmallInverse for the textAppearance attribute is the correct way to set text parameters, and it should work on all devices, but it seems like I was wrong. So what should I do to enable AlertDialog display list items on all platforms? Thanks in advance.

+7
source share
4 answers

The solution is to use the built-in Android resource selection system. You must specify two different styles and place them in the appropriate folders based on the version of the API. Please note that the following examples are not mine, I took them from this tutorial.

res/values-v4/styles.xml :

 <resources> <!-- Text for listboxes, inverted for Andorid prior to 3.0 --> <style name="MyListTextAppearanceSmall"> <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item> </style> <style name="MyListTextAppearanceDefault"> <item name="android:textAppearance">?android:attr/textAppearanceInverse</item> </style> <style name="MyListTextAppearanceMedium"> <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item> </style> </resources> 

res/values-v11/styles.xml :

 <resources> <!-- Text for listboxes, non-inverted starting with Android 3.0 --> <style name="MyListTextAppearanceSmall"> <item name="android:textAppearance">?android:attr/textAppearanceSmall</item> </style> <style name="MyListTextAppearanceDefault"> <item name="android:textAppearance">?android:attr/textAppearance</item> </style> <style name="MyListTextAppearanceMedium"> <item name="android:textAppearance">?android:attr/textAppearanceMedium</item> </style> </resources> 

Then in the TextView specify the style:

 <TextView android:style="@style/MyListTextAppearanceSmall" android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" /> 

See the tutorial above for a more detailed explanation.

+2
source

Your code for the popup dialog should look something like this:

 // Sets dialog for popup dialog list AlertDialog dialog; String[] items = {"exampleItem"}; ListAdapter itemlist = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title"); builder.setAdapter(itemlist, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { } }); dialog = builder.create(); dialog.getListView().setBackgroundColor(Color.WHITE); 

Here you get a list and set the background color to white. If you want to change the text color for each of the text views, you need to determine their color in the textview layout, in this case black:

 android:textColor="#000000" 
+1
source

The accepted answer seems a bit redundant. I just made the reverse call:

 dialogBuilder.setInverseBackgroundForced(true); 

perfectly solves the problem.

+1
source

This probably happens because you are not specifying a theme, then it reverts to the default theme. In 2.x it should be Theme.Black and in 3.x Theme.Holo (or Theme.Light, not sure about that). Then textAppearanceSmallInverse resolves a different style in each theme.

0
source

All Articles