Set Time In Android App Using TimePicker In Android Studio

Introduction

This blog explains to how to set the time in an Android app, using TimePicker in Android Studio.

Requirements

  • Android Studio 2.1.3

If you want to set the time in an Android app, you should follow the steps given below.

Step 1

Now, open Android Studio and you can choose the file, followed by New. Afterwards, choose New Project.

android

Step 2

Here, you can create your application's name and choose where your project is stored on the location, and click Next button.

android

Now, we can select the version of Android -- Target Android Devices.

android

Step 3

Here, we can add the activity and click Next button.

android

Now, we can write the activity name and click Finish button.

android

Step 4

Now, open your project and you will go to activity_main.xml and afterwards, you will build the design. You should choose toolbox and if you want some options (TimePicker,TextView), use the drag and drop method.

android

Now, we can see the Graphical User Interface design.

android

Step 5

Here, you need to build on the design and write .XML code.

activity_mai.xml code.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.timepickerapp.MainActivity">  
  3.     <TimePicker android:id="@+id/simpleTimePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:background="#998f00" android:padding="20dp" android:timePickerMode="spinner" />  
  4.     <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Time Is ::" android:textColor="#8c0099" android:textSize="20sp" android:textStyle="bold" />  
  5. </RelativeLayout>  
Step 6

Now, you will go to the MainActivity.java page and build Java code.

First of all, you will declare a file, which is an extension file.

android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.timepickerapp;  
  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 7

Here, you will go to run it and select Run-> Run app option.

android

Here, you will choose Emulator or the devices; it is Nokia Nokia _X.

android

Step 8


Here, you can see the output.

android

Now, you will set the PM time and afterwards, you will see the output, as given below.

android

 

Ebook Download
View all
Learn
View all