TimePicker in PreferenceScreen
There is no TimePreference built into Android. However, creating your own is fairly easy. Here's one I did:
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
public static int getHour(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[0]));
}
public static int getMinute(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
@Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());
return(picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}
lastHour=getHour(time);
lastMinute=getMinute(time);
}
}
Android: TimePicker in PreferenceScreen: send data to the TimePicker
You can create custom preference with additional attributes, in your case with limit
attribute.
res/values/attrs.xml
<declare-styleable name="CustomPreference">
<attr name="limit" format="integer"/>
</declare-styleable>
CustomPreference.java
public class CustomPreference extends DialogPreference {
private int limit;
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomPreference, 0, 0);
limit = a.getInteger(R.styleable.CustomPreference_limit, 0);
a.recycle();
}
In preference layout:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.package">
<PreferenceCategory>
<com.example.package.CustomPreference
android:key="@string/pref_key"
android:title="@string/pref_title"
app:limit="6" />
</PreferenceCategory>
</PreferenceScreen>
Also you can set an attribute value programmatically
How to get time from timepicker dialog to preference activity?
Try this below code I used code and checked,the newValue you will get is the time you selected from time preference dialog.
Preference preference = getPreferenceManager().findPreference("timePrefA_Key");
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d("Preference ", "" + preference);
Log.d("newValue", "" + newValue);
return true;
}
});
How to have this type of time picker in Android?
Please have a look at this tutorial . you will get the project over there .
http://www.zainodis.com/2011/05/android-custom-timepicker.html
have a look at this example number picker.. like this you can change it into for time picker.
Android Number Picker Dialog
Android: How to get the time from a TimePicker when it is typed in
I hadn't noticed this problem until i stumbled upon this question.
There is a simple solution:
When the user is done changing the date and time in my app, i simply call finish() and in the onPause() or onStop() or onDestroy() I do this:
// force the timepicker to loose focus and the typed value is available !
timePicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
time.hour = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();
After this i store the time.toMillis(false) value in the appropriate column of my table.
I don't know if the timepicker is still accessible in your onDialogClosed(boolean positiveResult). If not, find another callback to use when it still is.
Hope this helps.
Layout for TimePicker-based DialogPreference
I've figured it out and fixed it. It now works in every level of the API from Cupcake (1.5 - level 3) to Gingerbread (2.3.3 - level 10). Code is below. Please note that the original code is Apache-licensed as specified in the question. I hereby dedicate all of my modifications to the public domain.
package test.android.testproject2;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
protected int lastHour=0;
protected int lastMinute=0;
protected boolean is24HourFormat;
protected TimePicker picker=null;
protected TextView timeDisplay;
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
is24HourFormat = DateFormat.is24HourFormat(ctxt);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
@Override
public String toString() {
if(is24HourFormat) {
return ((lastHour < 10) ? "0" : "")
+ Integer.toString(lastHour)
+ ":" + ((lastMinute < 10) ? "0" : "")
+ Integer.toString(lastMinute);
} else {
int myHour = lastHour % 12;
return ((myHour == 0) ? "12" : ((myHour < 10) ? "0" : "") + Integer.toString(myHour))
+ ":" + ((lastMinute < 10) ? "0" : "")
+ Integer.toString(lastMinute)
+ ((lastHour >= 12) ? " PM" : " AM");
}
}
@Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext().getApplicationContext());
return(picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setIs24HourView(is24HourFormat);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
@Override
public void onBindView(View view) {
View widgetLayout;
int childCounter = 0;
do {
widgetLayout = ((ViewGroup) view).getChildAt(childCounter);
childCounter++;
} while (widgetLayout.getId() != android.R.id.widget_frame);
((ViewGroup) widgetLayout).removeAllViews();
timeDisplay = new TextView(widgetLayout.getContext());
timeDisplay.setText(toString());
((ViewGroup) widgetLayout).addView(timeDisplay);
super.onBindView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
picker.clearFocus();
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
timeDisplay.setText(toString());
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
if (defaultValue==null) {
time="00:00";
}
else {
time=defaultValue.toString();
}
if (shouldPersist()) {
persistString(time);
}
}
String[] timeParts=time.split(":");
lastHour=Integer.parseInt(timeParts[0]);
lastMinute=Integer.parseInt(timeParts[1]);;
}
}
Related Topics
Kill Another Application on Android
Firebaserecycleradapter() Cannot Be Applied to Firebaserecycleradapter
How to Implement Filterable in Realmrecyclerviewadapter
Android Checkbox Listview Select All (Disable/Enable)
Android - Out of Memory Exception When Creating Bitmap
Android Studio - Unable to Merge from Svn Branch
Org.JSON.JSON Exception:End of Input at Character 0
Marking Sms Messages as Read/Unread or Deleting Messages Not Working in Kitkat
Unresolved Reference - Activity Does Not Recognize Synthetic Imports in Android Studio V4
How to Encrypt File from Sd Card Using Aes in Android
Mediacodec and Camera: Colorspaces Don't Match
Intent Action for Network Events in Android Sdk
Edittext: Disable Paste/Replace Menu Pop-Up on Text Selection Handler Click Event
Xamarin.Forms Listview: Set the Highlight Color of a Tapped Item