title: How to set Maximum and Minimum Time Limit for TimePickerDialog for the user to choose within a certain Hour Range?
body: <p>I have created a normal TimePickerDialog that takes time input from user. But I want to change this so that it only accepts the hour of time within a minimum and maximum hour value. And i want to set the minimum and maximum value dynamically(sent through arguments when it is created or some other way)</p>
  
  <pre><code>public class TimePickerDialogFragment extends DialogFragment {
  
  public static final String EXTRA_TIME = "com.android.XYZ.time";
  
  
  private static final String ARG_TIME = "time";
  
  private TimePicker mTimePicker;
  
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return null;
  }
  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState){
  
      View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_time, null);
  
      mTimePicker = (TimePicker) v.findViewById(R.id.dialog_time_picker);
  
  
      return new AlertDialog.Builder(getActivity()).
              setView(v).
              setTitle("Choose a time").
              setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      if(Build.VERSION.SDK_INT &lt; 23){
                          int getHour = mTimePicker.getCurrentHour();
                          int getMinute = mTimePicker.getCurrentMinute();
                          int getSecond = 0;
  
                          Time time = new Time(getHour,getMinute,getSecond);
  
                          //Date date = new GregorianCalendar(year, month, day).getTime();
                          sendResult(Activity.RESULT_OK, time);
                      } else{
                          int getHour = mTimePicker.getHour();
                          int getMinute = mTimePicker.getMinute();
                          int getSecond = 0;
  
                          Time time = new Time(getHour,getMinute,getSecond);
  
                          //Date date = new GregorianCalendar(year, month, day).getTime();
                          sendResult(Activity.RESULT_OK, time);
  
                      }
  
                  }
              }).create();
  
  }
  
  
  /**
   * To send the chosen time result to Required class thorough intent
   * @param resultCode
   * @param time
   */
  private void sendResult(int resultCode, Time time){
      if(getTargetFragment() == null){
          return;
      }
  
      Intent intent = new Intent();
      intent.putExtra(EXTRA_TIME, time);
  
      getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
  }
  
  
  /**
   * Creating new instance of Time Picker
   * @param time
   * @return
   */
  public static TimePickerDialogFragment newInstance(Time time){
      Bundle args = new Bundle();
      args.putSerializable(ARG_TIME, time);
  
      TimePickerDialogFragment fragment = new TimePickerDialogFragment();
      fragment.setArguments(args);
      return fragment;
  }
  }
  </code></pre>