Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 291
Roshan Avatar asked Dec 12 '15 12:12

Roshan


People also ask

How do I change the background color on AlertDialog builder?

Use setInverseBackgroundForced(true) on the alert dialog builder to invert the background.

How do I change the background color of alert dialog in flutter?

You need to wrap your Dialog in a Builder like this. After that dialogBackgroundColor will have an effect. Show activity on this post. You can now use backgroundColor property of AlertDialog to change the color.

How to set theme of alertdialog in Android?

AlertDialog has some constants that provides alert theme. To set title, message, icon, button etc we need AlertDialog.Builder that can be instantiated by using two constructors. 1. AlertDialog.Builder (Context context) : Here we pass context only. 2. AlertDialog.Builder (Context context, int theme): This also accepts theme as an argument.

How to set title and icon button in alertdialog?

Instantiate AlertDialog.Builder to set title, icon button etc. These act as OK, Cancel, Yes, No etc button and on click of which an action is performed. To perform it DialogInterface.OnClickListener provides onClick () method which is implemented.

How do I change the color of a layout in Android?

With a custom View you can change every Color very easy. Just create a Layout and put your items there (and change the colors) Create a LayoutInflater: LayoutInflater layoutinflater = getLayoutInflater; Set a View like: View view1 = layoutinflater.inflate (R.layout.yourlayout, null);

How to create dialog dialog in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to res/layout/my_dialog.xml.


1 Answers

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

like image 158
Sam Chen Avatar answered Oct 26 '22 18:10

Sam Chen