title: How do I programmatically extract values from DatePicker/TimePicker Spinners in Android?
body: <p>I originally followed the tutorial on <a href="http://developer.android.com/guide/topics/ui/controls/pickers.html" rel="nofollow">android developers</a>, but since I plan on having 2 of each on the page, I figured it'd be a bit more elegant to pull the values from the pickers. I first used the onClick methods in the XML; however, now, I am attempting to pull the values once a button (btn_schedule_adventure) is pressed. I get an error when I try and set the DateView</p>
  
  <pre><code>DatePicker startDatePicker = (DatePicker) findViewById(R.id.picker_start_date);
  </code></pre>
  
  <p>The code I have below throws an error. How do I:
  1-Pull the values out of the date/time pickers (bypassing the error below)
  2-While maintaining the default times/dates</p>
  
  <p>Thanks in advance.</p>
  
  <p>Error:</p>
  
  <pre><code>05-26 04:27:44.084: E/AndroidRuntime(798): FATAL EXCEPTION: main
  05-26 04:27:44.084: E/AndroidRuntime(798): java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.DatePicker
  05-26 04:27:44.084: E/AndroidRuntime(798):  at com.scarr025.zwilio.AdventureSetUp$1.onClick(AdventureSetUp.java:40)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.view.View.performClick(View.java:4204)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.view.View$PerformClick.run(View.java:17355)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.os.Handler.handleCallback(Handler.java:725)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:92)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:137)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:5041)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:511)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  05-26 04:27:44.084: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Method</p>
  
  <pre><code>public class AdventureSetUp extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //Remove title bar
      //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      Log.d("$$LOGCHECK$$", "In onCreate");
  
      setContentView(R.layout.activity_adventure_set_up); 
  
      Button btnScheduleAdventure = (Button) findViewById(R.id.btn_schedule_adventure);
      btnScheduleAdventure.setOnClickListener(new View.OnClickListener() {
                      //perform your action here
          public void onClick(View v) {
              // Get adventure title
              EditText btnScheduleAdventure = (EditText) findViewById(R.id.adventure_title);
              String adventureTitle = btnScheduleAdventure.getText().toString();
              // Get adventure start date
              DatePicker startDatePicker = (DatePicker) findViewById(R.id.picker_start_date);
              int startDay = startDatePicker.getDayOfMonth();
              int startMonth = startDatePicker.getMonth();
              int startYear = startDatePicker.getYear();      
  
              Log.d("$$LOGCHECK$$", "Clicked Schedule Adventure");
              Log.d("$$LOGCHECK$$", "Title: " + adventureTitle);
              Log.d("$$LOGCHECK$$", "Start Date: " + startMonth + "-" + startDay + "-" + startYear);
  
  
              Intent newIntent = new Intent();
  
          }
      });
  }
  </code></pre>
  
  <p>Date/Time Picker Code (from tutorial)</p>
  
  <pre><code>//---------------Date/Time Picker Methods-------------------------------------
  // Date Picker
  public static class DatePickerFragment extends DialogFragment
      implements DatePickerDialog.OnDateSetListener {
  
      @Override
      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);
  
          // 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) {
          // Do something with the date chosen by the user
          Log.d("$$LOGCHECK$$", "Date Picked--Year:" + year + " Month: " + month + " Day: " + day);
      }
  }
  
  public void showDatePickerDialog(View v) {
      DialogFragment newFragment = new DatePickerFragment();
      newFragment.show(getFragmentManager(), "datePicker");
  }
  
  // Time Picker
  public static class TimePickerFragment extends DialogFragment
      implements TimePickerDialog.OnTimeSetListener {
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      // Use the current time as the default values for the picker
          final Calendar c = Calendar.getInstance();
          int hour = c.get(Calendar.HOUR_OF_DAY);
          int minute = c.get(Calendar.MINUTE);
  
          // Create a new instance of TimePickerDialog and return it
          return new TimePickerDialog(getActivity(), this, hour, minute,
          DateFormat.is24HourFormat(getActivity()));
      }
  
      public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
          // Do something with the time chosen by the user
      }
  }
  
  public void showTimePickerDialog(View v) {
      DialogFragment newFragment = new TimePickerFragment();
      newFragment.show(getFragmentManager(), "timePicker");
  }
  </code></pre>
  
  <p>XML</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context=".AdventureSetUp" &gt;
  &lt;EditText
      android:id="@+id/adventure_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="@string/hint_adventure_title"
      android:inputType="textEmailAddress" /&gt;
  &lt;Button 
      android:id="@+id/picker_start_date"         
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="@string/btn_adventure_start_date" 
      android:onClick="showDatePickerDialog" /&gt;
  &lt;Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="@string/btn_adventure_start_time" 
      android:onClick="showTimePickerDialog" /&gt;
  
  
  &lt;Button 
      android:id="@+id/picker_end_date"       
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="@string/btn_adventure_end_date" 
      android:onClick="showDatePickerDialog" /&gt;
  &lt;Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="@string/btn_adventure_end_time" 
      android:onClick="showTimePickerDialog" /&gt;
  
  &lt;Button 
      android:id="@+id/btn_schedule_adventure"        
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="@string/btn_schedule_adventure" 
      android:onClick="scheduleAdventure" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>