Android - change dialog title style of all dialogs in application

Viewed 27900

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.

3 Answers

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