info@thistimebd.com

Thursday 21st of November 03:53:28pm

Time Picker android app source code

package com.thistimebd.web.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 {

    private TextView textView;

    private TimePicker timePicker;

    private Button button;




    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        timePicker = (TimePicker) findViewById(R.id.timePickerId);

        timePicker.setIs24HourView(true);

        textView = (TextView) findViewById(R.id.textViewId);

        button = (Button) findViewById(R.id.buttonId);


        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String time= timePicker.getCurrentHour()+ ":" +timePicker.getCurrentMinute();

                textView.setText(time);

            }

        });

    }

}

======================================================================

string.xml

---------

<resources>

    <string name="app_name">TimePicker</string>

    <string name="button_text">ShowTime</string>

</resources>

=======================================================================

activity_main.xml

-------------------


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

<LinearLayout 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"

    android:orientation="vertical"

    android:padding="10dp"

    tools:context="com.thistimebd.web.timepicker.MainActivity">


    <TimePicker

        android:layout_marginBottom="20dp"

        android:id="@+id/timePickerId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"></TimePicker>


    <Button

        android:id="@+id/buttonId"

        android:layout_marginBottom="20dp"

        android:layout_gravity="center_horizontal"

        android:textSize="20sp"

        android:textStyle="bold"

        android:text="@string/button_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/textViewId"

        android:layout_gravity="center_horizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textStyle="bold"

        android:textSize="20sp"

        />


</LinearLayout>