Tuesday, October 22, 2013

Time Picker




In Android, we use “android.widget.TimePicker” class to render a time picker component to select hour and minute in a pre-defined user interface.

In this tutorial, we show you how to render time picker component via android.widget.TimePicker in current page, and also in dialog box via android.app.TimePickerDialog. In addition, we also show you how to set a hour and minute in time picker component.

File : res/layout/activity_time_picker.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"
    android:padding="20dp"
    tools:context=".Time_PickerActivity" >

    <TextView
        android:id="@+id/lblTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Current Time (H:M): "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/lblTime"
        android:layout_marginTop="24dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tvTime"
        android:layout_marginTop="36dp" />

    <Button
        android:id="@+id/btnChangeTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/timePicker1"
        android:layout_marginTop="44dp"
        android:text="Change Time" />


</RelativeLayout>


File : Time_PickerActivity.java


package com.mindmedia.timepicker;

import java.util.Calendar;
import android.app.Activity;
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.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Time_PickerActivity extends Activity {

private TextView tvDisplayTime;
private TimePicker timePicker1;
private Button btnChangeTime;

private int hour;
private int minute;

static final int TIME_DIALOG_ID = 999;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker);

setCurrentTimeOnView();
addListenerOnButton();

}

// display current time
public void setCurrentTimeOnView() {

tvDisplayTime = (TextView) findViewById(R.id.tvTime);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);

// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));

// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);

}

public void addListenerOnButton() {

btnChangeTime = (Button) findViewById(R.id.btnChangeTime);

btnChangeTime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

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() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;

// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));

Toast.makeText(
Time_PickerActivity.this,
"Time Picked : "
+ new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)), Toast.LENGTH_LONG)
.show();

// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);

}
};

private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}



Output :













0 comments:

Post a Comment

    Blogger news

    MIND MEDIA INNOVATIONS PVT LTD
    25/2953(2), Old GPO Building,
    Near Ayurveda College,
    Kunnumpuram,
    Thiruvananthapuram-695001
    Phone: +91 471 257 1001

    Email us @ :
    mindmediateam@gmail.com

    Total Pageviews

    Share us

    Cool Social Media Sharing Touch Me Widget by Blogger Widgets

    About

    Our hands with you, towards Android Technology