Roshan
Roshan

Reputation: 3592

Android AlertDialog title background color

Is there any way to change the title background color of AlertDialog (android.support.v7.app.AlertDialog)?? Currently in my theme I have

 <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>

 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
  </style>

and I am getting it like this,

enter image description here

How can I make it look like this,

enter image description here

Using

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowTitleStyle">@style/DialogTitle</item>
    </style>

    <style name="DialogTitle">
        <item name="android:background">@color/colorPrimary</item>
    </style>

gives

enter image description here

Any ideas on how this can be accomplished?

Upvotes: 11

Views: 11812

Answers (4)

G Dias
G Dias

Reputation: 89

With androidx.appcompat.app.AlertDialog

AlertDialog alertdialog;
...
alertdialog.show();
final View v = alertdialog.findViewById(R.id.title_template);
if (v != null)
    v.setBackgroundColor(myColor);

Upvotes: 0

Sam Chen
Sam Chen

Reputation: 8847

I guess, the reason why custom style doesn't work is because the default padding (around the whole AlertDialog layout) remains there:

<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowTitleStyle">@style/myDialogTitle</item>
</style>

<style name="myDialogTitle" parent="TextAppearance.AppCompat.Title">
    <item name="android:background">@color/colorPrimary</item>
</style>

enter image description here

Also getResource.getIdentifier() and findViewByid() don't work (I've tried so far). Therefore, setCustomTitle() becomes the only way to make it.


1. Creating a Custom Title Layout for AlertDialog

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:orientation="vertical"
    android:paddingStart="24dp"                         //following Material Design guideline
    android:paddingTop="16dp"
    android:paddingEnd="24dp"
    android:paddingBottom="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Marvel Cinematic Universe"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"      //key point 1, using AlertDialog default text style
        android:textColor="@android:color/white" />

</LinearLayout>

2. Applying Custom Title Layout to AlertDialog Using "setCustomTitle()"

AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
View titleView = getLayoutInflater().inflate(R.layout.dialog_title, null);

builder.setCustomTitle(titleView);                      //key point 2
builder.setMessage(R.string.main_marvel_info);
builder.setPositiveButton("OK", null);

builder.create().show();

enter image description here

Upvotes: 9

ice spirit
ice spirit

Reputation: 1535

You can just set custom title like this

LayoutInflater inflater = this.getLayoutInflater();

    View titleView = inflater.inflate(R.layout.custom_title, null);

    new AlertDialog.Builder(SubCategoryActivity.this)
                        .setCustomTitle(titleView);

and in custom_title layout you can create custom title like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/llsubhead"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/exemptionSubHeading4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:text="Exemption Sub Head"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

Upvotes: 14

theshivamlko
theshivamlko

Reputation: 281

Use custom Alerbox,use this code on click.I made a custom Layout "alert_input" and OK and Cancel option will be shown

 LayoutInflater layoutInflater = LayoutInflater.from(Login.this);
                View promptView = layoutInflater.inflate(R.layout.alert_input, null);
                final EditText editText = (EditText) promptView.findViewById(R.id.alertEdit2);
                final EditText editText2 = (EditText) promptView.findViewById(R.id.alertEdit3);
                final TextView at=(TextView)findViewById(R.id.alertText);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this,AlertDialog.THEME_HOLO_LIGHT);
                alertDialogBuilder.setView(promptView);
                alertDialogBuilder.setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {


                            }
                        })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                // create an alert dialog
                AlertDialog alert = alertDialogBuilder.create();
                alert.show();

Upvotes: 0

Related Questions