Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Data Picker in Android Application
Data Picker in Android Application
Data Picker in Android Application


by Rohit Kesharwani on 10/25/2011 8:05:08 PM

Views: 2832       Comments: 0

Data Picker in Android Application

To provide a widget for selecting a date, use the DatePicker widget, which allows the user to select the month, day, and year, in a familiar interface.

In this article, I am going to explain how to create a DatePickerDialog, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, a TextView will update with the new date.

·         Start a new project named DatePickerDemo.

·         Open the res/layout/main.xml file and insert the following:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView

       android:id="@+id/dateDisplay"

       android:layout_marginTop="10dp" 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

<Button android:id="@+id/pickDate"

              android:layout_marginTop="10dp"

              android:layout_width="wrap_content"

              android:layout_height="wrap_content"

              android:text="Change the date"    />

</LinearLayout>

·         Open Activity file and add the following code as shown below:

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.Button;

import android.widget.DatePicker;

import android.widget.TextView;

 

public class DatePickerActivity extends Activity {

   

       private TextView mDateDisplay;

       private Button mPickDate;

       private int mYear;

       private int mMonth;

       private int mDay;

       static final int DATE_DIALOG_ID = 0;

      

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        // capture or view elements

        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);

        mPickDate = (Button) findViewById(R.id.pickDate);

       

        // add a click listener on the button

        mPickDate.setOnClickListener(new OnClickListener() {

                    

                     public void onClick(View v) {

                           showDialog(DATE_DIALOG_ID);

                     }

              });

       

        // get the current date

        final Calendar c = Calendar.getInstance();

        mYear = c.get(Calendar.YEAR);

        mMonth = c.get(Calendar.MONTH);

        mDay = c.get(Calendar.DAY_OF_MONTH);

       

        updateDisplay();

    }

   

    // updates the date in the TextView   

    private void updateDisplay()

    {       

       mDateDisplay.setText(new StringBuilder().append(mMonth + 1).append("-
                            "
).append(mDay).append("-").append(mYear).append(" "));   

    }

   

    // the callback received when the user "sets" the date in the dialog

    private DatePickerDialog.OnDateSetListener mDateSetListener = new 
                                                DatePickerDialog.OnDateSetListener()

              {

 

                           public void onDateSet(DatePicker view, int year,

                                         int monthOfYear, int dayOfMonth) {

                                 

                                   mYear = year;                   

                                   mMonth = monthOfYear;                   

                                   mDay = dayOfMonth;                  

                                   updateDisplay();

                           }

              };

   

    @Override

    protected Dialog onCreateDialog(int id)

    {

       switch(id)

       {

       case DATE_DIALOG_ID:

              return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);

       }

       return null;

    }

}

 

·         Run the application.
You output looks like something this:

Data Picker in Android Application

When you click on the button (Change the date), a date picker dialog will open, you can set the date from the date picker dialog.

Data Picker in Android Application

After setting the date, a new date will display in the TextView.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2904
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.