Skip to content

Instantly share code, notes, and snippets.

@gvr23
Created January 18, 2018 22:51
Show Gist options
  • Save gvr23/adf45b9dea0f706fb42fa7ea41ebc0a2 to your computer and use it in GitHub Desktop.
Save gvr23/adf45b9dea0f706fb42fa7ea41ebc0a2 to your computer and use it in GitHub Desktop.
date picker pop up and implementation
ANDROID
==================================================================================================================================
package dsbmobile.com.drokasa.ui.dialog;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import java.util.Calendar;
import java.util.Date;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private int idPicker;
IDialogAceptar mListener;
public void setListener(IDialogAceptar mListener) {
this.mListener = mListener;
}
public interface IDialogAceptar {
void dialog_aceptar(Date date);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
mListener.dialog_aceptar(calendar.getTime());
}
@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);
return new android.app.DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
}
}
================================================================================================================================
IMPLEMENTATION
================================================================================================================================
private Date currentDate;
private SimpleDateFormat dateFormat;
private DatePickerFragment dateFragment;
if (dateFragment == null){
dateFragment = new DatePickerFragment();
dateFragment.setListener(PaymentOptionActivity.this);
dateFragment.show(getFragmentManager(), "datePicker");
}
@Override
public void dialog_aceptar(Date date) {
currentDate = date;
edtDate.setText(dateFormat.format(currentDate));
}
dateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
currentDate = dateFormat.parse(Util.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment