info@thistimebd.com

Thursday 21st of November 03:59:41pm

Time Picker Dialog android app source code

package com.thistimebd.web.timepicker;


import android.app.DatePickerDialog;

import android.app.TimePickerDialog;

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 implements View.OnClickListener {

    private TextView textView;


    private Button button;

    private TimePickerDialog timePickerDialog;




    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



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

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

        button.setOnClickListener(this);



    }


    @Override

    public void onClick(View v) {


        TimePicker timePicker=new TimePicker(this);

        int currentHour=timePicker.getCurrentHour();

        int currentMinute=timePicker.getCurrentMinute();


        timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {

            @Override

            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                textView.setText(hourOfDay+":"+minute);


            }

        },currentHour,currentMinute,true);


        timePickerDialog.show();


    }

}

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

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




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