Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format attribute value "android:drawable" not valid

Tags:

android

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"...

like image 999
Tiago Costa Avatar asked Aug 15 '10 11:08

Tiago Costa


People also ask

How do you reference drawable android?

In your Android/Java source code you can also refer to that same image like this: Resources res = getResources(); Drawable drawable = res. getDrawable(R.

What are Drawables in android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is stroke in android XML?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.

Which function is used to load a drawable image resource?

Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);


2 Answers

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

like image 58
JOG Avatar answered Sep 21 '22 13:09

JOG


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.

like image 28
Ionut Negru Avatar answered Sep 21 '22 13:09

Ionut Negru