I’m trying to get selected dates using two date and time picker (like fromdate and To date). If I select fromdate I can get the fromdate to the textview. But if I select second datepicker (todate) update same textview (Fromdate textview).
public class F2Activity extends InfosoftActivity
{
private int mYear;
private int mMonth;
private int mDay;
private int mYear2;
private int mMonth2;
private int mDay2;
private TextView mDateDisplay;
private TextView mDateDisplay2;
private Button mPickDate;
private Button mPickDate2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_IDD = 0;
public String AAA;
public String BBB;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black);
setContentView(R.layout.activity_f2);
Button BtnView=(Button) findViewById(R.id.BtnView);
Button BtnDownload=(Button) findViewById(R.id.BtnDownload);
mDateDisplay = (TextView) findViewById(R.id.showMyDate);
mDateDisplay2 = (TextView) findViewById(R.id.showMyDatee);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate2 = (Button) findViewById(R.id.myDatePickerButton2);
mPickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
mPickDate2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(DATE_DIALOG_IDD);
}
});
// get the current date
final Calendar c2 = Calendar.getInstance();
mYear2 = c2.get(Calendar.YEAR);
mMonth2 = c2.get(Calendar.MONTH);
mDay2 = c2.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplayTo();
private void updateDisplay()
{
this.mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private void updateDisplayTo()
{
this.mDateDisplay2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth2 + 1).append("-")
.append(mDay2).append("-")
.append(mYear2).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new
DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener2 =new
DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int
monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear2 = year;
mMonth2 = monthOfYear;
mDay2 = dayOfMonth;
updateDisplayTo();
}
};
protected Dialog onCreateDialog2 (int id)
{
switch (id)
{
case DATE_DIALOG_IDD:
return new DatePickerDialog(this,
mDateSetListener2,
mYear2, mMonth2, mDay2);
}
return null;
}
}
If you want to display more than one time of date using DatePicker in your application then no need to use two time DATE_DIALOG_ID. You can use only one and fulfill your requirements. I posted here code I hope it will helps you.
In button click
Then
Then
Then
I hope it will solve your problem. If you have any problem regarding this then you can ask me.