Saturday, 26 January 2013

Ics Date Picker Example in Android

Download the following Libraries from the url :  https://github.com/SimonVT . You will find list of libraries and download it which is circled in the below image.

Ensure you have added calendarview and number picker library to the date picker libray.
 And your project should add this date picker library as follows :

activity_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Date" />

</RelativeLayout>

IcsDatePickerActivity.java


package com.rajeshvijayakumar.datepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import net.simonvt.app.DatePickerDialog;
import net.simonvt.widget.DatePicker;

import java.util.Calendar;


public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {

        showDialog(DATE_PICKER_ID);

    }
}

styles.xml


    <style name="SampleTheme" parent="@android:style/Theme">
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
        <item name="datePickerStyle">@style/Widget.Holo.DatePicker</item>
        <item name="numberPickerUpButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerUpButton</item>
        <item name="numberPickerDownButtonStyle">@style/NPWidget.Holo.ImageButton.NumberPickerDownButton</item>
        <item name="numberPickerInputTextStyle">@style/NPWidget.Holo.EditText.NumberPickerInputText</item>
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>

In Manifest.xml  give android:theme="@style/SampleTheme"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.datepicker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/SampleTheme" >
        <activity
            android:name="com.rajeshvijayakumar.datepicker.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 Output :








Source Code : Download this example Here



1 comment: