In my app I have one datepicker, I am able to select and set the selected date in textview, but the issue is if again I click on textview to open datepicker dialog, it always shows current date instead of last selected date..so what is the issue?
public class MainActivity extends Activity {
private TextView date_dropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date_dropdown=(TextView)findViewById(R.id.shows_dt);
Calendar calendar = Calendar.getInstance();
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
date_dropdown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
}
public void showDatePickerDialog() {
System.out.println("show date picke dilg ");
System.out.println("show date picke dilg");
DialogFragment newFragment1 = new SelectDateFragment();
newFragment1.show(getFragmentManager(), "DatePicker");
}
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
public void populateSetDate(int year, int month, int day) {
date_dropdown.setText(day + "-" + month + "-" + year);
}
}
Below Line You Have Given get only Current date, so You will Get current date only.
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
save your Selected date in Shared Preference then Show in your TextView
You should define calender instance in onCreate()
method and then all year month and date value update onDateSet()
, So when you again open date picker dialog it will surly display previously selected date.
Do just like this
public class MainActivity extends Activity {
private TextView date_dropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Calendar calendar = Calendar.getInstance();
int yy1 = calendar.get(Calendar.YEAR);
int mm1 = calendar.get(Calendar.MONTH);
int dd1 = calendar.get(Calendar.DAY_OF_MONTH);
date_dropdown=(TextView)findViewById(R.id.shows_dt);
Calendar calendar = Calendar.getInstance();
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
date_dropdown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
}
public void showDatePickerDialog() {
System.out.println("show date picke dilg ");
System.out.println("show date picke dilg");
DialogFragment newFragment1 = new SelectDateFragment();
newFragment1.show(getFragmentManager(), "DatePicker");
}
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), this, yy1, mm1, dd1);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
yy1 = yy ;
mm1 = mm;
dd1 = dd ;
}
public void populateSetDate(int year, int month, int day) {
date_dropdown.setText(day + "-" + month + "-" + year);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With