Datepicker: How to popup datepicker when click on button and store value in textview

23,126

here is the Sample code set onclicklistener on your view like this

 Calendar dateSelected = Calendar.getInstance();
  private DatePickerDialog datePickerDialog;
dateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           setDateTimeField() ;
        }
    });


private void setDateTimeField() {
    Calendar newCalendar = dateSelected;
    datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            dateSelected.set(year, monthOfYear, dayOfMonth, 0, 0);
            dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
        }

    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
}

or you can follow this link

Datepicker: How to popup datepicker when click on edittext

Share:
23,126
hashim
Author by

hashim

Updated on July 09, 2022

Comments

  • hashim
    hashim almost 2 years
     <TextView
                android:id="@+id/tv_arrival"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="@string/arrivaldate"/>
    
            <Button
                android:id="@+id/btn_arrival"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Set"/>
    

    I want to show datepicker popup window. I have found some examples but i am not getting it properly. I have one button and i want that when i click on button the datepicker dialog should popup and after setting the date, the date should be display in a . PLease provide me sample code or good links.

     btn_arrival.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    final Dialog setDate = new Dialog(MainActivity.this);
                    setDate.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    setDate.setContentView(R.layout.dialog_setdate);
                    setDate.show();
                    DatePicker date = (DatePicker) setDate.findViewById(R.id.datePicker);
                    date.setMinDate(System.currentTimeMillis() - 1000);
                    Calendar calender = Calendar.getInstance();
    
                    DatePicker.OnDateChangedListener onDateChangedListener = new DatePicker.OnDateChangedListener() {
                        @Override
                        public void onDateChanged(DatePicker view, int years, int monthOfYear, int dayOfMonth) {
                            day = dayOfMonth;
                            year = years;
                            month = monthOfYear;
                            setDate.dismiss();
                            txt_arrival.setText("Arrival Date on: " + day + "." + (month + 1) + "." + year);
                            tv_departure.setText("Departure Date on: " + (day + 1) + "." + (month + 1) + "." + year + "");
                        }
                    };
    
                    day = calender.get(Calendar.DAY_OF_MONTH);
                    month = calender.get(Calendar.MONTH);
                    year = calender.get(Calendar.YEAR);
    
                    date.init(year, month, day, onDateChangedListener);
    
                }
    
            });