As in our last tutorial we discussed about Date Picker Dialog, Now in this tutorial I am going to discuss about How to implement time picker in android app ?

Android provides controls for the user to pick a Time or pick a Date as ready-to-use dialogs . Each picker provides controls for selecting each part of the Time (hour, minute). Using these pickers helps ensure that your users can pick a time that is valid, formatted correctly, and adjusted to the user’s locale.

Time Picker is a kind of widget which is used pick a particular time.Time Picker is a dialog that prompts the user for the time of day using a TimePicker

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a button to your main layout file :

Here we create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<Button
        android:id="@+id/mTimePicker"
        android:text="Time Picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_margin="8dp"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="22sp"
        android:background="@android:color/holo_green_light"/>

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/mTimePicker"
        android:text="Time Picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_margin="8dp"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="22sp"
        android:background="@android:color/holo_green_light"/>


</RelativeLayout>

3. Now open your MainActivity.java and add following code :

Now we create a Time picker Dialog view in android, when we click on button using setOnClickListener( )  .

MainActivity.java :

package com.technxtcodelabs;

import android.app.TimePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {

    Button mTimePicker;
    TimePickerDialog.OnTimeSetListener mOnTimeSetListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTimePicker = (Button)findViewById(R.id.mTimePicker);

        mTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar mCalendar =  Calendar.getInstance();
                int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
                int minute = mCalendar.get(Calendar.MINUTE);


                TimePickerDialog mTimePickerDialog = new TimePickerDialog(
                        MainActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        mOnTimeSetListener,
                        hour,minute,true);

                mTimePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                mTimePickerDialog.show();

            }
        });

        mOnTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourofday, int minute) {
                String mTime = hourofday+":"+minute;
                mTimePicker.setText(mTime);
            }
        };

    }
}

Now just run the project/app to test date picker in android app through android device or any Emulator .

%d bloggers like this: