Sagar Zala
Sagar Zala

Reputation: 5134

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

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>

Upvotes: 13

Views: 23168

Answers (8)

batsheva
batsheva

Reputation: 2295

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));

Upvotes: -1

AndroChunk
AndroChunk

Reputation: 49

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);

Upvotes: 4

Anisuzzaman Babla
Anisuzzaman Babla

Reputation: 7480

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

Upvotes: 16

user8959091
user8959091

Reputation:

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));

Upvotes: 4

Grygorii
Grygorii

Reputation: 342

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();

Upvotes: 1

Omkar
Omkar

Reputation: 3100

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>"));

Upvotes: 2

Jakob
Jakob

Reputation: 1895

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)

Upvotes: 1

Abdul Waheed
Abdul Waheed

Reputation: 4678

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.

Upvotes: -2

Related Questions