Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the highlighting colors (button colors) of a "DialogPreference"

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
like image 709
ashiaka Avatar asked Jan 18 '26 05:01

ashiaka


1 Answers

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

like image 73
adneal Avatar answered Jan 20 '26 21:01

adneal