DatePicker

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android DatePicker Example

android datepicker

Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.

The android.widget.DatePicker is the subclass of FrameLayout class.

Android DatePicker Example

Let's see the simple example of datepicker widget in android.

activity_main.xml

File: activity_main.xml

 
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="50dp"  
  14.         android:layout_marginTop="36dp"  
  15.         android:text="Current Date:" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_centerHorizontal="true"  
  23.         android:layout_marginBottom="140dp"  
  24.         android:text="Change Date" />  
  25.   
  26.     <DatePicker  
  27.         android:id="@+id/datePicker1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_above="@+id/button1"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginBottom="30dp" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java

 

  1. package com.example.datepicker2;  

  2.   

  3. import android.os.Bundle;  

  4. import android.app.Activity;  

  5. import android.view.Menu;  

  6. import android.view.View;  

  7. import android.view.View.OnClickListener;  

  8. import android.widget.Button;  

  9. import android.widget.DatePicker;  

  10. import android.widget.TextView;  

  11. import android.widget.Toast;  

  12.   

  13. public class MainActivity extends Activity {  

  14.     DatePicker picker;  

  15.     Button displayDate;  

  16.     TextView textview1;  

  17.     @Override  

  18.     protected void onCreate(Bundle savedInstanceState) {  

  19.         super.onCreate(savedInstanceState);  

  20.         setContentView(R.layout.activity_main);  

  21.           

  22.         textview1=(TextView)findViewById(R.id.textView1);  

  23.         picker=(DatePicker)findViewById(R.id.datePicker1);  

  24.         displayDate=(Button)findViewById(R.id.button1);  

  25.           

  26.         textview1.setText(getCurrentDate());  

  27.           

  28.         displayDate.setOnClickListener(new OnClickListener(){  

  29.             @Override  

  30.             public void onClick(View view) {  

  31.                 textview1.setText(getCurrentDate());  

  32.             }  

  33.               

  34.         });  

  35.     }  

  36.     public String getCurrentDate(){  

  37.         StringBuilder builder=new StringBuilder();  

  38.         builder.append("Current Date: ");  

  39.         builder.append((picker.getMonth() + 1)+"/");//month is 0 based  

  40.         builder.append(picker.getDayOfMonth()+"/");  

  41.         builder.append(picker.getYear());  

  42.         return builder.toString();  

  43.     }  

  44.     @Override  

  45.     public boolean onCreateOptionsMenu(Menu menu) {  

  46.         // Inflate the menu; this adds items to the action bar if it is present.  

  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  

  48.         return true;  

  49.     }  

  50.   

  51. }