UPDATE - please see a resolution at the end, all I need now is an explanation. Despite searching I could not find the same question
I created a custom Viewpager and defined a custom attribute in attrs.xml
In the view pager constructor when I try and retrieve the attribute I cannot get a meaningful value. I tried using
abstract int getAttributeIntValue(String namespace, String attribute, int defaultValue)
Return the integer value of 'attribute'.
public MyViewPager(Context context, AttributeSet attrs) {
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeUnsignedIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","view_pager_type"));
Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","garbage"));
As you can see the attrs.getAttributeValue did a string but it is not meaningful to me.
This is the output from the log statements - 9 is the default if not found, I do not undertand what the @213 number is
ViewPagerType:9
ViewPagerType:9
ViewPagerType:@2131230723
ViewPagerType:null
This is my attrs.xml
<resources>
<declare-styleable name="AutoResizeTextView">
<attr name="ttf_name" format="string"/>
</declare-styleable>
<declare-styleable name="ViewPagerType">
<attr name="view_pager_type" format="integer"/>
</declare-styleable>
</resources>
This is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.rh.ellierides.MyViewPager xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textPager"
app:view_pager_type="@integer/text_viewpager"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="24"
tools:context=".Main">
This is integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="text_viewpager">1</integer>
<integer name="image_viewpager">2</integer>
</resources>
I think I will just create two separate viewpagers but I would still like to understand this.
Update I found a resolution but do not understand why the first one did not work so please still answer
TypedArray a = null;
try {
a = getContext().obtainStyledAttributes(attrs, R.styleable.ViewPagerType);
int viewpagerType = a.getInt(R.styleable.ViewPagerType_view_pager_type, 9);
Log.d(Constant.TAG, "viewpagerType:" + viewpagerType);
} finally {
if (a != null) {
a.recycle(); // ensure this is always called
}
}