In this tutorial we will see how we are selecting the date and time in android mobile. Android supports this functionality through the TimePicker and DatePicker views. The following sections show how to make use of these views in your activity...

The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode. The following Try it Out shows you how to use it.

TRY IT OUT  Using the Time&Date Picker View 

1.    Using Eclipse, create an Android  project and name it pickerview.

2.    Modify the main.xml file located in the res/layout folder by adding the following line:

<?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:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="26px"
android:typeface="sans"></TextView>
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="26px"
android:typeface="sans"></TextView>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

3.    Press F11 to debug the application on the Android Emulator. The below shows the Time&Date picker view in action. Besides clicking on the plus(+) and minus(-) button, you can use the numeric keypad on the device to change the hour and minute, and click the AM button to toggle between AM and PM.



4.    Back inn Eclipse, add the following statements to the pickerview.java file:

package com.pickerview;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
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;
import android.widget.TimePicker;

public class pickerview extends Activity 

{
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 2;
private TextView dateDisplay;
private Button pickDate;
private int year, month, day;
private TextView timeDisplay;
private Button pickTime;
private int hours, min;

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
   
dateDisplay = (TextView)findViewById(R.id.textView1);
pickDate = (Button)findViewById(R.id.button1);
pickDate.setOnClickListener( new OnClickListener() 
{
public void onClick(View v) 
{
showDialog(DATE_DIALOG_ID);
}
});
final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
updateDate();
timeDisplay = (TextView)findViewById(R.id.textView2);
pickTime = (Button)findViewById(R.id.button2);
pickTime.setOnClickListener( new OnClickListener () 
{
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
hours = cal.get(Calendar.HOUR);
min = cal.get(Calendar.MINUTE);
updateTime();
}
private void updateTime() 
{
timeDisplay.setText(new StringBuilder().append(hours).append(':')
.append(min));
}
private void updateDate() 
{
dateDisplay.setText(new StringBuilder().append(day).append('-')
.append(month + 1).append('-').append(year));
}
private DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() 
{
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
year = yr;
month = monthOfYear;
day = dayOfMonth;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() 
{
public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
{
hours = hourOfDay;
min = minute;
updateTime();
}
};
protected Dialog onCreateDialog(int id)
{
switch(id) 
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateListener, year, month, day);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeListener, hours, min, false);
}
return null;
}
}

5.    Add the following statements to the android manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pickerview"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".pickerview"
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>
Download Code

Categories: