10

I'm trying to create a custom dialog, basically I'm using DatePickerDialog.THEME_HOLO_DARK but I want to change the "divider" color and the text color.

enter image description here

I want to change the blue lines and the text color to red.

Thanks in advance!

EDIT:

Using this code:

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <item name="android:divider">@drawable/dialog_divider</item>
</style>

This is what I get: enter image description here

The drawable for the divider is basically a red line..

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Robin.v
  • 719
  • 3
  • 8
  • 16

6 Answers6

1

You will have to create your own theme that extends THEME_HOLO_DARK. Change basic theme color of android application

Edit: Try something like this

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <item name="android:divider">@drawable/MyDivider</item>
</style>

And pass the reference to your theme to the constructor

Community
  • 1
  • 1
ahodder
  • 11,353
  • 14
  • 71
  • 114
  • I can't extend THEME_HOLO_DARK is it the same as Theme.black? And if I try to set android:_DEFAULT_BASE_COLOR_1 i get this error: error: Error: No resource found that matches the given name: attr 'android:_DEFAULT_BASE_COLOR_1'. – Robin.v Oct 03 '12 at 15:47
  • You should be able to extend THEME_HOLO_DARK. Did you define _DEFAULT_BASE_COLOR_1 in your resources? – ahodder Oct 03 '12 at 15:48
  • This is what i have now: how can i define that in my resources? – Robin.v Oct 03 '12 at 15:50
  • I've edited my question. I believe you've found the right theme to extend, now I have to figure out which attributes to set, and how to set them properly. Thanks for your answer! – Robin.v Oct 05 '12 at 08:33
  • @KarimVarela The question is tagged Android 4.0 which is API 14. – ahodder Apr 19 '13 at 00:38
  • @Robin.v In-detail analysis here: [Android: how to change the color of the datepicker divider?](http://stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider). – user3264740 Feb 14 '14 at 21:51
1

add this item to your main theme

<item name="numberPickerStyle">@style/MyApp.NumberPicker</item>

set custom divider color in this style

<style name="MyApp.NumberPicker" parent="Widget.Holo.NumberPicker">
   <item name="selectionDivider">@color/white</item>
</style>

EDIT : tested only on HoloEverywhere

Oleg Koshkin
  • 3,483
  • 2
  • 15
  • 6
0

I know it's a late reply but I would like to share my findings with those who experienced or will experience this problem in the future.

I searched many places, and the only solution I found was SimonVT's Pickers.

After implementing the library, it was very easy to change the colors. In numberpicker- the library uses images that can be simply replaced with the desired designs.

This example has three pickers: NumberPicker, DatePicker and TimerPicker.

https://github.com/rodrigobusata/android-pickers

I hope you find it useful.


UPDATED

The above link do not existe anymore, now you must use Material Design https://github.com/wdullaer/MaterialDateTimePicker.

Busata
  • 1,029
  • 1
  • 13
  • 27
0

Please use the below code

Here, dialog is an object of DatePickerDialog.

    int dividerId = dialog.getContext().getResources().getIdentifier
            ("android:id/titleDivider", null, null);
    View divider = dialog.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.red));

    int textViewId = dialog.getContext().getResources().getIdentifier
            ("android:id/alertTitle", null, null);
    TextView tv = (TextView) dialog.findViewById(textViewId);
    tv.setTextColor(getResources().getColor(R.color.red));
miteshpithadiya
  • 214
  • 1
  • 10
0
     fun applyStyLing(timePickerDialog: DatePickerDialog,context: Context) {

        try {
            var dividerId = timePickerDialog.getContext().getResources().getIdentifier("titleDivider", "id", "android")
            val divider = timePickerDialog.findViewById<View>(dividerId)
            divider.setBackgroundColor(ContextCompat.getColor(context, R.color.colorDatePicker))

            var dividerId1 = timePickerDialog.getContext().getResources().getIdentifier("alertTitle", "id", "android")
            val divider1 = timePickerDialog.findViewById<TextView>(dividerId1)
            divider1.setTextColor(ContextCompat.getColor(context, R.color.colorDatePicker))

            val system = Resources.getSystem()
            val hourNumberPickerId = system.getIdentifier("day", "id", "android")
            val minuteNumberPickerId = system.getIdentifier("month", "id", "android")
            val ampmNumberPickerId = system.getIdentifier("year", "id", "android")

            val hourNumberPicker = timePickerDialog.findViewById<View>(hourNumberPickerId) as NumberPicker
            val minuteNumberPicker = timePickerDialog.findViewById<View>(minuteNumberPickerId) as NumberPicker
            val ampmNumberPicker = timePickerDialog.findViewById<View>(ampmNumberPickerId) as NumberPicker

            setNumberPickerDividerColour(hourNumberPicker,context)
            setNumberPickerDividerColour(minuteNumberPicker,context)
            setNumberPickerDividerColour(ampmNumberPicker,context)

        }catch (e:Exception)
        {
            e.printStackTrace()
        }

    }


private fun setNumberPickerDividerColour(number_picker: NumberPicker,context: Context) {
        val count = number_picker.childCount

        for (i in 0 until count) {

            try {
                val dividerField = number_picker.javaClass.getDeclaredField("mSelectionDivider")
                dividerField.isAccessible = true
                val colorDrawable = ColorDrawable(context.getResources().getColor(R.color.colorDatePicker))
                dividerField.set(number_picker, colorDrawable)

                number_picker.invalidate()
            } catch (e: NoSuchFieldException) {
                Log.w("setNumberPickerTxtClr", e)
            } catch (e: IllegalAccessException) {
                Log.w("setNumberPickerTxtClr", e)
            } catch (e: IllegalArgumentException) {
                Log.w("setNumberPickerTxtClr", e)
            }

        }
    }
Rambabu Padimi
  • 569
  • 5
  • 17
-2

you can change the text color by doing something like

<style name="testo" parent="@android:style/Widget.DeviceDefault.DatePicker">

<item name="android:textColor">#FF0000</item>

</style>

Did you ever figure out how to change the divider color? I'm trying to do the same thing and can't really find anything at the moment.

MoLo
  • 41
  • 3
  • In-detail analysis here: [Android: how to change the color of the datepicker divider?](http://stackoverflow.com/questions/20148671/android-how-to-change-the-color-of-the-datepicker-divider). – user3264740 Feb 14 '14 at 21:46
  • 1
    -1 Not only does this code not work, but it fails to answer the second half of the question (`divider` colors). Please keep your questions in comments, not answers. – adamdport Mar 12 '15 at 14:42