Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change DatePicker background color

I am trying to display a DatePicker dialog on top of another activity and what is happening is it is somehow inheriting its color.

I'd like it to have a green header and white background, like on this example

Here is excerpt from styles

<style name="DatePickerDialog" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorAccent">@color/primary</item>
</style>

And this code is used to pop up the DatePicker

    DatePickerDialog datepicker = new DatePickerDialog(this, R.style.DatePickerDialog, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            TextView newdate = (TextView) findViewById(R.id.newdate);
            Date date = getDate(year, monthOfYear, dayOfMonth);
            DateFormat dateformat = new SimpleDateFormat(getResources().getString(R.string.date_format_full));
            newdate.setText(dateformat.format(date));
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    datepicker.show();

However it instead is displayed in all green

If I specify the white background in the styles, it overrides the header and the buttons as well

    <item name="android:background">@color/app_background</item>

One last thing I've tried is to use AlertDialog.THEME_DEVICE_DEFAULT_DARK as the DatePicker theme

DatePickerDialog datepicker = new DatePickerDialog(this, 
AlertDialog.THEME_DEVICE_DEFAULT_DARK, new 
DatePickerDialog.OnDateSetListener() 

But it'd still display the whole dialog in green

Here is the style of the activity I am opening the dialog from

<style name="UserDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:background">@color/primary</item>
    <item name="android:textColor">@color/dialog_text</item>
    <item name="colorPrimary">@color/app_background</item>
    <item name="android:windowTitleStyle">@style/NewDialogTitle</item>
</style>

<style name="NewDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
    <item name="android:gravity">center_horizontal</item>
</style>

And the colors I am using

<color name="primary">#4CAF50</color>
<color name="app_background">#FFFFFF</color>

Does anyone know how to get it done? I'd appreciate any guidance. I've tried to follow this answer, but had no luck

like image 278
rianor Avatar asked Jan 20 '18 17:01

rianor


People also ask

What is DatePicker in JavaScript?

The JavaScript DatePicker (Calendar Picker) is a lightweight and mobile-friendly control that allows end users to enter or select a date value. It has month, year, and decade view options to quickly navigate to the desired date.

How do I add a date picker to a form?

In this article, we will learn how to add a datepicker in form using HTML5. Approach: Create an HTML document that contains a form with an input field. Using a type attribute in input element which is set to “datetime-local“.

How do I change the color of my date picker flutter?

Here's how you do it: Step 1: Inside the showDatePicker function, add the builder paramter. Step 2: Inside the builder parameter, return the Theme widget. Step 3: Inside the Theme widget, add the data property and define the new theme by specifying the colorScheme for the date picker dialog.

How do I change the color of my DatePicker?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Kindly fine the highlighted code, this is the simplest way to change the colour of your datePicker.


1 Answers

This code worked for me try this ...

styles.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@android:color/holo_green_dark</item>
</style>

Popup Code

        Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);
        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker;
        mDatePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {

                Toast.makeText(context,"Selected Date " + + selectedday + "-" + ++selectedmonth  + "-" + selectedyear ,Toast.LENGTH_SHORT).show();
            }
        }, mYear, mMonth, mDay);
        mDatePicker.show();

enter image description here

like image 85
Naveen Dew Avatar answered Oct 16 '22 06:10

Naveen Dew