Format attribute value “android:drawable” not valid

I’m trying to create custom attributes to my button but I dont know which format I must use to images in attributes declaration…

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

    <declare-styleable name="TCButton">
        <attr name="Text" format="string"/>
        <attr name="BackgroundImage" format="android:drawable"  />
    </declare-styleable>


</resources>

Error is in the format=”android:drawable”…

  • Best Android equivalent for iOS Tab Bar Controller
  • How do I keep my screen unlocked during USB debugging?
  • How to pass ArrayList<CustomeObject> from one activity to another?
  • Android Development Tool 23.0.0 and Android L Update error in eclipse
  • Convert Ruby on Rails app to native Android PhoneGap app
  • EditText not editable
  • Related posts:

    How to feed DatePicker widget with other calendar sytems?
    How to use Flipboard effect on Android
    Best practices implementing C2DM registration
    Alternatives to PreferenceFragment with android-support-v4
    How to get Facebook photo, full name, gender using Facebook SDK android
    How to get N text that can be fit on Screen/TextView with a specific size?
  • Is it possible to have a ViewPager inside of a ScrollView?
  • list view position is getting changed when scrolling on android
  • List of Installed App From Google Play Against Google Account
  • Android - WebView = Web page not available
  • AsyncTask, must it take such a performance penalty hit…?
  • change font for editText hint
  • 2 Solutions collect form web for “Format attribute value “android:drawable” not valid”

    You can use format=”integer”, the resource id of the drawable, and AttributeSet.getDrawable(…).

    Here is an example.

    Declare the attribute as integer in res/values/attrs.xml:

    <resources>
        <declare-styleable name="MyLayout">
            <attr name="icon" format="integer" />
        </declare-styleable>
    </resources>
    

    Set the attribute to a drawable id in your layout:

    <se.jog.MyLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        myapp:icon="@drawable/myImage"
    />
    

    Get the drawable from the attribute in your custom widget component class:

    ImageView myIcon;
    //...
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyLayout);
    Drawable drawable = a.getDrawable(R.styleable.MyLayout_icon);
    if (drawable != null)
        myIcon.setBackgroundDrawable(drawable);
    

    To see all options possible check the android src here

    I think it will be better to use it as a simple reference:

    <declare-styleable name="TCButton">
            <attr name="customText" format="string"/>
            <attr name="backgroundImage" format="reference"  />
    </declare-styleable>
    

    And set it in your xml like this:

    <your.package.name.TCButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        custom:customText="Some custom text"
        custom:backgroundImage="@drawable/myImage"
    />
    

    And in your class set the attributes like this:

    public TCButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MembershipItemView, 0, 0);
    
        String customText;
        Drawable backgroundImage;
        try {
            customText = a.getString(R.styleable.TCButton_customText);
            backgroundImage = a.getDrawable(R.styleable.TCButton_backgroundImage);
        } finally {
            a.recycle();
        }
    
        if(!TextUtils.isEmpty(customText)) {
          ((TextView)findViewById(R.id.yourTextView)).setText(customText);
        }
    
        if(null != backgroundImage) {                    
            ((ImageView)findViewById(R.id.yourImageView)).setBackgroundDrawable(backgroundImage);
        }
    }
    

    PS:
    Don’t forget to add this line for the root element of the layout you are using your custom view in

    xmlns:custom="http://schemas.android.com/apk/res-auto"
    

    If you don’t set this, you won’t be able to access your custom attributes.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.