,

[TUT] Basic Date Picker

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

inline datepicker with toast of date

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 collapse=”false”]
<?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>
[/xml]

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
[java collapse=”false”]
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();
}
}
[/java]

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.

Comments

22 responses to “[TUT] Basic Date Picker”

  1. mwexo avatar
    mwexo

    Sincerely this is the best tutorial on date picking thanks alot for enlighting me

  2. Meghna avatar
    Meghna

    Perfect thanks… This https://www.compiletimeerror.com/2013/07/android-date-picker-example-android.html looks a bit different, may help.. Have a look…

  3. Hi
    Very nice tutorial on datepicker you can also this one

    Date Picker

    1. blundell avatar
      blundell

      hey sure, link to your site no problem 😉