I want to show datepicker popup window. I have found some examples but i am not getting it properly. I have one edittext and i want that when i click on edittext the datepicker dialog should popup and after setting the date, the date should show in edittext in dd/mm/yyyy format. PLease provide me sample code or good links.
Step 1. Crate a layout activity_main.xml
Step 1. Crate a layout activity_main.xml
<EditTextStep 2. Create a class activity_main.java
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="true"
android:editable="false"
android:hint="@string/birthday"/>
final Calendar myCalendar = Calendar.getInstance();Step 3. you create event click edittext popup datepicker in android
EditText edittext= (EditText) findViewById(R.id.Birthday);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
edittext.setOnClickListener(new OnClickListener() {Step 4. Now add the method in the above activity.
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(classname.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
private void updateLabel() {Note: Add android:focusable="false" within the xml file of the EditText to allow for a single touch.
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}