In this tutorial, you are going to learn how to make a time picker dialog with an android studio. And I have also said you the reason where you will face problem while generating the Signed apk for google marketplace. The benefit of using time picker dialog is that user will not face any problem while giving the time input. Otherwise, there might be an inconsistency while entering the time to the database. And after all, it also looks beautiful to enter the time and widely supported by most of the devices. This time picker dialog works as a pop-up on pressing the Edittext or Textview in android studio user will get a pop-up window to enter the time that too in AM or PM.


  1. First of all, open up a new project in the android studio and select the blank project to start Gradle build and generate an XML file.
  2. After successfully generating XML file move on to the XML file and add Textview to the XML file and assign it an appropriate id as shown in the video tutorial.
  3. Now open up file to create the instance of the Textview which we have already created in the XML file, after that mention the id in the java file which we have already assigned in the XML file.
  4. Now create a new class as TimeDialog which will act as the popup in the application asking the user for the time input.
  5. After successfully creating the class TimeDialog extends it with DialogFragment and also implement it with TimePickerDialog.OnTimeSetListener we are doing so because it will create a Fragment which will get executed when the user clicks on the Textview.
  6. So now you will face error if you will pass the value from the to the TimeDialog class. You will face the error while generating the Signed Apk only but while generating unsigned Apk you will not face any problem. So the solution is to create another function in the TimeDialog class where you will pass the value from the class.
  7. So now in the TimeDialog class create the instance of Textview as I am dealing with Textview that's why am creating Textview because am going to pass the view from the class. As am passing the View of the Textview so am also creating the instance of the Textview in the TimeDialog class.
  8. Now create a function to get the view from the class I have created one with the name get(View view) function which will get the view from the class and inside that get function to initialize the view to the instance of the Textview which we have assigned in the TimeDialog.
  9. Again create two new functions named as onCreateDialog and OnTimeSet in which onCreateDialog will pass the time to the OnTimeSet function and OnTimeSet function will pass the value to the which will further put that in the Textview created in the XML file.
  10. Now there is a predefined command called as Calendar which will give the output of the date and time but we only want the time from this predefined command.
  11. So now it's time to define an object through which we will access the time from the Calendar and after that create two integer datatype variables named as an hour and minute where we will put the hour and minute obtained from the Calendar. We will access the hour through the object as the "HOUR_OF_DAY" and for the minute it is "MINUTE".
  12. after defining this change the return of the onCreateDialog to TimePickerDialog because we need to pass these values to the class which will further put that selected value in the Textview previously created in the XML file.
  13. Now we have to change the argument of the TimePickerDialog to "view, hour, minute" which will receive the hour minute from the onCreateDialog.
  14. After getting those values from the onCreateDialog now define a string named as the time which will concatenate this two hour and minute separated by a colon.
  15. After concatenating both the hour and minute to the String time now its time to set the time to the Textview in the XML file, for that simply write 'instance of Textview.SetText("time")'.
  16. Now we have finished up coding with the TimeDialog now open class and assign a setOnClickListener to the Textview so that when user will click on that Textview this TimeDialog will execute and there will be a pop-up to choose the time.
  17. Inside that setOnClickListener function create an object of TimeDialog to access the class from class.
  18. After creating the object of the TimeDialog now through the object pass the view to the get() assigned inside the TimeDialog class.
  19. Now create another object of the FragmentTransaction because we are going to pop-up the TimeDialog for that we need FragmentTransaction to run that Fragment TimeDialog and remember your FragmentTransaction must be of "".
  20. now from the object created for the TimeDialog now call the show() through the object to show the transaction.
  21. Now you are all set with the code now its time to execute the program and generate a Signed Apk.

Code:- -
package com.example.nigamasis.beginnertopro;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView time;

protected void onCreate(Bundle savedInstanceState) {

time = (TextView)findViewById(;

time.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Timedialog timedialog = new Timedialog();
timedialog.get(view); fragmentTransaction = getFragmentManager().beginTransaction();,"TimePicker");

} - 
package com.example.nigamasis.beginnertopro;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

* Created by NIGAMASIS on 04-Jan-17.

public class Timedialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

TextView tv;

public void get(View view)
tv = (TextView)view;

public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

return new TimePickerDialog(getActivity(),this,hour,minute,false);

public void onTimeSet(TimePicker view, int hourofday, int minute) {
String time = hourofday+":"+minute;

XML File:-

Video Tutorial:-