Pages

Friday, 16 December 2011

Using android Date Picker




This is a sample activity which shows how to use Date picker control.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DatePickerExample.
2.) Create a date_picker.xml in res/layout.
3.) Put the following code in date_picker.xml :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
 <TextView android:id="@+id/dateDisplay"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="4dip"
           android:text="@string/hello"/>
        <Button android:id="@+id/pickDate"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="4dip"
           android:text="@string/hello"/>    
</LinearLayout>
4.) To create a date picker control we need the following imports :
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
5.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DatePickerExample. Enter following information:
Project name: DatePickerExample
Build Target: Google APIs
Application name: DatePickerExample
Package name: com.sample.DatePickerExample
Create Activity: DatePickerExample
On Clicking Finish DatePickerExample code structure is generated with the necessary Android Packages being imported along with DatePickerExample.java. DatePickerExample class will look like following:
package com.sample.DatePickerExample;
import android.app.Activity;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DatePickerExample extends Activity {
        private TextView mDateDisplay;
        private int mYear;
        private int mMonth;
        private int mDay;
        static final int DATE_DIALOG_ID = 1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.date_picker);
                mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
                Button pickDate = (Button) findViewById(R.id.pickDate);
                pickDate.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                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,
                                mDateSetListener,
                                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() {
                mDateDisplay.setText(
                        new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "));
        }
        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();
                }
        };
}
Output –The final output:

Android Project structure



android-logo
PROJECT STRUCTURE:
When new Android Application created. It use specific directory tree structure showing in eclipse SDK platform .There are Eight key items show in project tree structure as follows
1. AndroidManifest.Xml
2. bulid.xml
3. bin/
4. libs
5. src/
6. res/
7. assests
8. gen
project structure
AndroidManifest.xml :
When creating Android application must have an AndroidManifest.xml file in its root directory. It is an XML file describing the application being built and it contains activity, services, uses permission etc.
When creating a new project automatically generated file in project it contains only single main activity of project.
  • It describes the components of the application‚ the activities, services, broadcast receivers, and content providers that the application is composed of. It names the classes that implement each of the components and publishes their capabilities .These declarations let the Android system know what the components are and under what conditions they can be launched.
  • IN AndroidManifest.xml ¬†uses-permission elements, to indicate what permissions your application will need in order to function properly,
Example
android.permission.CALL_EMERGENCY_NUMBERS
android.permission.READ_CONTACT
android.permission.SET_WALLPAPER.
  • permission elements, to declare permissions that activities or services might require other applications hold in order to use your application’s data or logic ‚
  • instrumentation elements, to indicate code that should be invoked on key system events, such as starting up activities, for the purposes of logging or monitoring
  • uses-library elements, to hook in optional Android components, such as mapping services
  • its declares the minimum level of the Android API that used in applications as well as it declare verion name and version code just like
Example
android:versionCode=“1″
android:minSdkVersion=“3″
android:versionName=“1.0″
Build.Xml:
It is an ANT script for compiling the application and installing on the device.
BIN /:
When you compile your project (via ant or the IDE), the results go into the bin/ directory under your project root.
bin/classes/ holds the compiled Java classes
Bin/classes.dex holds the executable created from those compiled Java classes
bin/nameapp.ap_ holds your application’s resources, packaged as a ZIP file
bin/namedebug.apk or bin/nameapp-unsigned.apk is the actual Android application (where nameap is the name of your application)
The .apk file is a ZIP archive containing the .dex file, the compiled edition of your resources, any un-compiled resources (such as what you put in and the AndroidManifest.xml file. )
SRC/:
It contains all source code file (.java file) of android application. E.g hello_world.java
RES/:
Resources are to store all external contents that used in android applications.
These are external elements that you want to include and reference within your application, like images, audio, video, text strings, layouts, themes, etc.It contains all resources that are used in android application
Root directory:
  1. res/drawable : its contain images ,pictures (png, jpeg etc) e.g. .icon.png
  2. res/layout : contains XML(User Interface) that UI layout used in Project or view window of an application.(E.g. main.xml)
  3. res/Values : it declare Arrays, colors, dimensions, strings, and styles.(E.g. strings.xml)
  4. res/raw : it contains non-complied file (e.g. Audio file .mp3, video file .mpg)
E.g. it create an xml file  in values directory
<?xml version=“1.0″ encoding=“utf-8″?>
<resources>
<string name=“hello”>Welcome to Mobisoft Infotech! </string>
<string name=“app_name”>Hello world</string>
</resources>
ASSETS:
Assets it also store an external content refer in android applications. Just like images, audio, video, text strings .It same as resources but different in assets directory will maintain its raw file format and, in order to read it, you must use the AssetManager to read the file as a stream of bytes. So keeping files and data in resources (res/) makes them easily accessible.
libs/:
It’s holds any third-party Java JARs your application Requires.
GEN:
In Eclipse create an application that time auto generated R.java file in gen directory. The R.java file is an index (id) into all the resources defined in the file. This class files use in source code file or it gives for reference or location user interface object that uses in sources code.
Imp Note: Don’t change any code or id (assign value).