Sunday, 15 March 2015

Android Date Picker

So here we are with an example to demonstrate how to implement Date Picker in your Android application.

First we need to create a TextBox to display the date and a button to trigger the Date Picker Dialog Box.

<RelativeLayout 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"
    tools:context="com.codingredefined.datepicker.MainActivity" >

    <TextView
        android:id="@+id/displayDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button 
        android:id="@+id/pickDate"
        android:layout_below="@+id/displayDate"
        android:paddingTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick Date" />

</RelativeLayout>



After creating the layout for our application we need to add following code in our MainActivity class.

package com.codingredefined.datepicker;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity
{

private TextView displayDate;
private int mday, mmonth, myear;
static final int DATE_DIALOG_ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        displayDate = (TextView)findViewById(R.id.displayDate);
        Button pickDate = (Button)findViewById(R.id.pickDate);
       
        pickDate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

showDialog(DATE_DIALOG_ID);

}
});
   
        final Calendar c = Calendar.getInstance();
        myear = c.get(Calendar.YEAR);
        mmonth = c.get(Calendar.MONTH);
        mday = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();
       
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
    switch(id)
    {
    case DATE_DIALOG_ID:
    return new DatePickerDialog(this, dateSetListener, myear, mmonth, mday);
    }
    return null;
    }
   
    protected void onPrepareDialog(int id, Dialog dialog)
    {
    switch(id)
    {
    case DATE_DIALOG_ID:
    ((DatePickerDialog)dialog).updateDate(myear, mmonth, mday);
    break;
    }
    }
   
    private void updateDisplay()
    {
    displayDate.setText(new StringBuilder().append(mmonth +1).append("-").append(mday).append("-").append(myear).append(" "));
    }
   
    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub

myear = year;
mmonth = monthOfYear;
mday = dayOfMonth;
updateDisplay();

}
};
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}







Save the code and run.




No comments:

Post a Comment