title: Custom DatePicker Button. Custom Components in Android
body: <p>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: </p>
  
  <pre><code>error! ClassNotFoundException: android.app.DatePickerDialog$OnDateSetListener
  </code></pre>
  
  <p>Here is what I want the button to look at (very simple)</p>
  
  <p><img src="https://i.stack.imgur.com/EtfHJ.png" alt="enter image description here"></p>
  
  <p>and when it is clicked it will pop up the built in android datepicker dialog:</p>
  
  <p><img src="https://i.stack.imgur.com/dupHR.png" alt="enter image description here"></p>
  
  <p>Here is my current code for this custom datebutton:</p>
  
  <pre><code>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 &gt;= 10)
          return String.valueOf(c);
      else
          return "0" + String.valueOf(c);
     }
      }
  </code></pre>
  
  <p>EDIT: I'm still running into trouble anyone have any examples of something similar to this?
  Thanks for any help!</p>