Android - Timepicker Minutes To 15
Can I control the Android TimePicker view to just show the minutes to 15 minutes interval? Meaning if it's 12:28 now, show 12:30 and clicking the + and - button will increment and
Solution 1:
Here's my version where you can set the interval:
privatestaticfinalint TIME_PICKER_INTERVAL=15;
privateboolean mIgnoreEvent=false;
private TimePicker.OnTimeChangedListener mTimePickerListener=newTimePicker.OnTimeChangedListener(){
publicvoidonTimeChanged(TimePicker timePicker, int hourOfDay, int minute){
if (mIgnoreEvent)
return;
if (minute%TIME_PICKER_INTERVAL!=0){
int minuteFloor=minute-(minute%TIME_PICKER_INTERVAL);
minute=minuteFloor + (minute==minuteFloor+1 ? TIME_PICKER_INTERVAL : 0);
if (minute==60)
minute=0;
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;
}
}
};
Solution 2:
Create a xml file and name it activity_time_picker.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TimePickerandroid:id="@+id/timePicker1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/layoutHourMinute"android:layout_centerInParent="true"
/></RelativeLayout>
now create activity class like this
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.NumberPicker;
import android.widget.TimePicker;
publicclassTimePickerActivityextendsActivity {
TimePicker timePicker;
privateintTIME_PICKER_INTERVAL=15;
NumberPicker minutePicker;
List<String> displayedValues;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_time_picker);
super.onCreate(savedInstanceState);
timePicker = (TimePicker)findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(0);
timePicker.setCurrentMinute(0);
setTimePickerInterval(timePicker);
}
@SuppressLint("NewApi")privatevoidsetTimePickerInterval(TimePicker timePicker) {
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
// Field timePickerField = classForid.getField("timePicker"); Fieldfield= classForid.getField("minute");
minutePicker = (NumberPicker) timePicker
.findViewById(field.getInt(null));
minutePicker.setMinValue(0);
minutePicker.setMaxValue(3);
displayedValues = newArrayList<String>();
for (inti=0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
// for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {// displayedValues.add(String.format("%02d", i));// }
minutePicker.setDisplayedValues(displayedValues
.toArray(newString[0]));
minutePicker.setWrapSelectorWheel(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Solution 3:
The following worked for me.
First, in onCreate:
pickStartTime = (TimePicker)findViewById(R.id.StartTime);
pickStartTime.setOnTimeChangedListener(mStartTimeChangedListener);
Set the OnTimeChangedListener:
private TimePicker.OnTimeChangedListenermStartTimeChangedListener=newTimePicker.OnTimeChangedListener() {
publicvoidonTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(view, startDate, hourOfDay, minute);
}
};
Null OnTimeChangedListener (explained in comments in updateDisplay below):
private TimePicker.OnTimeChangedListenermNullTimeChangedListener=newTimePicker.OnTimeChangedListener() {
publicvoidonTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
};
privatevoidupdateDisplay(TimePicker timePicker, Date date, int hourOfDay, int minute) {
// do calculation of next time intnextMinute=0;
if (minute >= 45 && minute <= 59)
nextMinute = 45;
elseif(minute >= 30)
nextMinute = 30;
elseif(minute >= 15)
nextMinute = 15;
elseif(minute > 0)
nextMinute = 0;
else {
nextMinute = 45;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(mNullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(nextMinute);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(mStartTimeChangedListener);
// update the date variable for use elsewhere in code
date.setMinutes(nextMinute);
}
Solution 4:
You have to write your own TimePicker. Not sure if you can extend the existing TimePicker and handle the button events on your own.
Solution 5:
I think I figured it out. Might be ghetto way but...
Here is what I did.
start_time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
publicvoidonTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(hourOfDay, minute);
}
});
privatevoidupdateDisplay(int hourOfDay, int minute) {
// do calculation of next time// nextTime = calculation of next time;// the processed boolean is to prevent infinite loopif (!processed) {
start_time.setCurrentMinute(nextTime);
processed = true;
} else {
processed = false;
}
}
Post a Comment for "Android - Timepicker Minutes To 15"