Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Thursday 19 June 2014

Android: Date picker in android example

Lets see how to code datepicker in android. You should have seen this various applications for example in ticket reservation apps. Its very simple to program this Datepicker dialogbox functionality. You need to have an editText, ImageButton components in activitymain.xml.
 
When you click on Imagebutton it should open a dialog box with todays date and while clicking on "set" it will set the value in editText component. To get todays date we need to create instance for Calendar class and get year, month and day.
 
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
           Then we need to set the ImageButton to invoke calendar dialogbox.
ib.setOnClickListener(this);
           This will return the DatePickerDialog with todays date.
 return new DatePickerDialog(this, datePickerListener,year, month, day);
 
 

            This is a constructor with some define parameters.
datePickerListener - This is a callback to indicate the date has been set.
Object for this listener is created as follows,
DatePickerDialog.OnDateSetListener datePickerListener =new DatePickerDialog.OnDateSetListener()
           Once the date is set this will invoke , "onDateSet" method to perform user define functionality where we have assigned the date value to editText.
 
 
activity_main.xml
 
<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"
    tools:context=".MainActivity"    
     >
     <EditText
         android:id="@+id/editText"
         android:layout_width="250dp"
         android:layout_marginTop="50dp"
         android:layout_marginLeft="5dp"
         android:layout_height="wrap_content"
         android:editable="false" >
</EditText>
     
     <ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/editText"
         android:layout_toRightOf="@+id/editText"
         android:contentDescription="@string/selectdate"
         android:cropToPadding="true"
         android:src="@drawable/calendar_icon" />

</RelativeLayout>
 
 
MainActivity.java
 
 
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {
 private ImageButton ib;
 private Calendar cal;
 private int day;
 private int month;
 private int year;
 private EditText et;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // mDateButton = (Button) findViewById(R.id.date_button);
  ib = (ImageButton) findViewById(R.id.imageButton1);
  cal = Calendar.getInstance();
  day = cal.get(Calendar.DAY_OF_MONTH);
  month = cal.get(Calendar.MONTH);
  year = cal.get(Calendar.YEAR);
  et = (EditText) findViewById(R.id.editText);
  ib.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  showDialog(0);
 }

 @Override
 @Deprecated
 protected Dialog onCreateDialog(int id) {
  return new DatePickerDialog(this, datePickerListener, year, month, day);
 }

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
  public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {
   et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
     + selectedYear);
  }
 };

}
 
Source code of this application   
 

3 comments:

  1. use full blog very nice friend

    ReplyDelete
  2. Thanks for doing this, really helps when thinking about what to buy. Any chance you including other manufacturers like OPPO, Huawei, OnePlus etc... in the future?

    ReplyDelete

Followers