Damir
Damir

Reputation: 56199

Android widget for choosing time

Is there in android already some widget for choosing time which doesn't show popup like TimePicker? I have criteria for choosing time in popup so it looks weird to use Time Picker inside popup and open new.

Upvotes: 0

Views: 231

Answers (2)

Mobius
Mobius

Reputation: 699

When writing my first app, I could not find one. I ended up using 3 spinners in a RelativeLayout, hour, minute, and am/pm. Then just have accept button that pulls the values from each and build your time.

    final Spinner hourSpinner = (Spinner) findViewById(R.id.hourSpinner);
    ArrayAdapter<CharSequence> hourAdapter = ArrayAdapter.createFromResource(
            this, R.array.editHourArray, android.R.layout.simple_spinner_item);
    hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hourSpinner.setAdapter(hourAdapter);
    hourSpinner.setSelection(myHour - 1);

    final Spinner minuteSpinner = (Spinner) findViewById(R.id.minuteSpinner);
    ArrayAdapter<CharSequence> minuteAdapter = ArrayAdapter.createFromResource(
            this, R.array.editMinuteArray, android.R.layout.simple_spinner_item);
    minuteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    minuteSpinner.setAdapter(minuteAdapter);
    minuteSpinner.setSelection(myMinute);

    final Spinner am_pmSpinner = (Spinner) findViewById(R.id.am_pmSpinner);
    ArrayAdapter<CharSequence> am_pmAdapter = ArrayAdapter.createFromResource(
            this, R.array.editam_pmArray, android.R.layout.simple_spinner_item);
    am_pmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    am_pmSpinner.setAdapter(am_pmAdapter);
    am_pmSpinner.setSelection(my_am_pm);

Then in the onclick of the accept button

 myNewHour = Integer.valueOf(hourSpinner.getSelectedItem().toString());
 myNewMinute = Integer.valueOf(minuteSpinner.getSelectedItem().toString());
 String tempAm_Pm = am_pmSpinner.getSelectedItem().toString();

And build your time with whatever you are using (I was using calendar because I had dates also)

Upvotes: 1

Carlos Silva
Carlos Silva

Reputation: 544

No, not really. If you have to a custom "business logic" on your time picker, you should create one "by hand" and not using TimePicker

Upvotes: 1

Related Questions