Reputation: 258
Here is the source for selecting the date. It is working properly but I want to change date when selecting date again.
private int year;
private int month;
private int day;
signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);
signup_bday.setOnClickListener(new 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);
DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
{
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
signup_bday.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));
}
},year, month, day);
mDatePicker.setTitle("Please select date");
mDatePicker.show();
}
});
Upvotes: 1
Views: 1860
Reputation: 6738
I think your onDateSet
method not getting called. Add @override
annotation and try following code.
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
}
What I understood.. First time it displays current date. If user select 5,Jan 2014 then second time it'll display previously selected data(5, Jan 2014). If you are trying to achieve this then try something like this
final Calendar cal = Calendar.getInstance();
try {
cal.setTime(new Date(PREVIOUSLY_SELECTED_DATE));
} catch (IllegalArgumentException e) {
}
Upvotes: 1
Reputation: 348
Ok, I solved your problem, Here is the code,
public class MainActivity extends Activity
{
private int year;
private int month;
private int day;
private EditText signup_bday;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);
signup_bday.setOnClickListener(new 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);
DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, new OnDateSetListener()
{
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
year = selectedyear;
month = selectedmonth;
day = selectedday;
signup_bday.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));
}
},year, month, day);
mDatePicker.setTitle("Please select date");
mDatePicker.show();
}
});
}
}
You just need to add following lines in onDateSet() method,
year = selectedyear;
month = selectedmonth;
day = selectedday;
Upvotes: 2