Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show DatePickerDialog on Button click? [closed]

I'm developing an app that requires user to select date(dd/mm/yyyy). I want to show a dialog box with DatePicker on button click. once date is selected it must show in EditText.

I'm using Android Studio 2.2, project with min sdk is 23.

Kindly help me to do required code.

like image 342
Pratik Pitale Avatar asked Oct 07 '16 11:10

Pratik Pitale


People also ask

What is picker view in Android?

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year).

What is ui view controller that can be used to select a date and Time for an Android mobile app?

Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components. In this tutorial, we are going to demonstrate the use of Date Picker through DatePickerDialog.


3 Answers

enter image description here

I. In your build.gradle add latest appcompat library, at the time 24.2.1

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' 
    // where X.X.X version
}

II. Make your activity extend android.support.v7.app.AppCompatActivity and implement the DatePickerDialog.OnDateSetListener interface.

public class MainActivity extends AppCompatActivity  
    implements DatePickerDialog.OnDateSetListener {

III. Create your DatePickerDialog setting a context, the implementation of the listener and the start year, month and day of the date picker.

DatePickerDialog datePickerDialog = new DatePickerDialog(  
    context, MainActivity.this, startYear, starthMonth, startDay);

IV. Show your dialog on the click event listener of your button

((Button) findViewById(R.id.myButton))
    .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        datePickerDialog.show();
    }
});
like image 176
saulmm Avatar answered Oct 15 '22 11:10

saulmm


    final Calendar newCalendar = Calendar.getInstance();
    final DatePickerDialog  StartTime = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    activitydate.setText(dateFormatter.format(newDate.getTime()));
                }
    
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    
      btn_checkin.setOnClickListener(new View.OnClickListener() {
@Override   public void onClick(View v) {
         StartTime.show():    
     });
like image 22
M.Yogeshwaran Avatar answered Oct 15 '22 09:10

M.Yogeshwaran


it works for me. if you want to enable future time for choose, you have to delete maximum date. You need to to do like followings.

 btnDate.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          DialogFragment newFragment = new DatePickerFragment();
              newFragment.show(getSupportFragmentManager(), "datePicker");
          }
     });
            
     public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
              final Calendar c = Calendar.getInstance();
              int year = c.get(Calendar.YEAR);
              int month = c.get(Calendar.MONTH);
              int day = c.get(Calendar.DAY_OF_MONTH);
              DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
              dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
              return  dialog;
         }
            
         public void onDateSet(DatePicker view, int year, int month, int day) {
              btnDate.setText(ConverterDate.ConvertDate(year, month + 1, day));
         }
     }
 }
like image 18
Kayra Avatar answered Oct 15 '22 10:10

Kayra