Reputation: 39
I have searched in many sites for date picker in android, but all sites are providing code which contains showdialog()
, and eclipse is mentioning that showDialog
is deprecated. So anyone please help me with basic date picker. I'm new to android developing, so please be tolerant. I tried from every sites.
Need to place the basic date picker in one xml. (I don't want to include time and all, date alone will do). Thanks in Advance
this is my class file
public class PremiumCurator extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.premiumcurator);
Button btnDate = (Button)findViewById(R.id.btnDate);
btnDate.setOnClickListener(new OnClickListener()
{
@SuppressLint("NewApi")
public void onClick(View v)
{
daatepopup();
}
});
}
public void daatepopup()
{
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
OnDateSetListener mDateSetListener = null;
//updateDisplay();
DatePickerDialog d = new DatePickerDialog(this, mDateSetListener,mYear, mMonth, mDay);
d.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
from the reference of yours above is my code, date picker is populated i can able to choose the date. But whenever i give set i need to save my date values. How to fire the event when i click the set button of the date picker ? please see my code and help me out where to add.
Upvotes: 1
Views: 1729
Reputation: 18112
I am using this code, and its working fine...
Time currDate = new Time(Time.getCurrentTimezone());
currDate.setToNow();
DatePickerDialog d = new DatePickerDialog(this, datePickerListener,
currDate.year, currDate.month, currDate.monthDay);
d.show();
private DatePickerDialog.OnDateSetListener datePickerListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
{
// Do as you need
}
};
Upvotes: 2