Iam using DatePickerDialog in one of my activity and its working fine but now I have to implement it in many activities so I thought to put it in a helper class and extend that function to all activities as good code practice.I have created an interface but couldn't able to use it properly.I have seen another link on stackoverflow but over there they are extending from one class to another class not activity.What I want is I have a helper class where I have put multiple function which Iam using repeatedly in my app and then extend them to any activity according to requirement.So I want to put this datepicker function in HelperClass then then get this function any where in app.Thanku
This is my datePicker function which is currently Iam using in an activity and its working fine
public void datePicker(int day,int month,int year) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
final Calendar myCalendar = Calendar.getInstance();
year = myCalendar.get(Calendar.YEAR);
month = myCalendar.get(Calendar.MONTH);
day = myCalendar.get(Calendar.DAY_OF_MONTH);
}
picker = new DatePickerDialog(RequestTraining.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}, year, month, day);
picker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
picker.show();
}
This is the interface I made
public interface DatePickerListener {
public void onDate(DatePicker view, int year, int monthOfYear, int dayOfMonth);
}
I think, the best approach will be like this
class Utils {
public void datePicker(Context mContext, EditText et) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
final Calendar myCalendar = Calendar.getInstance();
year = myCalendar.get(Calendar.YEAR);
month = myCalendar.get(Calendar.MONTH);
day = myCalendar.get(Calendar.DAY_OF_MONTH);
}
picker = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
et.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, year, month, day);
picker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
picker.show();
}
}
And now just call this method from any class like
Utils.datePicker(this, editText);