Monday, May 15, 2017

Android Material Date picker Dialog

Welcome to this post.

In this post, we are going to create a material date picker.

Date picker Dialog helps you to pick the date for your application.
If you have an option for user pick their date then you can use it. This popup in your layout. SO it's won't take place on your layout. In this post, we learn about how we can use Date picker Dialog and pick our date.

Ok, let's started.
First, you need to create a class name here DatePickerFragment and extend to DialogFragment and implement DatePickerDialog.OnDateSetListener

Let me show-
public class DatePickerFragment extends DialogFragment implements
  DatePickerDialog.OnDateSetListener {

}

now override two methods named onCreateDialog and onDateSet. in onCreateDialog, we add a calendar and get current date from our calendar and return those values-

@Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

  Calendar c = Calendar.getInstance();

  int startYear = c.get(Calendar.YEAR);
  startMonth = c.get(Calendar.MONTH);
  int startDay = c.get(Calendar.DAY_OF_MONTH);

  return new DatePickerDialog(getActivity(),this,startYear,startMonth,startDay);
  }

Note: in this methods, you can set you desire date if you want to show to your user. For this, you can first set date on the calendar the before getting data.

A quick look-
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
  try {
  String dateStr = "1/5/2017";
  Date date = format.parse(dateStr);
  c.setTime(date);
  } catch (ParseException e) {
  e.printStackTrace();
  }

Note: here I formatted a string into date and set it to the calendar. But you can add directly like this way-

Calendar calendar =Calendar.getInstance();
        
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 17);

when user set the date then onDateSet will be executed. the code is your wish. you get all in an integer. here I save it on shared preference.

@Override
  public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

  String dateStr = dayOfMonth+"/"+month+"/"+year;
  SharedPreferences preferences = getActivity().getSharedPreferences("TimeDate", Context.MODE_PRIVATE);
  
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("Date",dateStr);
  editor.apply();

  }

Ok. we created our DatepickerDialog.java class. Now we use it on on button listener.

dateTvLabel.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  DatePickerFragment datePickerFragment = new DatePickerFragment();
  datePickerFragment.show(getFragmentManager(), "Date Picker");
  }
  });

and That's it. Now you can use this Date Picker Dialog and customize your project.

datePicker


 Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :