Reputation: 977
I have a date in my button (5-12-2031) when i click the button i want to show date picker with date what i have in my button. In Stackoverflow i found to set the time picker
Android: Setting time in time picker with the time shown in text view i tried the same but the date picker not shown.
My code is:
Calendar mDateCalender = Calendar.getInstance();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_details_main_layout);
date.setOnClickListener(new onClickListener) {
public void onClick(){
new DatePickerDialog(AddDetailsActivity.this, onDateListener,
mDateCalender.get(Calendar.YEAR), mDateCalender.get(Calendar.MONTH),
mDateCalender.get(Calendar.DAY_OF_MONTH)).show();
}
}
}
DatePickerDialog.OnDateSetListener onDateListener = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
String dateFormat = mOfflineDatas.get(0).getOnsket_utfort();
String[] getDates = dateFormat.split("-");
Toast.makeText(getApplicationContext(), "" + dateFormat, Toast.LENGTH_LONG).show();
mDateCalender.set(Calendar.DAY_OF_MONTH, Integer.valueOf(getDates[0]));
mDateCalender.set(Calendar.MONTH, Integer.valueOf(getDates[1]));
mDateCalender.set(Calendar.YEAR, Integer.valueOf(getDates[2]));
}
};
my dateFromat = 5-12-2013 it splits correctly.
What change i need to change in this anybody suggest some ideas..
Upvotes: 1
Views: 7551
Reputation: 24853
Try this..
Global :
static final int DATE_DIALOG_ID = 999;
private int myear;
private int mmonth;
private int mday;
OnCreate :
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String[] str = btn.getText().toString().trim().split("-");
mday = Integer.parseInt(str[0]);
mmonth = Integer.parseInt(str[1]);
myear = Integer.parseInt(str[2]);
showDialog(DATE_DIALOG_ID);
}
});
Method outside OnCreate() :
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday){
};
return _date;
}
return null;
}
EDIT 1:
date.setOnClickListener(new onClickListener) {
public void onClick(){
String[] str = date.getText().toString().trim().split("-");
mday = Integer.parseInt(str[0]);
mmonth = Integer.parseInt(str[1]);
myear = Integer.parseInt(str[2]);
new DatePickerDialog(AddDetailsActivity.this, onDateListener,
myear, mmonth,
mday).show();
}
}
Upvotes: 2
Reputation: 581
If Your Using Fragments this code may help you .
mLesson_Date = (TextView) rootView.findViewById(R.id.Lesson_Date);
--------------------------------------------------------------------
mLesson_DateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment1 = new DatePickerFragment(
mLesson_Date);// Here you are passing the TextView Object to DatePickerFragment by calling the constructor
newFragment1.show(getFragmentManager(), "datePicker");
}
});
--------------------------------------------------------------------
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
TextView sample;
public DatePickerFragment() {
// TODO Auto-generated constructor stub.
}
public DatePickerFragment(TextView setTimeView) {
// TODO Auto-generated constructor stub.
this.sample = setTimeView;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// return new DatePickerDialog(getActivity(),
// (EditSessionActivity)getActivity(), year, month, day);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
sample.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}
Upvotes: 0
Reputation: 11131
this may help you...
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(yourButton.getText().toString());
}
});
private void showDatePickerDialog(String date) {
// here date is 5-12-2013
String[] split = date.split("-");
int day = Integer.valueOf(split[0]);
int month = Integer.valueOf(split[1]);
int year = Integer.valueOf(split[2]);
OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
dateSetListener, year, month, day);
datePickerDialog.show();
}
Upvotes: 1
Reputation: 4382
There's the DatePicker.init where you can set it...
Or to set the date you can use the calendar control as shown in the android developer sample. See Steps 4-6.
Here's another sample blog post on this.
or you can use following on button click event
Calendar cal=Calendar.getInstance(Locale.ENGLISH);
dp.init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);
Upvotes: 0