I've implemented a custom seekbar preference, using this site - seekbar preference and it works just fine. Now I want to add values to the seekbar's customized properties from the string.xml file instead of hard-coding them:
Instead of writing customseekbar:unitsRight="Seconds" I want to have a string resource like <string name="units">Seconds</string> and use it like this: customseekbar:unitsRight="@string/units". I've tried to implement this guide. My relevant code is:
attrs.xml
<resources>
<declare-styleable name="CustomSeekBarPreference">
<attr name="unitsRight" format="reference|string"/>
</declare-styleable>
And the constructor -
CustomSeekBarPreference.java
public class CustomSeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener {
private static final String APPLICATIONNS="http://CustomSeekBarPreference.com";
public CustomSeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CustomSeekBarPreference, 0 ,0);
mUnitsRight = a.getString(R.styleable.CustomSeekBarPreference_unitsRight);
a.recycle();
}
and the layout -
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customseekbar="http://CustomSeekBarPreference.com" >
<com.x.sharedpreferencestestapp.CustomSeekBarPreference
android:key="1"
android:defaultValue="30"
android:max="100"
customseekbar:min="0"
android:title="Default step"
customseekbar:unitsRight="@string/units"/>
</PreferenceScreen>
But as you can see, I get 'null' instead of the right value:
Even if I change the value to a fixed string instead of string resource, like customseekbar:unitsRight="Seconds" I still get the same result. And just to make it clear - if I stick to the original code of the seekbar preference: mUnitsRight = getAttributeStringValue(attrs, APPLICATIONNS, "unitsRight", "defaultValue") it works, but not with string resource.
xmlns:customseekbar="http://schemas.android.com/apk/res-auto".min, I get -No resource identifier found for attribute 'min' in package 'com.x.sharedpreferencestestapp'. Does it mean that I have to add a third namespace (and so - how?) or that I must implement values at theattrs.xmlfor all the customed properties?<declare-styleable>. That example is rather odd, in that it's kinda handling the XML attributes "raw". The reasongetAttributeStringValue()works is because it's specifying its own namespace in retrieving the attributes' values. When you useobtainStyledAttributes(), it's using a particular namespace that is basically"http://schemas.android.com/apk/res/" + packageName(res-autois a shortcut), so it won't pull any attributes with your"http://CustomSeekBarPreference.com"namespace.getAttributeStringValue()method to handle string resource references, too, and keep the current setup, but I'd have to do a little research to be sure how that's done, exactly.