Yesterday we are facing issues regarding the Custom TimePickerDialog,our main requirement was adjustment in minute field of TimePickerDialog as per user requirement.And here is our solution.
import java.util.Calendar; import android.app.Activity; import android.app.TimePickerDialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.TimePicker; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void timeAction(View v) { CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog( this, timeSetListener, Calendar.getInstance() .get(Calendar.HOUR), CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance() .get(Calendar.MINUTE) + CustomTimePickerDialog.TIME_PICKER_INTERVAL), false); timePickerDialog.setTitle("2. Select Time"); timePickerDialog.show(); } private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } };// using CustomTimePickerDialog } class CustomTimePickerDialog extends TimePickerDialog { public static final int TIME_PICKER_INTERVAL = 15; private boolean mIgnoreEvent = false; public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) { super(context, callBack, hourOfDay, minute, is24HourView); } @Override public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) { super.onTimeChanged(timePicker, hourOfDay, minute); this.setTitle("2. Select Time"); if (!mIgnoreEvent) { minute = getRoundedMinute(minute); mIgnoreEvent = true; timePicker.setCurrentMinute(minute); mIgnoreEvent = false; } } public static int getRoundedMinute(int minute) { if (minute % TIME_PICKER_INTERVAL != 0) { int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL); minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0); if (minute == 60) minute = 0; } return minute; } }