Android - Disable (or Hide) Minute selection in TimePicker Dialog
This fixed my issue with native time picker component
1. disabling minute selection
2. hiding time separator
3. Disabling transition from hour to a minute when the user selects hours
The code is in kotlin.
// Disbale time picker minutes
time_picker.findViewById<View>(Resources.getSystem()
.getIdentifier("minutes", "id", "android"))
.visibility = View.GONE
// Disbale time seperator
time_picker.findViewById<View>(Resources.getSystem()
.getIdentifier("separator", "id", "android"))
.visibility = View.INVISIBLE
val delegateField = time_picker.javaClass.getDeclaredField("mDelegate")
delegateField.isAccessible = true;
val autoAdvance = delegateField.get(time_picker)
val autoAdvanceField = autoAdvance.javaClass.getDeclaredField("mAllowAutoAdvance")
autoAdvanceField.isAccessible = true
autoAdvanceField.set(autoAdvance, false)
Output :
Without library and simple time picker
time_picker.setIs24HourView(true)
time_picker.currentMinute = 0
time_picker.currentHour = 0
time_picker.findViewById<View>(Resources.getSystem()
.getIdentifier("hour", "id", "android"))
.visibility = View.GONE
change id identifier with what you need to hide or show
I solved my problem by using Material TimePickerDialog Library.
My solution:
public class home extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, ... {
...
private EditText time;
private TimePickerDialog timePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
...
timePickerDialog = TimePickerDialog.newInstance(home.this, new Date().getHours(), new Date().getMinutes(), false);
timePickerDialog.enableMinutes(false);
time = (EditText) findViewById(R.id.time);
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePickerDialog.show(getFragmentManager(), "TimePickerDialog");
}
});
}
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
String form;
if (hourOfDay > 12) {
hourOfDay = hourOfDay - 12;
form = "PM";
} else {
if (hourOfDay == 0) {
hourOfDay = 12;
}
form = "AM";
}
time.setText(String.valueOf(hourOfDay) + " " + form);
}
...
}
Short answer is that you can't. You'll have to create your own custom dialog.
Long answer is that you might be able to, but you shouldn't. You could find the id of the view and disable or hide it, but each version and manufacture may use different ids for this dialog so you will run into many issues and bugs.
StackOverflow answer about hiding year