articles

TimePicker in Andriod

Nitish Kesarwani 2794 28-Dec-2017
 Time Picker

1. Time Picker is a control which provides user to pick the time from the analog clock to pick the time.

2. It allows to pick hours first and then minutes.

3. It is a part of widget control panel.

4. It provides two formats one is 24 hours and another one is AM/PM.

5. TimePicker, TextView and Button controls are used in this program.

6. onClickListener() and timePicker() method is used to invoke method.

Attributes

android: timePickerMode: The default value of timePicker is a clock(1) and it can set as a spinner mode.  

Methods

• is24HourView(): This method checks whether a 24-hour view is true or false

• setis24HourView(): By this method, we can set 24-hour view status

• isEnabled(): By this method, we get the enabled status of view

• setEnabled(): This method is used to enable the view status

• setCurrentHour(): It receives the current status of an hour clock from the user

• setCurrentMinutes(): It sets the current status of minutes from clock by a user

• setonTimeChangedListener(): This method generates a response when the user selects the value from the clock


Android_manifest.xml 
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsample.msclient009.timepicker">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.mapsample.msclient009.timepicker.MainActivity">
    <TimePicker
        android:id="@+id/tpick"
        android:layout_width="370dp"
        android:layout_height="150dp"
        android:layout_weight="1" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
         android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="timePicker Clock" />
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#199119"/>
    </LinearLayout>
</LinearLayout>
Main_Activity.java
package com.mapsample.msclient009.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;
TextView tv;
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        tp =(TimePicker)findViewById (R.id.tpick );
        tv = (TextView)findViewById ( R.id.tv );
        btn =(Button)findViewById ( R.id.btn );
        tv.setText(timePicker ());
        btn.setOnClickListener ( new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                tv.setText ( timePicker () );
            }
        } );

    }
    public String timePicker()
    { String time = "Current Time :"+tp.getCurrentHour ()+":"+tp.getCurrentMinute ();
    return time;
    }
}


Run Your APP

TimePicker in Andriod TimePicker in Andriod


Updated 07-Sep-2019

Leave Comment

Comments

Liked By