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.

6 comments:

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

    ReplyDelete
  2. It is really a great work and the way in which you are sharing the knowledge is excellent.Thanks for your informative article

    software testing course in chennai

    ReplyDelete
  3. I have used calender for date of birth but it also take date of future how to custmize this my before the cuurrent date and It will not taking future date.

    ReplyDelete
  4. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... Click her

    ReplyDelete
  5. This short description of CTFO gives you some idea of what the company does: ---
    Changing The Future Outcome also known as CTFO is a reputable (MLM) multi level marketing business in the CBD Oil niche. The primary reason behind their fast growth is that they provide some of the best CBD products in the marketplace. They also provide the products at a low cost for their associates and consumers. Considering that the CBD niche is growing faster and faster every year, it's easy to see why the CTFO network marketing company is growing so fast.cbd oil

    ReplyDelete
  6. Online you can see lots of wooden sunglasses variety. All glasses comes in a market with unique design. You can wear on any occassion.homescapes cheats

    ReplyDelete