3

I implemented a DialogPreference exactly the way it is explained in http://www.lukehorvat.com/blog/android-seekbardialogpreference

Additionally I was able to change the text- and divider color of the DialogPreference, but I couldn't change the highlighting color of the buttons when they are pressed. Does anybody know how to do this?

Update:

I use the following layout for the DialogPreference:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
<TextView
        android:id="@+id/text_dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dip"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"/>
<TextView
        android:id="@+id/text_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dip"
        android:gravity="center_horizontal"/>
<SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginTop="6dip"/>
</LinearLayout>

The only style attributes regarding this DialogPreference or the layout I change so far are changed programatically:

        int alertTitleId = this.getContext().getResources().getIdentifier("alertTitle", "id", "android");
        TextView alertTitle = (TextView) getDialog().getWindow().getDecorView().findViewById(alertTitleId);
        alertTitle.setTextColor(color); // change title text color

        int titleDividerId = this.getContext().getResources().getIdentifier("titleDivider", "id", "android");
        View titleDivider = getDialog().getWindow().getDecorView().findViewById(titleDividerId);
        titleDivider.setBackgroundColor(color); // change divider color
ashiaka
  • 3,994
  • 8
  • 32
  • 45

3 Answers3

4

All you need to do is subclass DialogPreference, then call Resource.getIdentifier to locate each View you want to theme, much like you're doing, but you don't need to call Window.getDecorView. Here's an example:

Custom DialogPreference

public class CustomDialogPreference extends DialogPreference {

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

        // Button views
        window.findViewById(res.getIdentifier("button1", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));
        window.findViewById(res.getIdentifier("button2", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));
        window.findViewById(res.getIdentifier("button3", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));

    }

}

XML preferences

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <path_to.CustomDialogPreference
        android:dialogMessage="Message"
        android:negativeButtonText="Cancel"
        android:positiveButtonText="Okay"
        android:title="Title" />

</PreferenceScreen>

Custom selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true">

    <item android:drawable="@drawable/your_pressed_drawable" android:state_pressed="true"/>
    <item android:drawable="@drawable/your_default_drawable"/>

</selector>

Alternate selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true">

    <item android:drawable="@color/your_pressed_color" android:state_pressed="true"/>
    <item android:drawable="@color/your_default_color/>

</selector>

Screenshot

Example

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Thanks for your answer, but I'm afraid it's not the answer I am looking for. I'd like to change the color of the button the moment it is pressed :) – ashiaka Mar 24 '14 at 07:49
  • Yeah, exactly. My example doesn't include a `selector`, but that's all you need. Then just call `View.setBackgroundDrawable`. I'll make an edit. – adneal Mar 24 '14 at 11:29
0

If you fail to find a solution for styling the built-in buttons to your liking, you could actually add a button row to the bottom of your custom layout, which looks and acts exactly like the built-in one. Then set your button listeners to your custom button bar's buttons, which will result in no built-in button bar.

In this way you can make them look however you want!

Josh
  • 10,618
  • 2
  • 32
  • 36
0

I you can try this Answer. Here you don't need write code just need customise AlertDialog theme.

After customisation theme it may be applicable for your complete application.

Community
  • 1
  • 1
Sachin Shelke
  • 449
  • 4
  • 12