I have began to develop a Custom Preference:
<com.package.lib.android.preference.CustomPreference
xmlns:customPreference="http://schemas.android.com/apk/src/com.package.lib.android.preference.CustomPreference"
android:key="test"
android:title="test"
customPreference:minValue="4" />
The associated class:
public class CustomPreference extends DialogPreference {
private Context mContext;
private LayoutInflater mLayoutInflater;
public CustomPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
mContext = context;
mLayoutInflater = LayoutInflater.from(mContext);
// how can I access customPreference:minValue?
}
}
In my CustomPreference
class I want to access customPreference:minValue
. How is that possible?
I got it working.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customPreference="http://schemas.android.com/apk/res-auto" >
<com.package.lib.android.preference.CustomPreference
android:defaultValue="@integer/font_size_default_value"
android:key="test"
android:title="title"
customPreference:minValue="@integer/font_size_min_value"
customPreference:maxValue="@integer/font_size_max_value" />
Create a attrs.xml
file in res/values
folder with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomPreference">
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
</declare-styleable>
</resources>
In the preference class you can get the attributes using:
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomPreference);
Log.i(TAG, String.valueOf(a.getInt(R.styleable.CustomPreference_minValue, 0)));
Log.i(TAG, String.valueOf(a.getInt(R.styleable.CustomPreference_maxValue, 100)));
a.recycle();