• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to access android:tag XML attribute from code?

I set the tag attribute on a custom widget in my layout XML:

Code:
<com.louisvillemade.thegirlyapp.widgets.GirlySeekBar
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="40dp"
 android:layout_marginRight="40dp"
 android:layout_marginBottom="16dp"
 android:gravity="center"
 android:layout_gravity="center"
 android:max="19"
[B] android:tag="attr_2"[/B]
 android:progress="0"
 android:secondaryProgress="0"
 android:id="@+id/slider2"
 android:progressDrawable="@xml/progress_drawable_0"
 android:thumb="@xml/thumb_drawable_0"/>

Inside my GirlySeekBar class I implement some listener methods where I can set properties just fine:
Code:
    @Override
    public void onProgressChanged(SeekBar v, int progress, boolean isUser) {
        if(progress < 25) {
            LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.xml.progress_drawable_0);
            v.setProgressDrawable(ld);
            v.setThumb(getResources().getDrawable(R.xml.thumb_drawable_0));
        }
        if(progress > 25 && progress < 50) {
            LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.xml.progress_drawable_1);
            v.setProgressDrawable(ld);
            v.setThumb(getResources().getDrawable(R.xml.thumb_drawable_1));
        }
        if(progress > 50 && progress < 75) {
            LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.xml.progress_drawable_2);
            v.setProgressDrawable(ld);
            v.setThumb(getResources().getDrawable(R.xml.thumb_drawable_2));
        }
        if(progress > 75) {
            LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.xml.progress_drawable_3);
            v.setProgressDrawable(ld);
            v.setThumb(getResources().getDrawable(R.xml.thumb_drawable_3));
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // How to access android:tag?
    }

How do I grab some of the XML elements, like the android:tag element (set to "attr_2") through reflection of seekBar? Calling seekBar.getTag() returns null and I can't seem to find any methods on the Drawable that will give me what I want.

Thanks in advance!
 
I solved my problem-- in the constructor of my custom SeekBar I was not passing the AttributeSet to the super constructor. After resolving this I now get my values in code from the android:tag in my layout XML.
 
Back
Top Bottom