Android Date Time example

Please follow the 2 steps to implement Date and Time picker in application.

1. create xm like  “main.xml”

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical” android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:text=”@string/hello” />
<RelativeLayout android:orientation=”vertical”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
<Button android:id=”@+id/timepicker” android:layout_width=”121px”
android:layout_height=”wrap_content” android:text=”Time Picker”
android:layout_alignParentTop=”true” android:layout_alignParentRight=”true”>
</Button>
<Button android:id=”@+id/datepicker” android:layout_width=”122px”
android:layout_height=”wrap_content” android:text=”Date Picker”
android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true”>
</Button>
</RelativeLayout>
</LinearLayout>

2. Append an event listner in Activity like below:

package com.pr.testone;

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.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class TestOneActivity extends Activity implements Button.OnClickListener{
private Button b1, b2;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Adding button to the layout
b1 = (Button) findViewById(R.id.datepicker);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.timepicker);
b2.setOnClickListener(this);

}

// Creating dialog
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
cday);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, 0, 0,false);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
String date_selected = String.valueOf(monthOfYear + 1) + ” /”
+ String.valueOf(dayOfMonth) + ” /” + String.valueOf(year);
Toast.makeText(TestOneActivity.this,
“Selected Date is =” + date_selected, Toast.LENGTH_SHORT)
.show();
}
};

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(TestOneActivity.this, “Time is=”+hourOfDay+”:”+minute, Toast.LENGTH_SHORT).show();
}
};

@Override
public void onClick(View v) {
if (v == b1){
showDialog(DATE_DIALOG_ID);
}else if (v == b2) {
showDialog(TIME_DIALOG_ID);
}
}
}

The result will be as shown in screen captures.

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s