Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display year only in date picker in android

Tags:

android

Can anybody tell how to display year only in date picker in android

Thanks

like image 583
user386430 Avatar asked May 29 '12 06:05

user386430


1 Answers

First, create MonthYearPickerDialog.java class extends with DialogFragment

public class MonthYearPickerDialog extends DialogFragment {

private static final int MAX_YEAR = 2099;
private DatePickerDialog.OnDateSetListener listener;

public void setListener(DatePickerDialog.OnDateSetListener listener) {
    this.listener = listener;
}

@SuppressLint("ResourceAsColor")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
    LayoutInflater inflater = getActivity().getLayoutInflater();

    Calendar cal = Calendar.getInstance();

    View dialog = inflater.inflate(R.layout.month_year_picker_dialog, null);
    final NumberPicker monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
    final NumberPicker yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);

    monthPicker.setMinValue(1);
    monthPicker.setMaxValue(12);
    monthPicker.setValue(cal.get(Calendar.MONTH) + 1);

    int year = cal.get(Calendar.YEAR);
    yearPicker.setMinValue(1900);
    yearPicker.setMaxValue(3500);
    yearPicker.setValue(year);

    builder.setView(dialog).setPositiveButton(Html.fromHtml("<font color='#FF4081'>Ok</font>"), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            listener.onDateSet(null, yearPicker.getValue(), monthPicker.getValue(), 0);
        }
    }).setNegativeButton(Html.fromHtml("<font color='#FF4081'>Cancel</font>"), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MonthYearPickerDialog.this.getDialog().cancel();
        }
    });
    return builder.create();
   }
 }

in styles.xml paste this code.

  <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:textColor">#FF4081</item>  // you can change the color of the yearPiker dialog
    <item name="android:textColorPrimary">#FF4081</item> // you can change the color of the yearPiker dialog
  </style>

create month_year_picker_dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="220dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/colorAccent"
        android:gravity="top">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elevation="8dp"
            android:gravity="center"
            android:text="Year"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/picker_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:descendantFocusability="blocksDescendants"
            android:visibility="gone"
            tools:ignore="RtlCompat">

        </NumberPicker>

        <NumberPicker
            android:id="@+id/picker_year"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"
            >

        </NumberPicker>

      </LinearLayout>

   </LinearLayout>

</LinearLayout>

In your main class (activity)

 private DatePickerDialog createDialogWithoutDateField() {
    DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
    try {
        java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
        for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
            if (datePickerDialogField.getName().equals("mDatePicker")) {
                datePickerDialogField.setAccessible(true);
                DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
                java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
                for (java.lang.reflect.Field datePickerField : datePickerFields) {
                    Log.i("test", datePickerField.getName());
                    if ("mDaySpinner".equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = datePickerField.get(datePicker);
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        }
    }
    catch (Exception ex) {
    }
    return dpd;
}

Finally , call wherever you want

createDialogWithoutDateField().show();

enter image description here

enter image description here

like image 179
Agilanbu Avatar answered Sep 24 '22 07:09

Agilanbu