Introduction:-

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.

Procedure:-

  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 MainActivity.java 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 MainActivity.java 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 MainActivity.java 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 MainActivity.java 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 MainActivity.java class I have created one with the name get(View view) function which will get the view from the MainActivity.java 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 MainActivity.java 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 MainActivity.java 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 MainActivity.java 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 mainActivity.java 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 "V4.app.Fragment".
  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:-
MainActivity.java -
package com.example.nigamasis.beginnertopro;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView time;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

time = (TextView)findViewById(R.id.time_picker);

time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Timedialog timedialog = new Timedialog();
timedialog.get(view);
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
timedialog.show(fragmentTransaction,"TimePicker");
}
});

}
}

TimeDialog.java - 
package com.example.nigamasis.beginnertopro;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
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;
}

@Override
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);
}

@Override
public void onTimeSet(TimePicker view, int hourofday, int minute) {
String time = hourofday+":"+minute;
tv.setText(time);
}
}

XML File:-
activity_main.xml

Video Tutorial:-

THANKS FOR WATCHING THIS VIDEO TUTORIAL
PLEASE LIKE,SHARE AND SUBSCRIBE
HAVE A NICE DAY...