Reputation: 1518
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);
}
}
Upvotes: 12
Views: 2816
Reputation: 1
A simple way to print selected date is:
picker.addOnPositiveButtonClickListener {
tv_date.text = "picker.headerText"
}
picker
is an object of DatePicker class.
Upvotes: 0
Reputation: 11254
updateDate through set previous selected date in date picker
mDatePicker.updateDate(1994, 6, 12);
mDatePicker.setTitle("Please select date");
mDatePicker.updateDate(1994, 6, 12);
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Upvotes: 0
Reputation: 11254
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
Upvotes: 0
Reputation: 11254
How to disable future date picker in android
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
Upvotes: 0
Reputation: 81
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);
}
}
Upvotes: 2
Reputation: 1879
It looks like you're creating a new dialog every time you click, which means onCreateDialog
is setting the date to the current time again. You want to have a class variable DialogFragment df
, and in your showDatePickerDialog
do something like this:
public void showDatePickerDialog() {
if (df == null){
// Only create a new fragment if one does not exist
DialogFragment df = new SelectDateFragment();
}
df.show(getFragmentManager(), "DatePicker");
}
You will also want to store the value the user selects in your onPause
method and set the DialogFragment
to that value in onResume
, in case your user navigates away from your app while inputting information.
Upvotes: 0
Reputation: 433
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
Upvotes: 2