Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Disable (or Hide) Minute selection in TimePicker Dialog


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?

like image 762
Harshil Avatar asked Jan 11 '17 16:01

Harshil


4 Answers

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

    ...
}
like image 125
Harshil Avatar answered Nov 03 '22 23:11

Harshil


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

like image 40
Ahmed Fahmy Avatar answered Nov 04 '22 00:11

Ahmed Fahmy


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

like image 1
Cory Roy Avatar answered Nov 04 '22 01:11

Cory Roy


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

like image 1
Sahitya Pasnoor Avatar answered Nov 04 '22 01:11

Sahitya Pasnoor