Monday, August 24, 2015

How to get AM PM value from TimePickerDialog in Android

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

        // Return the TimePickerDialog
        return new TimePickerDialog(
                getActivity(),0,
                this,hour,minute,false);
    }



    public void onTimeSet(TimePicker view,
                          int hourOfDay, int minute){
        // Variable to hold the current time AM PM Status
        // Initially we set the variable value to AM
        String status = "AM";

        if(hourOfDay > 11){
            // If the hour is greater than or equal to 12
            // Then the current AM PM status is PM
            status = "PM";
        }

        // Initialize a new variable to
        // hold 12 hour format hour value
        int hour_of_12_hour_format;


        if(hourOfDay > 11){
            // If the hour is greater than or equal to 12
            // Then we subtract 12 from the hour
            // to make it 12 hour format time
            hour_of_12_hour_format = hourOfDay - 12;
        }
        else { hour_of_12_hour_format = hourOfDay; }


        // Get the calling activity TextView reference
        TextView tv = (TextView) requireActivity()
                .findViewById(R.id.textView);


        // Display the 12 hour format time in app interface
        tv.setText(hour_of_12_hour_format
                + " : " + minute + " : " + status);
    }
}
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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tool="http://schemas.android.com/tools"
    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>