Tuesday, March 16, 2021

Android TimePicker

 

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. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.timepicker.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_above="@+id/button1"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentStart="true"  
  16.         android:layout_marginBottom="102dp"  
  17.         android:layout_marginLeft="30dp"  
  18.         android:layout_marginStart="30dp"  
  19.         android:text="" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/button1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignParentBottom="true"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:layout_marginBottom="20dp"  
  28.         android:text="Change Time" />  
  29.   
  30.     <TimePicker  
  31.         android:id="@+id/timePicker"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_above="@+id/textView1"  
  35.         android:layout_centerHorizontal="true"  
  36.         android:layout_marginBottom="36dp" />  
  37. </RelativeLayout>  


Activity class

File: MainActivity.java
  1. package example.javatpoint.com.timepicker;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. import android.widget.TimePicker;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     TextView textview1;  
  12.     TimePicker timepicker;  
  13.     Button changetime;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         textview1=(TextView)findViewById(R.id.textView1);  
  20.         timepicker=(TimePicker)findViewById(R.id.timePicker);  
  21.         //Uncomment the below line of code for 24 hour view  
  22.         timepicker.setIs24HourView(true);  
  23.         changetime=(Button)findViewById(R.id.button1);  
  24.   
  25.         textview1.setText(getCurrentTime());  
  26.   
  27.         changetime.setOnClickListener(new View.OnClickListener(){  
  28.             @Override  
  29.             public void onClick(View view) {  
  30.                 textview1.setText(getCurrentTime());  
  31.             }  
  32.         });  
  33.   
  34.     }  
  35.   
  36.     public String getCurrentTime(){  
  37.         String currentTime="Current Time: "+timepicker.getCurrentHour()+":"+timepicker.getCurrentMinute();  
  38.         return currentTime;  
  39.     }  
  40.   
  41. }  

Output:

android timepicker example 1 android timepicker example 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile