In my project, I am using TimePicker dialog to allow users to select time. But I don't want them to select minutes. So, all I want is to disable (or hide) minute selection.
So, How to do that?
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);
}
...
}
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
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
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With