Last active
August 29, 2015 14:06
-
-
Save bsara/16fbed97cd7d785e2d5f to your computer and use it in GitHub Desktop.
Android Preference for Selecting Hour of Day (12/24 Hour Clock Compatible)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2015 Brandon Sara (http://bsara.github.io) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
import android.content.Context; | |
import android.preference.DialogPreference; | |
import android.text.format.DateFormat; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.NumberPicker; | |
public class HourOfDayPreference extends DialogPreference { | |
private String[] _hourStrings; | |
private String[] _ampmStrings; | |
private NumberPicker _hourView; | |
private NumberPicker _ampmView; | |
private boolean _isValueSet; | |
private int _value; | |
// region Constructors | |
public HourOfDayPreference(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.init(); | |
} | |
public HourOfDayPreference(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
this.init(); | |
} | |
private void init() { | |
_hourStrings = this.getContext().getResources().getStringArray(DateFormat.is24HourFormat(this.getContext()) ? R.array.customPref_hourOfDay_hours_24 : R.array.customPref_hourOfDay_hours_12); | |
_ampmStrings = this.getContext().getResources().getStringArray(R.array.customPref_hourOfDay_ampm); | |
_hourView = null; | |
_ampmView = null; | |
_isValueSet = false; | |
_value = 0; | |
this.setDialogLayoutResource(R.layout.pref_hourofday); | |
} | |
// endregion | |
// region Event Handlers | |
@Override | |
protected void onBindDialogView(View view) { | |
super.onBindDialogView(view); | |
final boolean isUsing24HourFormat = DateFormat.is24HourFormat(this.getContext()); | |
_hourView = (NumberPicker)view.findViewById(R.id.customPref_hourOfDay_hour); | |
_hourView.setSaveFromParentEnabled(false); | |
_hourView.setSaveEnabled(true); | |
_hourView.setMinValue(0); | |
_hourView.setMaxValue(isUsing24HourFormat ? 23 : 11); | |
_hourView.setDisplayedValues(_hourStrings); | |
_ampmView = (NumberPicker)view.findViewById(R.id.customPref_hourOfDay_ampm); | |
_ampmView.setSaveFromParentEnabled(false); | |
_ampmView.setSaveEnabled(true); | |
if (isUsing24HourFormat) { | |
_ampmView.setVisibility(View.GONE); | |
} else { | |
_ampmView.setVisibility(View.VISIBLE); | |
_ampmView.setMinValue(0); | |
_ampmView.setMaxValue(1); | |
_ampmView.setDisplayedValues(_ampmStrings); | |
if (_value > 11) { | |
_hourView.setValue(_value - 12); | |
_ampmView.setValue(1); | |
return; | |
} | |
_ampmView.setValue(0); | |
} | |
_hourView.setValue(_value); | |
} | |
@Override | |
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { | |
if (defaultValue == null || !(defaultValue instanceof Integer)) { | |
defaultValue = 0; | |
} | |
if (restorePersistedValue) { | |
_value = this.getPersistedInt((Integer)defaultValue); | |
_isValueSet = true; | |
return; | |
} | |
this.setValue((Integer)defaultValue); | |
} | |
@Override | |
protected void onDialogClosed(boolean positiveResult) { | |
super.onDialogClosed(positiveResult); | |
if (positiveResult) { | |
int hour = _hourView.getValue(); | |
if (!DateFormat.is24HourFormat(this.getContext()) && _ampmView.getValue() == 1) { | |
hour += 12; | |
} | |
if (this.callChangeListener(hour)) { | |
this.setValue(hour); | |
} | |
} | |
} | |
// endregion | |
// region Getters/Setters | |
public int getValue() { | |
return _value; | |
} | |
public void setValue(int newValue) { | |
final boolean wasChanged = (newValue != _value); | |
if (wasChanged || !_isValueSet) { | |
_value = newValue; | |
_isValueSet = true; | |
this.persistInt(_value); | |
if (wasChanged) { | |
this.notifyChanged(); | |
} | |
} | |
} | |
@Override | |
public CharSequence getSummary() { | |
if (DateFormat.is24HourFormat(this.getContext())) { | |
return _hourStrings[ _value ]; | |
} | |
if (_value > 11) { | |
return String.format("%s %s", _hourStrings[ _value - 12 ], _ampmStrings[ 1 ]); | |
} | |
return String.format("%s %s", _hourStrings[ _value ], _ampmStrings[ 0 ]); | |
} | |
// endregion | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
The MIT License (MIT) | |
Copyright (c) 2015 Brandon Sara (http://bsara.github.io) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
--> | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center"> | |
<NumberPicker | |
android:id="@+id/customPref_hourOfDay_hour" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="2dp" /> | |
<NumberPicker | |
android:id="@+id/customPref_hourOfDay_ampm" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="2dp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
The MIT License (MIT) | |
Copyright (c) 2015 Brandon Sara (http://bsara.github.io) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
--> | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string-array name="customPref_hourOfDay_hours_12"> | |
<item>12:00</item> | |
<item>1:00</item> | |
<item>2:00</item> | |
<item>3:00</item> | |
<item>4:00</item> | |
<item>5:00</item> | |
<item>6:00</item> | |
<item>7:00</item> | |
<item>8:00</item> | |
<item>9:00</item> | |
<item>10:00</item> | |
<item>11:00</item> | |
</string-array> | |
<string-array name="customPref_hourOfDay_hours_24"> | |
<item>00:00</item> | |
<item>01:00</item> | |
<item>02:00</item> | |
<item>03:00</item> | |
<item>04:00</item> | |
<item>05:00</item> | |
<item>06:00</item> | |
<item>07:00</item> | |
<item>08:00</item> | |
<item>09:00</item> | |
<item>10:00</item> | |
<item>11:00</item> | |
<item>12:00</item> | |
<item>13:00</item> | |
<item>14:00</item> | |
<item>15:00</item> | |
<item>16:00</item> | |
<item>17:00</item> | |
<item>18:00</item> | |
<item>19:00</item> | |
<item>20:00</item> | |
<item>21:00</item> | |
<item>22:00</item> | |
<item>23:00</item> | |
</string-array> | |
<string-array name="customPref_hourOfDay_ampm"> | |
<item>AM</item> | |
<item>PM</item> | |
</string-array> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment