14


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?

Harshil
  • 914
  • 1
  • 12
  • 26
  • Possible duplicate of http://stackoverflow.com/questions/27834989/hide-year-from-date-picker-widget – Cory Roy Jan 11 '17 at 16:59
  • You can't , you have to create you own custom wheel http://stackoverflow.com/questions/7643532/how-to-hide-or-disable-minutes-widget-in-android-timepickerdialog – Shubham Sharma Jan 11 '17 at 17:24

4 Answers4

5

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

    ...
}
Harshil
  • 914
  • 1
  • 12
  • 26
  • 1
    Great library...It's incredible how the android libraries lack stuff as basic as disable certain time ranges. Thanks for sharing... – Ramiro G.M. Aug 29 '20 at 20:05
5

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

Ahmed Fahmy
  • 69
  • 1
  • 4
2

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 :

enter image description here

Sahitya Pasnoor
  • 597
  • 5
  • 11
  • For some reason I can't access `mAllowAutoAdvance` despite the delegate instance holding the field name in the debugger. – Dmytro Puzak May 01 '23 at 18:32
1

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

Community
  • 1
  • 1
Cory Roy
  • 5,379
  • 2
  • 28
  • 47