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. tools:context="abu.timepicker.MainActivity">
  7. <TimePicker
  8. android:id="@+id/simpleTimePicker"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerHorizontal="true"
  12. android:layout_marginTop="50dp"
  13. android:background="#0000FF"
  14. android:padding="20dp"
  15. android:timePickerMode="clock" />
  16. <TextView
  17. android:id="@+id/time"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_centerHorizontal="true"
  21. android:text="Time Is ::"
  22. android:textColor="#008000"
  23. android:textSize="20sp"
  24. android:textStyle="bold" />
  25. </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. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. time = (TextView) findViewById(R.id.time);
  19. firstTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);
  20. firstTimePicker.setIs24HourView(false); // used to display AM/PM mode
  21. // perform set on time changed listener event
  22. firstTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
  23. @Override
  24. public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  25. // display a toast with changed values of time picker
  26. Toast.makeText(getApplicationContext(), hourOfDay + " " + minute, Toast.LENGTH_SHORT).show();
  27. time.setText(" Now Time is :: " + hourOfDay + " : " + minute);
  28. }
  29. });
  30. }
  31. }
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