TimePicker In Android

Introduction

 
This article explains TimePicker in Android. Android Studio is used to create the sample.
 
This application picks the time from the system on a button click. For this you need to use the TimePicker to pick the time of a system, Button to provide input, and TextView in your XML file. The TimePicker class provides two methods, getCurrentHour() and getCurrentMinute().
  • getCurrentHour() is provided by the TimePicker class that returns the current hour.  
  •  getCurrentMinute() is provided by the TimePicker class that returns the current minute.
Step 1
 
Create a project like this:
 
imageentervalue
 
Step 2
 
Create an XML file and put a TimePicker, Buttton, and TextView in it, as in the following:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#f1234567">  
  11.    
  12. <TimePicker  
  13.     android:layout_height="wrap_content"  
  14.     android:layout_marginTop="86dp"  
  15.     android:layout_centerHorizontal="true"  
  16.     android:layout_alignParentTop="true"  
  17.     android:layout_width="wrap_content"  
  18.     android:id="@+id/timePicker">  
  19.    
  20. </TimePicker>  
  21.    
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="TimePicker"  
  26.         android:textStyle="bold"  
  27.         android:textSize="30dp"  
  28.         android:id="@+id/textView"  
  29.         android:layout_marginLeft="80dp"  
  30.         />  
  31.    
  32.     <Button  
  33.         android:layout_width="200dp"  
  34.         android:layout_height="wrap_content"  
  35.         android:text="TimePicker"  
  36.         android:textColor="#ffffff"  
  37.         android:textStyle="bold"  
  38.         android:id="@+id/button"  
  39.         android:background="#000000"  
  40.         android:layout_marginTop="300dp"  
  41.         android:layout_centerHorizontal="true"  
  42.         />  
  43. </RelativeLayout> 
Step 3
 
Create a Java file with the following.
 
In this file, you will create the ID's of all the resources and provide the functionality of all. Now create the getCurrentTime() method in which you will get the current hour and current minute in the string variable. Set the button on its click and in the onClick set the text of a textView to "textView.setText(getCurrentTime)".
  1. package com.timepicker;  
  2. import android.content.DialogInterface;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.text.format.Time;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10. import android.widget.TimePicker;  
  11. public class MainActivity extends Activity   
  12. {  
  13.  TextView textView;  
  14.  Button button;  
  15.  TimePicker timePicker;  
  16.  @Override  
  17.  protected void onCreate(Bundle savedInstanceState)   
  18.  {  
  19.   super.onCreate(savedInstanceState);  
  20.   setContentView(R.layout.activity_main);  
  21.   textView = (TextView) findViewById(R.id.textView);  
  22.   button = (Button) findViewById(R.id.button);  
  23.   timePicker = (TimePicker) findViewById(R.id.timePicker);  
  24.   button.setOnClickListener(new View.OnClickListener()   
  25.   {  
  26.    @Override  
  27.    public void onClick(View v)   
  28.    {  
  29.     textView.setText(getCurrentTime());  
  30.    }  
  31.   });  
  32.  }  
  33.  public String getCurrentTime()   
  34.  {  
  35.   String currentTime = (timePicker.getCurrentHour()) + ":" + timePicker.getCurrentMinute();  
  36.   return currentTime;  
  37.  }  
  38.  @Override  
  39.  public boolean onCreateOptionsMenu(Menu menu)   
  40.  {  
  41.   getMenuInflater().inflate(R.menu.main, menu);  
  42.   return true;  
  43.  }  
Step 4
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.timepicker"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.timepicker.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 5
 
Time set on the TextView on a button click by TimePicker:
 
firstactivity
 
createproject


Similar Articles