Android program to implement TimePicker

Android program to implement TimePicker

Android program to implement TimePicker. TimePicker is a widget used to select the time of the day. It can either be in a 24-hour format or AM/PM mode. For example, when you set an alarm on your android phone, you mostly use TimePicker. You can set the timePickerMode to spinner or clock.

Methods associated with TimePicker are:-

In the code given below, we have taken the 24-hour mode. Now to display AM/PM we have used a simple logic that is if the time is greater than 11 then display PM else display AM. Just to be bit user friendly we have also added the condition that if the hour is 0 then display 12. We have used the timePickerMode as spinner., you can also set it to clock.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “TimePicker” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the contents of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="codedost.timepicker.MainActivity">

    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/timePicker"
        android:timePickerMode="spinner"/>

    <Button
        android:text="Display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timePicker"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_below="@+id/button"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:id="@+id/textView" />

</RelativeLayout>

MainActivity.java

package codedost.timepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

    TimePicker tp;
    Button b1;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tp=(TimePicker) findViewById(R.id.timePicker);
        b1=(Button) findViewById(R.id.button);
        tv=(TextView) findViewById(R.id.textView);
        tp.setIs24HourView(true);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb= new StringBuffer();
                if(tp.getHour()==0)
                {
                    sb.append((tp.getHour()+12)+":");
                    sb.append(tp.getMinute()+":");
                    sb.append("AM");

                }
                else if(tp.getHour()>11)
                {
                    sb.append((tp.getHour()-12)+":");
                    sb.append(tp.getMinute()+":");
                    sb.append("PM");
                }
                else
                {
                    sb.append((tp.getHour())+":");
                    sb.append(tp.getMinute()+":");
                    sb.append("AM");
                }
                tv.setText(sb.toString());
            }
        });
    }
}

Output

Share Me!