1

For my fragment class I need month and year picker, how can i change the following date picker code into month/year picker or please share any tutorial/example that working in fragment.

public class Month_Year_Picker extends Fragment{

  .............
        expDate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar c = Calendar.getInstance();
                DialogFragment newFragment = new DateFragment();
                newFragment.show(getChildFragmentManager(),
                        "DatePicker");
            }
        });
...........
@SuppressLint("ValidFragment")
class DateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }   


    @Override
    public void onDateSet(DatePicker arg0, int yy, int mm, int dd) {
        // TODO Auto-generated method stub
    }

}

}
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

2 Answers2

0

I solved using date picker like iPhone,and just hided date field so that I get Month and year picker.

Source:

http://sunilsuthar02.blogspot.in/2013/04/date-picker-like-iphone-in-android.html

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
0

I tried using the existing DatePicker in-order to pick month and year only. But its a very hacky approach and does not work well on all device.

Went ahead making a modified Date Picker called Simple Date picker ... have used the code similar to Date Picker just to show month and year

see https://github.com/resengupta/Month-Year-Date-Picker

SimpleDatePickerDialog.java class is responsible for showing the month and year number picker. SimpleDatePickerDelegate.java works to apply rules to the number pickers. SimpleDatePickerDialogFragment.java is a DialogFragment which wraps the alert dialog.

ReeSen
  • 75
  • 4