Question Detail

I want to create a EditView on Click event of EditView Raise or Popup DatePicker in Android.
 

 

<EditText
    style="@style/textStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/divider_margin"
    android:background="@drawable/edittext_bg"
    android:hint="Email ID *"
    android:inputType="phone"
    android:padding="@dimen/divider_margin"
    android:textColor="@color/tabBack_Black"/>


Thread Reply

alex levine

- 1 months ago

You have to use DatePickerDialog class in your DateDialog customized class to show dialog picker when you click on your EditText you don't need to do with your Edittext after click and setValue.

here is your reusable class to show and select DatePickerDialog

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateDialog {
    private final Calendar cal = Calendar.getInstance();
    private final String dateFormat = "dd/MM/yyyy";
    DataClass dataClass;

    public DateDialog(final Context context, DataClass _dataClass) {
        this.dataClass = _dataClass;
        showDateDialog(context).show();
    }
    private DatePickerDialog.OnDateSetListener onDateSelectListener() {
        return new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int y, int m, int d) {
                cal.set(Calendar.YEAR, y);
                cal.set(Calendar.MONTH, m);
                cal.set(Calendar.DAY_OF_MONTH, d);
                SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
                dataClass.onDone(sdf.format(cal.getTime()));
            }
        };
    }
    private DatePickerDialog showDateDialog(Context context) {
        return new DatePickerDialog(context, onDateSelectListener(), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    }
}

 

Create an abstract class to set selected value to your Edittext

public abstract class DataClass {
    public abstract void onDone(String value);

}

Here is your final code snippet to set a selected date to your Edittext

editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new DateDialog(mActivity, new DataClass() {
            @Override
            public void onDone(String value) {
                editText.setText(value);
            }
        });
    }
});