How to Change the Datepicker style for Android

主宰稳场 提交于 2019-12-12 08:48:46

问题


I'm currently making a registration form, and one of the fields is for the users Date of Birth. I want to use datepicker, however I don't want the calendar layout as shown below:

I want the layout to look something like this so that way its easier to choose the year and month without having to scroll through everything:

However I do not know how to go about this, or what to put inside the styles.xml file. Any help would be greatly appreciated.


回答1:


Add this to style.xml

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

And then you should apply style for your date picker like this:

<DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        style="@style/date_picker_theme" />



回答2:


Create style with

<style name="DatePicker" parent="@android:style/Theme.Holo.Light">
    <item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>
</style>

Then in custom fragment dialog xml

<DatePicker
    android:theme="@style/DatePicker"
    android:id="@+id/fragment_date_picker_control"
    android:datePickerMode="spinner"
    android:spinnersShown="true"
    android:calendarViewShown="false"
    android:layout_marginLeft="@dimen/margin_normal"
    android:layout_marginRight="@dimen/margin_normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />

Link : http://yogeshkalwar.blogspot.in/2017/08/custom-spinner-datepicker-supporting-n70.html




回答3:


To set the DatePicker style globally, add the following to style.xml, or adjust existing content:

<resources>
  <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
  </style>
  ...
</resources>

And define your customized DatePicker style, either in style.xml or a separate file, e.g.:

<resources>
  <style name="MyDatePicker" parent="@android:style/Widget.DatePicker">
    <item name="android:calendarViewShown">false</item>
    <item name="android:datePickerMode">spinner</item>
  </style>
</resources>



回答4:


Above properties are of XML, if you want to set it programmatically you can you the below code but I haven't checked this code whether its working or not.

datepickerDialog_object.getDatePicker().setSpinnersShown(true);
datepickerDialog_object.getDatePicker().setCalendarViewShown(false);



回答5:


DatePickerDialog has a convenience method, where you set the desired layout.

public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId,
        @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
    this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth);
}


public static final int MODE_SPINNER = 1;
public static final int MODE_CALENDAR = 2;

Passing 1 or 2 in themesResId does the trick.




回答6:


It might be too late but you can set it programmatically like these.
Apply it inside your onClick listener.

        DatePickerDialog datePicker = new DatePickerDialog(
                this,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                datePickerListener,
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        datePicker.setCancelable(false);
        datePicker.setTitle("Select the date");
        datePicker.show();

The android.R.style.Theme_Holo_Light_Dialog_MinWidth create style that you want.
Hope it helps someone.




回答7:


hi try this a simplest way...

public void datePicker(final Context context, final EditText editText, final String type) {
        Calendar calendar = Calendar.getInstance();
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                newDate.set(year, monthOfYear, dayOfMonth);
                editText.setText(dateFormatter.format(newDate.getTime())

            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();
    }

android in style.xml add below line

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


来源:https://stackoverflow.com/questions/39429946/how-to-change-the-datepicker-style-for-android

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