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"...
In your Android/Java source code you can also refer to that same image like this: Resources res = getResources(); Drawable drawable = res. getDrawable(R.
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.
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.
Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With