Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?
Asked Answered
T

8

13

I want to change AlertDialog title color and background color without using custom layout. My requirement,

I want like this

I tried below code, but can't work.

final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
            .setTitle(title).setCancelable(false);

    builder.setItems(items, (dialog, item) -> {
    });

    AlertDialog dialog = builder.create();
            dialog.show();
    int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    TextView tv = dialog.findViewById(textViewId); // It always returns null
    if (tv != null) {
        tv.setTextColor(activity.getResources().getColor(R.color.white));
        tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}

Using below lines I tried but it always returns null in findViewById,

int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);

I also tried using style but it only change Title text color,

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:background">#ffffff</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:headerBackground">@color/colorPrimary</item>
</style>
Tectonics answered 17/7, 2018 at 11:21 Comment(10)
you can change via themeSuitor
Possible duplicate of Alert Dialog background theme/ColorHitandmiss
try this, but it uses a custom view for header. but very simple and neat ans. https://mcmap.net/q/904605/-set-the-title-background-color-of-alert-dialog-without-making-a-custome-dialogBartender
@SaurabhBhandari, my question is different, I want to change title text background not dialog background.Tectonics
@SagarZala in theme add your text color attribute and for background add background attributeHitandmiss
@SaurabhBhandari, Using android:textColor we can change text color, but how can change title background?Tectonics
textColorPrimary used for title text and colorPrimary used for backgroundHitandmiss
textColorPrimary not works, I tried.Tectonics
Then you need to set Custom title View in your java fileHitandmiss
Looks like findViewById() to find the title doesn't work, android:windowTitleBackgroundStyle to change title background color doesn't work. I guess setCustomTitle() is only way.Insincerity
V
16

You can use custom title to your alert dialog:

TextView textView = new TextView(context);
textView.setText("Select an option");
textView.setPadding(20, 30, 20, 30);
textView.setTextSize(20F);
textView.setBackgroundColor(Color.CYAN);
textView.setTextColor(Color.WHITE);

final CharSequence[] items = {"Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCustomTitle(textView);
builder.setItems(items, (dialog, item) -> {
    }).show();

Custom Alert Dialog Header

Vogele answered 17/7, 2018 at 11:28 Comment(7)
I only want to change title text color and background color.Tectonics
It will change your title text colorVogele
Yes, but what about title background color?Tectonics
Now this is exactly what you wanted.Vogele
My question is without using custom layout, but its is good solution. For now I can use this. Thanks.Tectonics
Let us continue this discussion in chat.Vogele
setPadding takes pixel values, so would be better to take the value from resources with getResources().getDimensionPixelSize(R.dimen.dialog_text_padding). About the size/style I would better use setTextAppearance( android.R.style.TextAppearance_Material_DialogWindowTitle );Vosges
I
4

You can change everything in the alert dialog:

// Title
TextView titleView = new TextView(context);
titleView.setText("Title");
titleView.setGravity(Gravity.CENTER);
titleView.setPadding(20, 20, 20, 20);
titleView.setTextSize(20F);
titleView.setTypeface(Typeface.DEFAULT_BOLD);
titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
titleView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

AlertDialog ad = new AlertDialog.Builder(context).create();

ad.setCustomTitle(titleView);

ad.setCancelable(false);

ad.setMessage("Message");

ad.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // your code
    }
});

ad.show();

// Message
TextView messageView = ad.findViewById(android.R.id.message);

if (messageView != null) {
    messageView.setGravity(Gravity.CENTER);
}

// Buttons
Button buttonOK = ad.getButton(DialogInterface.BUTTON_POSITIVE);
buttonOK.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
Incult answered 17/7, 2018 at 12:52 Comment(0)
Y
4

check sample image

You can change color of alert dialog title, background and button color by using custom theme.

   <style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
        <item name="android:windowBackground">@android:color/black</item>
        <item name="colorAccent">@android:color/white</item>
        <item name="android:textColorPrimary">@android:color/white</item>
    </style>

android: windowBackground to change background color

colorAccent to change button color

android:textColorPrimary to change Dialog title color

apply this theme to dialog

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialogTheme);
Yasmeen answered 11/2, 2019 at 1:27 Comment(1)
No help for AlertDialog title.Insincerity
A
2

IMO you call dialog.show(); before set color so try below code

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(message)
        .setTitle(title).setCancelable(false);
AlertDialog dialog = builder.create();

int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
    tv.setTextColor(activity.getResources().getColor(R.color.white));
    tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));

    }
  dialog.show(); //change here 

update

just try where you set title to alert below line

alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Hello World</font>"));
Affright answered 17/7, 2018 at 12:17 Comment(3)
Using style I can change font color, what about background??Tectonics
for this try dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack); ...Affright
omik, this changes the whole dialog background to that color. not the title.Empathic
M
1

I know you don't want this, but using a View is the best way to do this. With a custom View you can change every Color very easy.

  1. Just create a Layout and put your items there (and change the colors)

  2. Create a LayoutInflater:LayoutInflater layoutinflater = getLayoutInflater;

  3. Set a View like: View view1 = layoutinflater.inflate(R.layout.yourlayout, null);

  4. Set view1 to your builder: builder.setView(view1);

If you want to use items in the AlertDialog, declare your variables with view1!

Example: Textview tx = (TextView)view1.findViewById(R.id.textview)

Misvalue answered 17/7, 2018 at 11:38 Comment(2)
No, I don't want to use custom layout.Tectonics
@SagarZala Why? It's very easy and you've got more options to customize your AlertDialogMisvalue
C
1

You can set theme:

new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);

or you can add setOnShowListener() like below:

final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
            .setTitle(title).setCancelable(false);

builder.setItems(items, (dialog, item) -> {
});

AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface arg0) {
                    int titleId = getResources().getIdentifier("alertTitle", "id", "android");
                    TextView dialogTitle = (TextView) dialog.findViewById(titleId);
                    dialogTitle.setTextColor(Color.WHITE);
                    dialogTitle.setBackgroundColor(Color.BLACK);
                }
 });
 dialog.show();
Crampon answered 17/7, 2018 at 13:3 Comment(1)
getResources().getIdentifier("alertTitle", "id", "android") doesn't work. Returns null object.Insincerity
B
-1

That's what worked for me, so simple: my dialog for some reason showed the title background different than the dialog background, so after adding this code, the problem solved.

after dialog show, set setBackgroundDrawable with your color:

mDialog.show();

mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(put here your color));
Bashibazouk answered 25/12, 2019 at 9:21 Comment(0)
S
-2

You can change via code as well.

AlertDialog dialog = builder.create();
dialog.show();
Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));

Hope that helps you.

Suitor answered 17/7, 2018 at 11:29 Comment(1)
I don't want to change button color. I want to change title text and background color.Tectonics

© 2022 - 2024 — McMap. All rights reserved.