Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Custom View attributes are not accessible in xml android

I am trying to set drawable in xml of my CustomView. but it is not showing my app:enableProgressRes="@drawable/vbequalizer_bg" or customname:enableProgressRes="@drawable/vbequalizer_bg"

I mean custom attributes are not included. As we hit Ctrl+Space android:attr shows.

And i have tried including these namespaces one by one to the Root Layout.

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:customname="http://schemas.android.com/apk/res/zare.custom.views"

But my attributes aren't accessible in xml. by anyOne of them

This answer says that the Problem is resolved after ADT 17.

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="VolumnBar">
        <attr name="disableProgressRes" format="integer" />
        <attr name="enableProgressRes" format="integer" />
    </declare-styleable>
</resources>

CustomView(VolumeBar) Constructor

public VolumnBar(Context context, AttributeSet attributeset)
{
    super(context, attributeset);

    if(!isInEditMode())
    {
        TypedArray a = context.obtainStyledAttributes(attributeset, R.styleable.VolumnBar);//VolumnBar same name as Class Name

        try
        {
            drawableProgress = a.getDrawable(R.styleable.VolumnBar_disableProgressRes);

            drawableInvalidateProgress = a.getDrawable(R.styleable.VolumnBar_enableProgressRes);
        }
        catch (Exception e)
        {

        }
        finally
        {
            a.recycle();
        }
    }
}

Upvotes: 1

Views: 1105

Answers (1)

ivagarz
ivagarz

Reputation: 2444

The proper format for drawable resources is reference, no integer. Change your attribute definitions to something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="VolumnBar">
        <attr name="disableProgressRes" format="reference" />
        <attr name="enableProgressRes" format="reference" />
    </declare-styleable>
</resources>

Upvotes: 1

Related Questions