Create Android Simple TimePicker Widget Example Tutorial

How to use time picker tool in android application so user can select time.

Android time picker tool is the most useful widget for android application because with the use of this widget app developer can get time from app user. This widget is mostly used to create Alarm clock android application. This tutorial is very simple we are just creating a time picker in this app and getting its entered time on button click and display using Toast message. So here is the complete step by step tutorial for Android Simple TimePicker Widget Example Tutorial.

android-project-download-code-button

Android Simple TimePicker Widget Example Tutorial.

Code for MainActivity.java file.

 package com.timepicker_android_examples.com;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

 TimePicker timepicker;
 int hour, minutes;
 Button GetTime;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 timepicker = (TimePicker)findViewById(R.id.timePicker1);
 
 GetTime = (Button)findViewById(R.id.button1);
 
 timepicker.setIs24HourView(true);
 
 GetTime.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 hour = timepicker.getCurrentHour();
 minutes = timepicker.getCurrentMinute();
 
 Toast.makeText(MainActivity.this, "Selected time :" + hour +
 ":" + minutes ,Toast.LENGTH_LONG).show();
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.timepicker_android_examples.com.MainActivity" >

 <TimePicker
 android:id="@+id/timePicker1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/timePicker1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="14dp"
 android:text="Click Here to Show selected time" />

</RelativeLayout>

Screenshot:

Android Simple TimePicker Widget Example Tutorial

Click here to download Android Simple TimePicker Widget Example Tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *