I am trying to create a custom date picker button component. The button displays the date and when clicked it opens the date picker dialog box. I have implemented this method using the Android Dev Docs/Tutorial however I have 2 of them in my code and I figured it would be better to create a custom class called DateButton which would clean up my code a bit. So whenever I need a similar button I can create a declare a DateButton in my XML. Anyways this is my first time creating a custom view and I need some help whenever I try to add my custom button to my layout I get an error:
error! ClassNotFoundException: android.app.DatePickerDialog$OnDateSetListener
Here is what I want the button to look at (very simple)
and when it is clicked it will pop up the built in android datepicker dialog:
Here is my current code for this custom datebutton:
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class DateButton extends Button implements OnClickListener, OnDateSetListener {
private static final int DATE_DIALOG_ID = 0;
private int mYear;
private int mMonth;
private int mDay;
private OnDateSetListener mDateSetListener;
public DateButton(Context context) {
super(context);
}
public DateButton(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public DateButton(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
private DatePickerDialog showDialog(int dateDialogId) {
return new DatePickerDialog(getContext(),
mDateSetListener,
mYear, mMonth, mDay);
}
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
private void updateDisplay() {
this.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pad(mMonth + 1)).append("/")
.append(pad(mDay)).append("/")
.append(mYear).append(" "));
}
//if single digit append "0" to the number
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
EDIT: I'm still running into trouble anyone have any examples of something similar to this? Thanks for any help!