Android - change dialog title style of all dialogs in application

27,040

Solution 1

In you dialog theme, the attribute you need to modify windowTitleStyle. There, reference a style for your title and define a text appearance for this title. For example, to display your title red and bold:

<style name="AppTheme" parent="@android:style/Theme.Holo">
    ...  
    <item name="alertDialogTheme">@style/AppTheme.AlertDialog</item>
</style>

<style name="DialogStyle" parent="@style/Theme.Holo.Dialog.Alert">    
    ...
    <item name="android:windowTitleStyle">@style/DialogWindowTitle_Custom</item>
</style>

<style name="DialogWindowTitle_Custom" parent="@style/DialogWindowTitle_Holo">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@style/TextAppearance_DialogWindowTitle</item>
</style>

<style name="TextAppearance_DialogWindowTitle" parent="@android:style/TextAppearance.Holo.DialogWindowTitle">
    <item name="android:textColor">@android:color/holo_red_light</item>
    <item name="android:textStyle">bold</item>
</style>

If you want to style a little bit more your AlertDialog, I wrote a blog post detailing the steps.

Solution 2

In styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
       //All your other styles and add the one mntioned below.

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

In the styles of AlertDialog add:

 <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/primary_text</item>
        <item name="android:editTextColor">@color/primary_text</item>
        <item name="android:background">@color/white</item>
        <item name="android:windowTitleStyle">@style/TextAppearance.AppCompat.Title</item>
    </style>

windowTitleStyle is important to add as it will give your title that effect that you need.

Now simply use this Theme in your AlertDialog in any activity that you want like below:

 AlertDialog.Builder dialog = new AlertDialog.Builder(this, R.style.AlertDialogStyle);

Solution 3

import android.app.ProgressDialog;
import android.content.Context;
import android.view.animation.Animation;

public class MyProgressDialog extends ProgressDialog {

    public MyProgressDialog(Context context) {
        super(context,R.style.NewDialog);

        // TODO Auto-generated constructor stub
    }

}

//RESOURCE STYLE FILE res->values->mydialog.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="NewDialog" parent="@android:style/Theme.Dialog">

    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:colorBackground">#ffffff</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:width">30dip</item>
    <item name="android:height">30dip</item>
     <item name="android:textColor">#FF0000</item>

</style>
</resources>

//AND USE THIS CODE ANYWHERE IN YOUR APPLICATION TO GET CUSTOM PROGRESS DIALOG

MyProgressDialog pd = new MyProgressDialog(YouActivity.this);
pd.setMessage("Loading. Please wait...");
pd.show();

Thanks :)

Share:
27,040
jaibatrik
Author by

jaibatrik

I am a software developer and a user-experience enthusiast. I like technologies that enable us to design new products which will make others' life more bearable, comfortable and happy. Some of the technologies and patterns I admire include JavaScript, Node, React, GraphQL, Angular, Java, the Android platform, concepts of OOP, MVC, Flux, Reactive Programming, and many more. There are some tools we have developed in the playful pursuit of gaining more knowledge. And yes, they are free. AngularX-Social-Login EasyTodo - Smart Todo List for Android Imagine Picture Viewer for Windows XP / Vista / 7 (32 bit) Sectioned ListView Adapter for Android

Updated on September 13, 2020

Comments

  • jaibatrik
    jaibatrik almost 4 years

    Is there a way I can change all the alert dialogs appearing in my Android application? I want to change the dialogs that are system generated as well (like the Edit Text dialog that opens up when you long tap on any EditText). I want to change the title font color and size of all the dialogs in my app.

    Is there a way to do it?

    I have tried setting android:alertDialogTheme in my theme. But it seems to be not working. Code following -

    <style name="Theme.DLight" parent="android:Theme.Light.NoTitleBar">
        <item name="android:alertDialogTheme">@style/DialogStyle</item>
    </style>
    

    and

    <style name="DialogStyle" parent="android:Theme" >    
        <item name="android:windowBackground">@drawable/background_holo_light</item>
        <item name="android:textColor">#014076</item>       
    </style>
    

    EDIT

    I'm not invoking a dialog from my code. It's just the default dialog that appears when you long click on any EditText. Generally it contains the keyboard options like Select word, Select all, Input method etc.

  • jaibatrik
    jaibatrik almost 12 years
    Hi, I'm not invoking a dialog from my code. It's just the default dialog that appears when you long click on any EditText. Generally it contains the keyboard options like Select word, Select all, Input method etc.
  • moonlightcheese
    moonlightcheese over 10 years
    this answer doesn't satisfy the question. he's not calling any constructor for the dialog in question. he wants to change the theme for ALL DIALOGS by applying an application theme, so that styles do not have to be applied explicitly when a dialog is instantiated, but apply to every dialog automatically throughout the application.
  • User
    User over 4 years
    If it's necessary to pass the style to Builder, why have it in the app theme as well?
  • wasanga7
    wasanga7 almost 4 years
    Link is down :(
  • David Ferrand
    David Ferrand almost 4 years
    @wasanga7 Updated -- try now