Popup date picker or time picker dialog on click of edittext or any view in Android. - SpartaCode

SpartaCode

Make your development faster and easier !

Popup date picker or time picker dialog on click of edittext or any view in Android.




Simple Step by Step tutorial on adding DatePicker and TimePicker in Android :

Step 1 : Include Library in Apps "build.gradle"
compile 'com.wdullaer:materialdatetimepicker:3.0.0'


Step 2 : Declare and Initialize DatePicker and TimePicker :
        //Declaration of Datetimepicker and timepicker dialog
        TimePickerDialog tpd;
        DatePickerDialog dpd;
        Calendar now = Calendar.getInstance();
        //Definition of Datetimepicker and timepicker dialog place in onCreate()
        tpd = TimePickerDialog.newInstance(
                 new TimePickerListener(),
                now.get(Calendar.HOUR),
                now.get(Calendar.MINUTE),
                false
        );
        dpd = DatePickerDialog.newInstance(
                 new DatePickerListener(tv_event_date_from,tv_event_date_to),
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        );

       //To Open Datepicker dialog call this function as follows
       dpd.show(getFragmentManager(), "AnyTag");

       //To Open TimePicker dialog call this function as follows
       tpd.show(getFragmentManager(), "AnyTag");



Step 3 : Creating Listener
//Create a new Listener Class to manage clicks

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

public class DatePickerListener implements  DatePickerDialog.OnDateSetListener {

    TextView from,to;

    public DatePickerListener(TextView from, TextView to){
        this.from=from;
        this.to=to;
    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        String tag=view.getTag();
        
         if(tag.equals("from")){
            from.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
            OrderHistoryActivity.datefrom=year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
        }else{
            to.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
            OrderHistoryActivity.dateto=year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
        }
    }
}


//Create a new Listener Class to manage timepicker clicks

import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

public class TimePickerListener implements  TimePickerDialog.OnTimeSetListener {

    public TimePickerListener(){
    }

     @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
        String format;
        if (hourOfDay == 0) {

            hourOfDay += 12;

            format = "AM";
        }
        else if (hourOfDay == 12) {

            format = "PM";

        }
        else if (hourOfDay > 12) {

            hourOfDay -= 12;

            format = "PM";

        }
        else {
            format = "AM";
        }

        String time = hourOfDay+":"+minute+":0"+second+" ";
 
    }
}


Thank You for your Visit +1 and Comment down.

1 comment:

  1. Good and simple tutorial on Datepicker and timepicker dialog.

    ReplyDelete

News

Get Amazing Traffic to your Site

Traffic Exchange

Post Bottom Ad

Responsive Ads Here