What is timepicker

Android timepicker is used to pick the time.


Time Picker:.

In this section i am going to show the timepicker with practical example.

First open the android studio and create a project and give the name of your project like TimePickerDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the two TextView one Button and one TimePicker from pallet.If TimePicker not showing in pallet you simple write the code inside the activity_main.xml file. Open the activity_main.xml file and code look like this


<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="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">
<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"


    />



    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="400dp"
        android:layout_marginLeft="50dp"
        android:textSize="20sp"
        android:text="Selected Time" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="400dp"
        android:layout_marginLeft="200dp"
        android:textSize="20sp"
        android:text="Selected Time" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="470dp"
        android:layout_marginLeft="140dp"

        android:text="Button" />

</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.sql.Time;

public class MainActivity extends AppCompatActivity {

   TimePicker timePicker;
   Button button;
   TextView textView;

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

        button=(Button)findViewById(R.id.button);
        textView=(TextView)findViewById(R.id.textView2);
        timePicker=(TimePicker)findViewById(R.id.timePicker);
        button.setOnClickListener(
              new View.OnClickListener() {
              @Override
              public void onClick(View v) {

              String hour=timePicker.getCurrentHour().toString();
              String minut=timePicker.getCurrentMinute().toString();
              String time=hour+":"+minut;
              textView.setText(time);
                    }
                }
        );


    }
}



       

Explanation:

TimePicker timePicker:This statement declare the timePicker object of TimePicker.

Button button:This statement declare the button object of Button.

TextView textView:This statement declare the textView of TextView.

button=(Button)findViewById(R.id.button):This statement initialized the button object.

textView=(TextView)findViewById(R.id.textView2):This statement initialized the textView object.

timePicker=(TimePicker)findViewById(R.id.timePicker):This statement initialized the timepicker object of TimePicker.

String time=hour+":"+minut;:This statement concat the hour and minut and store into the time variable.

textView.setText(time):This statement set the time into textView with the help of setText().


Time Picker

What is timepicker for video Click here

Smiley face                     Smiley face