4

I'm trying to style the dialog box that appears when I select an EditTextPreference in my SettingsActivity. So far I have been able to change the colour of the two buttons and the background with:

    dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(0xffffffff);
    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(0xffffffff);
    dialog.getWindow().setBackgroundDrawableResource(R.drawable.blue_background);

But I am so far unable to change the title at the top of the dialog.

enter image description here

The colour of the 'Zone' title is being retrieved from a style I use for the preference list, it's being inherited and I can't figure out how to assign it a style that will change that title colour

<style name="MyPreferenceTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@color/care_call_white</item>
    <item name="android:textColor">@drawable/text_colour_state</item>
    <item name="android:textColorSecondary">@drawable/text_colour_state</item>
</style>

Can anyone help with a style that could do this? Or with some code that could possibly set it instead? All my dialog boxes are styled the same but this one is trickier as it's part of the Preference so I can't customise it the same way I did with the others.

Just to clarify, the only thing that needs changing is the "Zone" title.. I would like it to be white.

Murphybro2
  • 2,207
  • 1
  • 22
  • 36

4 Answers4

2

With new AndroidX Preference library there is no onPrepareDialogBuilder in EditTextPreference.

I've proceeded in this way:

My base app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
     //add here your properties
</style>

my preference theme:

<style name="AppTheme.Settings">
     <!-- This will change the opening dialogs for EditTextPreference -->
     <item name="alertDialogStyle">@style/Theme.AlertDialog.Default</item>
</style>

in my case Theme.AlertDialog.Default is a custom Dialog theme with with parent ThemeOverlay.MaterialComponents.MaterialAlertDialog

Then in your AndroidManifest.xml simply apply the theme to the the Activity which handle the preferences

MatPag
  • 41,742
  • 14
  • 105
  • 114
1

Sorted.

I created a CustomEditTextPreference class that inherited EditTextPreference and then overrode

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    builder.getContext().setTheme(R.style.PreferenceThemeDialog);
    super.onPrepareDialogBuilder(builder);
}

and changed the style in the builders context with

<style name="PreferenceThemeDialog">
    <item name="android:textColor">@color/care_call_white</item>
</style>
Murphybro2
  • 2,207
  • 1
  • 22
  • 36
0

One way you can change the theme or the title of a preference is by finding it within your settings activity or fragment and then editing the attributes.

To edit the theme you would do this:

findPreference<EditTextPreference>(YOUR_KEY_GOES_HERE)?.context?.setTheme(YOUR.THEME.GOES.HERE)

To change the title you would do this:

findPreference<EditTextPreference>(BACKGROUND_SYNC_PERIOD_KEY)?.dialogTitle = YOUR_TITLE_GOES_HERE

Both of these are in kotlin but it is essentially the same thing in java.(you can paste this into android studio and it should format it).

-1
<style name="cust_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowTitleStyle">@style/dialog_title_style</item>
</style>

<style name="dialog_title_style" parent="android:Widget.TextView">
<item name="android:background">@android:color/black</item>
<item name="android:padding">10dp</item>
</style>

And you can instantiate dialog:

Dialog dialog=new Dialog(this,R.style.cust_dialog);
dialog.setContentView(R.layout.fragment_features_dialog);
dialog.setTitle(R.string.features);

Now the dialog shows up with black title background color.

Kristo
  • 1,339
  • 12
  • 22
  • 1
    With EditTextPreference you don't create the dialog object you get it given to you.. so I can't input the style on creation – Murphybro2 Feb 17 '16 at 11:16
  • yes but you are saying that you are trying to style the dialog box.anyway good luck. And you are using methods of the dialog class..so give it a try.! – Kristo Feb 17 '16 at 11:18