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>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQKLgF7-Hu_YmQe92MiCfjt_3pp3J6nkeOkjvwPZ761awvwnasXVZ4Y0kR4CEPhEi_RrAD-meEJl2K6vAHr-fd_pott5AiR1D0x0R7OaND8wLPEQRBPkHGQpV27CQmdp3DlNwQCbMRBvKxC9BviO5ZkJjIn6Exm7vDQEkN6-R3nyYxYfWDjDiTb06Y1A/s1600/android_get_am_pm_from_time_picker_dialog.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0TRo0HuIv45KL46TAolV6hSkEiIZ6W0dDe0XvkFFK5JB6bnxMRNzhHKrCx4Yh0EBipwaP5pQil4-_OPkuKhaEQyk2vAVOSur37LfVh7wgzqvuo-Cal1xaUysCl0B0yBKGhQKLKIVO4phWxz2f6F0uZECVRS6tb_UJKOo9EtPNw9ZKy7bVU7TxZYa4uQ/s1600/android_get_am_pm_from_time_picker_dialog2.png)