3

I have an application that uses a preference activity to set some user settings. I been trying to figure this out all day. I am trying to theme the alert dialog when an user presses an Edit Text Preference object. A dialog opens up and the user can set the shared preference. The dialog pops up:

enter image description here

I want the text green. I want the divider green. The line and cursor green.

This is what I have so far.

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:background">@color/text_green</item>
    <item name="android:textColor">@color/text_green</item>
</style>

Can someone point me in the right direction or maybe share some code. I am at lost. I've been surfing the net to find something most of the day. Thanks in advance.

adneal
  • 30,484
  • 10
  • 122
  • 151
Peter Sun
  • 1,675
  • 4
  • 27
  • 50
  • This may help lead to your answer: http://stackoverflow.com/questions/7188339/android-how-to-change-the-style-of-edit-text – Jwc24678 Mar 23 '14 at 00:58

2 Answers2

6

If you don't want to create a custom layout or use a third party library, you can subclass EditTextPreference, then access each View you want to edit by using Resources.getIdentifier then using Window.findViewById. Here's a quick example.

public class CustomDialogPreference extends EditTextPreference {

    public CustomDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);
        final Resources res = getContext().getResources();
        final Window window = getDialog().getWindow();
        final int green = res.getColor(android.R.color.holo_green_dark);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = window.findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(green);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = window.findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(green);
        }

        // EditText
        final View editText = window.findViewById(android.R.id.edit);
        if (editText != null) {
            editText.setBackground(res.getDrawable(R.drawable.apptheme_edit_text_holo_light));
        }
    }
}

Implementation

Replace <EditTextPreference.../> with <path_to_CustomDialogPreference.../> in your xml.

Note

I used Android Holo Colors to create the background for the EditText.

Example

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Cool.. I'll give that a try... Can you basically use it for all preference dog types? – Peter Sun Mar 23 '14 at 01:50
  • If you want to replace any `DialogPreference` with it then you'll need to subclass `DialogPreference` instead of `EditTextPreference`. – adneal Mar 23 '14 at 01:56
  • i assume you basically call the class like.. `` in your preference layout? – Peter Sun Mar 23 '14 at 02:18
  • @user2252502 Yeah, I made an edit to my post about that. – adneal Mar 23 '14 at 02:24
  • so far so good... questions.. where did you learn this stuff. one thing I am struggling with android is a good reference – Peter Sun Mar 23 '14 at 02:54
  • @user2252502 Practice. Some good references to start with are [Android Developers website](http://developer.android.com/index.html) or the [Android Developers Google+ page](https://plus.google.com/+AndroidDevelopers). – adneal Mar 23 '14 at 03:00
  • so i am trying to user the Android Developers Website to figure out how to change the ListPreference RadioButton color. I have the drawables I got from Android Holo Colors. I just not sure what to set. so far I have `final View radiobt = window.findViewById(?)`. I can't find anything that matches a radiobutton or radiobutton group. thanks for your help so far.. i trying to understand android a little better – Peter Sun Mar 23 '14 at 04:48
  • You'll want to use the same structure as changing the title text color. But instead of casting `TextView`, cast `CheckedTextView` and instead of `TextView.setTextColor` use `CheckedTextView.setCheckedDrawable`. It should work if you're using the id `android.R.id.text1`. – adneal Mar 23 '14 at 06:03
  • So far this is what I got `final int checkId = res.getIdentifier("android.R.id.text1", "id", "android"); final View check = window.findViewById(checkId); if (check != null) { //No method setCheckedDrawable }` Where did you get the info about what id is to what? – Peter Sun Mar 23 '14 at 06:24
  • Browsing the [AlertController](https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/AlertController.java#L200) source. – adneal Mar 23 '14 at 06:33
  • thank for the help.. the list preference didn't work..i ended up creating a custom list preference... – Peter Sun Mar 24 '14 at 03:34
  • How would you do this using styles and themes in xml? – tzg Apr 24 '19 at 16:10
  • I got this error when creating this class. There is no default constructor available in 'android.preference.EditTextPreference' – tzg Apr 24 '19 at 16:24
-1

You can build your custom layout for your own dialog theme using your own customized components or you can use external libs, for example android-styled-dialogs

So in this case use can customize dialogs as you want:

<style name="DialogStyleLight.Custom">
    <!-- anything can be left out: -->
    <item name="titleTextColor">@color/dialog_title_text</item>
    <item name="titleSeparatorColor">@color/dialog_title_separator</item>
    <item name="messageTextColor">@color/dialog_message_text</item>
    <item name="buttonTextColor">@color/dialog_button_text</item>
    <item name="buttonSeparatorColor">@color/dialog_button_separator</item>
    <item name="buttonBackgroundColorNormal">@color/dialog_button_normal</item>
    <item name="buttonBackgroundColorPressed">@color/dialog_button_pressed</item>
    <item name="buttonBackgroundColorFocused">@color/dialog_button_focused</item>
    <item name="dialogBackground">@drawable/dialog_background</item>
</style>
Govtart
  • 332
  • 1
  • 9