Create TimePicker Android App Using Android Studio

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, I will show you how to create a TimePicker Android application using Android Studio.
 
Requirements
Steps to be followed
 
Follow these steps to create a Native TimePicker Android application using Android Studio. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
Android
 
Add the activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml. This XML file contains the designing code for an Android app in the activity_main.xml.
 
Android
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.   
  7.     tools:context="abu.timepicker.MainActivity">  
  8.     <TimePicker  
  9.         android:id="@+id/simpleTimePicker"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="50dp"  
  14.         android:background="#0000FF"  
  15.         android:padding="20dp"  
  16.         android:timePickerMode="clock" />  
  17.     <TextView  
  18.         android:id="@+id/time"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:text="Time Is ::"  
  23.         android:textColor="#008000"  
  24.         android:textSize="20sp"  
  25.         android:textStyle="bold" />  
  26. </RelativeLayout>  
Step 5
 
Go to MainActivity.java. This Java program is the back-end language for Android.
 
Android
 
The Java code is given below.
  1. package abu.timepicker;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.app.TimePickerDialog;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.widget.TextView;  
  9. import android.widget.TimePicker;  
  10. import android.widget.Toast;  
  11. public class MainActivity extends AppCompatActivity {  
  12.     TextView time;  
  13.     TimePicker firstTimePicker;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         time = (TextView) findViewById(R.id.time);  
  20.         firstTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);  
  21.         firstTimePicker.setIs24HourView(false); // used to display AM/PM mode  
  22.         // perform set on time changed listener event  
  23.         firstTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {  
  24.             @Override  
  25.             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {  
  26.                 // display a toast with changed values of time picker  
  27.                 Toast.makeText(getApplicationContext(), hourOfDay + " " + minute, Toast.LENGTH_SHORT).show();  
  28.                 time.setText(" Now Time is :: " + hourOfDay + " : " + minute);  
  29.             }  
  30.         });  
  31.     }  
  32. }  
Step 6
 
Now, go to the menu bar and click "Make a project" or press ctrl+f9 to debug the app from errors.
 
Android
 
Step 7
 
Then, click the "Run" button or press shift+f10 to run the project. Choose the "virtual machine" and click OK.
 
Android
 

Conclusion

 
We have successfully created a TimePicker Android application using Android Studio.
 
Android
 
Android