AdSense

AdSense3

Tuesday 4 August 2015

Android TimePicker Example

android time picker
Android TimePicker widget is used to select date. It allows you to select time by hour and minute. You cannot select time by seconds.
The android.widget.TimePicker is the subclass of FrameLayout class.

Android TimePicker Example

Let's see a simple example of android time picker.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="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.     tools:context=".MainActivity" >  
  6.   
  7.     <TimePicker  
  8.         android:id="@+id/timePicker1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="86dp" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/textView1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/timePicker1"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_marginTop="17dp"  
  22.         android:text="Current Time:" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/button1"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignLeft="@+id/timePicker1"  
  29.         android:layout_below="@+id/timePicker1"  
  30.         android:layout_marginLeft="37dp"  
  31.         android:layout_marginTop="55dp"  
  32.         android:text="Change Time" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.timepicker1;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10. import android.widget.TimePicker;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     TextView textview1;  
  15.     TimePicker timepicker1;  
  16.     Button changetime;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.           
  23.         textview1=(TextView)findViewById(R.id.textView1);  
  24.         timepicker1=(TimePicker)findViewById(R.id.timePicker1);  
  25.         //Uncomment the below line of code for 24 hour view  
  26.         timepicker1.setIs24HourView(true);  
  27.         changetime=(Button)findViewById(R.id.button1);  
  28.           
  29.         textview1.setText(getCurrentTime());  
  30.           
  31.         changetime.setOnClickListener(new OnClickListener(){  
  32.             @Override  
  33.             public void onClick(View view) {  
  34.                  textview1.setText(getCurrentTime());  
  35.             }  
  36.         });  
  37.           
  38.     }  
  39.   
  40.     public String getCurrentTime(){  
  41.         String currentTime="Current Time: "+timepicker1.getCurrentHour()+":"+timepicker1.getCurrentMinute();  
  42.         return currentTime;  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  48.         return true;  
  49.     }  
  50.   
  51. }  

1 comment:

  1. almost similar to datepicker. Now i can use them in my basic app.
    thank you kundan

    ReplyDelete