Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change TimePicker lines color in Appcompat theme?

How to change TimePicker lines color between choosed number in Appcompat theme? The lines are blue, but i need orange lines.

I use TimePickerDialog with ContextThemeWrapper.

   TimePickerDialog timePicker = new TimePickerDialog(
        new ContextThemeWrapper(getActivity(), R.style.timePicker), 
    this, hour, minute, DateFormat.is24HourFormat(getActivity()));

and style

<style name="timePicker">
    <item name="android:divider">@drawable/cab_background_top_play</item>
</style>

but the line have other id maybe. I'm not sure to use drawable or color only.

Thanks in advance

like image 862
Vanama Avatar asked Jul 07 '26 01:07

Vanama


1 Answers

I solved this with own Timepicker class which extends android Timepicker class. I inspired by post stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider/20291416#20291416 which posted mohammad rababah in comment below my ask.

Here is styled timepicker class:

public class StyledTimePicker extends TimePicker {

        public StyledTimePicker(Context context, AttributeSet attrs) {
            super(context, attrs);

            Class<?> internalRID = null;
            try {
                internalRID = Class.forName("com.android.internal.R$id");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Field month = null;
            try {
                month = internalRID.getField("hour");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npMonth = null;
            try {
                npMonth = (NumberPicker) findViewById(month.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Field day = null;
            try {
                day = internalRID.getField("minute");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npDay = null;
            try {
                npDay = (NumberPicker) findViewById(day.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Field year = null;
            try {
                year = internalRID.getField("amPm");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            NumberPicker npYear = null;
            try {
                npYear = (NumberPicker) findViewById(year.getInt(null));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Class<?> numberPickerClass = null;
            try {
                numberPickerClass = Class.forName("android.widget.NumberPicker");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Field selectionDivider = null;
            try {
                selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }

            try {
                selectionDivider.setAccessible(true);
                selectionDivider.set(npMonth, getResources().getDrawable(
                        R.color.apptheme_color));
                selectionDivider.set(npDay, getResources().getDrawable(
                        R.color.apptheme_color));
                selectionDivider.set(npYear, getResources().getDrawable(
                        R.color.apptheme_color));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
}

You can change color apptheme_color only.

Thanks to mohammad rababah

like image 166
Vanama Avatar answered Jul 08 '26 16:07

Vanama