How to change Android DatePicker Dialog's “Dividers” Colors

前端 未结 6 1391
無奈伤痛
無奈伤痛 2021-01-04 18:50

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.

相关标签:
6条回答
  • 2021-01-04 19:13

    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

    0 讨论(0)
  • 2021-01-04 19:13

    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

    0 讨论(0)
  • 2021-01-04 19:22
         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)
                }
    
            }
        }
    
    0 讨论(0)
  • 2021-01-04 19:30

    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));
    
    0 讨论(0)
  • 2021-01-04 19:32

    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.

    0 讨论(0)
  • 2021-01-04 19:32

    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.

    0 讨论(0)
提交回复
热议问题