I was just looking on the Android Developer site and the only tutorial they had to show a DatePicker was within a dialog. I didn’t want my datepicker in a dialog, I wanted it embedded into my activity.
However I already knew there was a DatePicker XML view that I could use, so I was wondering why the hell doesn’t the tutorial explain it. I spun up a quick little app and had my datepicker on screen, now I just needed to get at those fields. I trawled the APIs looking for something like ‘onDateChanged’ but there was nothing I was so confused!
Then I realised the datepicker is so much simpler than that. You just add the XML
to your layout file. And simply ask it what it’s values are! It’s so much easier than the examples on android developer. So here it is.
What we’re going to do:
- Add the date picker to your layout
- Get a global reference
- Query the date pickers values on a button click
So here .. we .. go
Layout is a no brainer just add your DatePicker and a button that when pressed will ask the date picker what its values are.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datePicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/selectButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onDateSelectedButtonClick" android:text="Select this date" /> </LinearLayout>
Then we have our Activity, this will display a toast when the button is pressed with the current day/month/year. Sorry my american friends I’ve stuck with my roots and my english date format 🙂 p.s. don’t forget the onClick of the button is set in the XML layout.
MainActivity.class
package com.blundell.tut.ui.phone; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.Toast; import com.blundell.tut.R; public class MainActivity extends Activity { private DatePicker picker; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); picker = (DatePicker) findViewById(R.id.datePicker); } public void onDateSelectedButtonClick(View v){ int day = picker.getDayOfMonth(); int month = picker.getMonth(); int year = picker.getYear(); Toast.makeText(this, day+"/"+month+"/"+year, Toast.LENGTH_SHORT).show(); } }
Hope you enjoy, haven’t added any source code this time as the code is pretty short, but if you think this is wrong and want the DatePicker Tutorial eclipse project source, just comment and I’ll hang it up.
Thanks for this posting. i search lot for basci datepicker for my app.finally i got the solution in your post ..thanks .a lot.
i want to set notification on particular date and time inbuilt in program , do not want set it from user side. can anyone suggest me how to set it in java file ?
very simple, the best datepicker ever posted….i spent hours trying to figure out the other tutorials just to get the values of mm/dd/yy….very very nice…keep on posting…