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

爷,独闯天下 提交于 2019-11-30 03:23:08

问题


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.

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:

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


回答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




回答2:


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




回答3:


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.




回答4:


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



回答5:


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

        }
    }



回答6:


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.



来源:https://stackoverflow.com/questions/12711212/how-to-change-android-datepicker-dialogs-dividers-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!