Introduction
Lets see how to code datepicker and timepicker in android.You should have seen this various applications for example in ticket reservation apps. Its very simple to program this Datepicker and Timepicker dialogbox functionality. You need to have an editText, ImageView components in activity_main.xml. When you click on ImageView it should open a dialog box with todays date and while clicking on "set" it will set the value in editText component. To get todays date we need to create instance for Calendar class and get year, month and day, and time too.demo
Using the code
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Date" />
<RelativeLayout
android:id="@+id/rel2"
android:layout_below="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/imageView1"
android:layout_marginTop="10dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/imageView1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:src="@drawable/date" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rel3"
android:layout_below="@+id/rel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Set Time " />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toLeftOf="@+id/imageView2"
android:layout_marginTop="10dp"
android:ems="10" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="32dp"
android:layout_marginRight="5dp"
android:src="@drawable/time" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
MainActivity.java:
package com.example.dateandtimepickers;
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.EditText;
import android.widget.ImageView;
import android.widget.TimePicker;
public class MainActivity extends Activity implements OnClickListener{
EditText setdate,settime;
ImageView date,time;
Button set;
static final int TIME_DIALOG_ID = 1111;
private int mYear, mMonth, mDay;
TimePickerDialog mTimeSetListener;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date=(ImageView)findViewById(R.id.imageView1);
time=(ImageView)findViewById(R.id.imageView2);
setdate=(EditText)findViewById(R.id.editText1);
settime=(EditText)findViewById(R.id.editText2);
date.setOnClickListener(this);
time.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == date)
{
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
setdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
if (v == time)
{
showDialog(TIME_DIALOG_ID);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new
TimePickerDialog.OnTimeSetListener() {
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
}
};
@SuppressWarnings("unused")
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
settime.setText(aTime);
}
}
1 comments:
Write commentsIt's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.android development course fees in chennai | android app development training in chennai|Android Training institute in chennai with placement
ReplyEmoticonEmoticon