Monday, August 24, 2015

Android TimePickerDialog Example

MainActivity.java

package com.cfsuman.androidtutorials;

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;


public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the widgets reference from XML layout
                Button button = (Button) findViewById(R.id.button);

                button.setOnClickListener(v -> {
                        // Initialize a new time picker dialog fragment
                        DialogFragment dialogFragment
                                = new TimePickerFragment();

                        // Show the time picker dialog fragment
                        dialogFragment.show(
                                getSupportFragmentManager(),
                                "Time Picker"
                        );
                });
        }
}
TimePickerFragment.java

package com.cfsuman.androidtutorials;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;


public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        // Get a Calendar instance
        Calendar calendar = Calendar.getInstance();
        // Get the current hour and minute
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);


        // Create a TimePickerDialog with current time
        // Return the TimePickerDialog
        return new TimePickerDialog(
                getActivity(), this,
                hour,minute,false);
    }



    public void onTimeSet(TimePicker view,
                          int hourOfDay, int minute){
        // Do something with the returned time
        TextView textView = (TextView) requireActivity()
                .findViewById(R.id.textView);
        textView.setText(hourOfDay + " : " + minute);
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Time"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        tool:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>