Mohsen Afshin
Mohsen Afshin

Reputation: 13436

AlertDialog Theme: How to change item text color?

When I try to apply a standard theme to AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....

Result:

enter image description here

The note is that I have no problem with builder.setItems(...) since its text color is Black while the theme applied with builder.setSingleChoiceItems(...) has a White text color.

Any fast fix? Or any way to create a customized theme based on AlertDialog.THEME_DEVICE_DEFAULT_LIGHT ?

My customized style doesn't work as expected:

<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
    <item name="android:textColor">#7ABDFF</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>


    <!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->

    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textColorAlertDialogListItem">#A844BD</item>
    <item name="android:itemBackground">#7ABDFF</item>
</style>

Update

@lopez answer is a complete solution, but I find a single line fix for my problem, a custom theme to be applied to the activity in manifest:

<style name="MyTheme">
    <item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>

Upvotes: 29

Views: 29764

Answers (5)

yigitserin
yigitserin

Reputation: 611

This is what worked for me for every color on my dialog:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#00ffff</item>
    <!-- Used for the title and text -->
    <item name="android:textColor">#00ff00</item>
    <item name="android:textColorAlertDialogListItem">#ffff00</item>
    <item name="android:textColorSecondary">#ff00ff</item>
    <!-- Used for the background -->
    <item name="android:background">#3a3a3c</item>
</style>

Every color of my alert dialog.

Upvotes: 0

Suneet Srivastava
Suneet Srivastava

Reputation: 571

What works for me

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@color/text</item>
    <item name="android:background">@color/background</item>
    <item name="android:textColorPrimary">@color/text</item>
    <item name="textColorAlertDialogListItem">@color/text</item>
</style>

Upvotes: 4

enyciaa
enyciaa

Reputation: 2122

If anyone happens to read this, and is using the Suport library Alert Dialogs and wants to change the text color of list items then drop the android: part, like so:

<item name="textColorAlertDialogListItem">@color/your_color</item>

Upvotes: 66

Jeekiran
Jeekiran

Reputation: 473

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....**change this line to**

builder.setSingleChoiceItems(info,0,null);

Upvotes: -4

lopez.mikhael
lopez.mikhael

Reputation: 10061

Personally, I use the android Dialog but I use a custom layout for that match the design of my application.

Here is an example:

new AlertDialog.Builder(context)
.setView(inflater.inflate(R.layout.dialog_delete_contact, null))
.setPositiveButton(context.getResources().getString(android.R.string.ok).toUpperCase(), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // YOUR TREATMENT
    }
})
.setNegativeButton(context.getResources().getString(android.R.string.cancel).toUpperCase(), null)
.show();

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/GrayLight"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/Black"
    android:gravity="center"
    android:orientation="horizontal"
    tools:ignore="DisableBaselineAlignment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true"
            android:padding="10dip"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remove_contact"
            android:textColor="@color/White"
            android:textSize="20sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    tools:ignore="DisableBaselineAlignment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp"
        android:text="@string/ask_remove_contact"
        android:textSize="15sp"
        tools:ignore="HardcodedText" />

</LinearLayout>

Result in picture:

enter image description here

To avoid rewriting the code every time here is a utility class :

public class MyDialog {

public static Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
    View view = layoutInflater.inflate(R.layout.generic_dialog, null);
    ((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
    ((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
    return new AlertDialog.Builder(context).setView(view);
}

}

And an example of using :

AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), "Quitter ECOLEMS", "Voulez-vous vraiment quitter l'application?");
myDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // YOUR TREATMENT
    }
})
.setNegativeButton("Non", null)
.show();

I hope you have helped!

Upvotes: 5

Related Questions