How to set ImageButton property of app:srcCompat="@drawable/pic" programmatically?

unknown attribute app:srccompat
app:srccompat error in android
set vector drawable programmatically android
srccompat png
is incompatible with attribute 'srccompat attr reference weak
appcompatimageview
appcompatimageview set image resource
vector drawable set true

How to set the ImageButton property of

app:srcCompat="@drawable/pic"

programmatically?

Something like myImageButton.setBackgroundResource(R.drawable.eng2); but the property of app:srcCompat.

You need to use setImageResource() method.

imageButton.setImageResource(R.drawable.eng2);

How to set ImageButton property of app:srcCompat=“@drawable/pic , How to set the ImageButton property of app:srcCompat="@drawable/pic". programmatically? Something like myImageButton. Questions: The title said itself, how to set ImageButton property of app:srcCompat=”@drawable/pic” programmatically? Something like myimagebutton.setBackgroundResource(R.drawable.eng2); but the property of app:srCompat Can you please help.

First, ensure you are dealing with AppCompatImageView, not "normal" ImageView:

AppCompatImageView iv = (AppCompatImageView)findViewById(....);

then you can use

iv.setImageResource(R.drawable.pic);

See other public methods in docs.

Is there a difference between android:src and app:srcCompat , Android:src would be the attribute for you if you were to go with a png drawable. You can use these attributes on ImageButtons and ImageViews (  How to set the ImageButton property of app: srcCompat = "@drawable/pic". programmatically? Something like myImageButton.setBackgroundResource(R.drawable.eng2); but the property of app:srcCompat.

Using setImageResource() should fetch its resource in a backwards-compatible manner without any further effort required.

However if you are using setImageDrawable(), the ImageView/ImageButton will not help with any backwards compat and it's up to you to supply a backward-compat drawable, which is important for eg. if using VectorDrawables. The following will load and set a drawable in such a way:

val drawable = AppCompatResources.getDrawable(context, resourceId)
image.setImageDrawable(drawable)

Using vector assets in Android apps - Android Developers, In this post we'll dive into how to use them in your apps. ImageView , ImageButton : Don't: android:src; Do: app:srcCompat places you may want to specify drawables on views that don't offer compat attributes (e.g. there's  android - How to set ImageButton property of app:srcCompat=“@drawable/pic” programmatically? How to set the ImageButton property of app:srcCompat="@drawable/pic" programmatically? Something like myImageButton.setBackgroundResource(R.drawable.eng2); but the property of app:srcCompat.…

I guess other methods are outdated and wouldn't work at the time of writing this,

iv_yourImageView.setImageDrawable(getResources().getDrawable(R.drawable.your_drawable));

This will work.

ImageButton, The image on the surface of the button is defined either by the android:src attribute in the <ImageButton> XML element or by the ImageView. An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children. This shows a button with an image (instead of text) that can be pressed or clicked by the user. Android button style set ImageButton Attributes. Following are the important attributes related to ImageButton control.

Floating Action Buttons, A floating button for the primary action in an application. Floating action buttons adjust their position and visibility in response to other UI Icon, app:srcCompat the value set using FloatingActionButton#setSize or the app:fabSize attribute. Here's what worked for me in setting the image:src on an ImageButton programmatically** or through code: 1.Retrieve the image drawable. Drawable tempImage = getResources().getDrawable(R.drawable.my_image); 2.Get the view. ImageButton tempButton = (ImageButton)findViewById(R.id.my_image_button); 3.Set the image for the view.

Can't use srcCompat for ImageViews in android – Website Information, But I can't use the srcCompat attribute for my imageview. “app" found for tag Imageview, when I add app:srcCompat="@drawable/wallpaper". InflateException: Binary XML file line #15: Error inflating class ImageButton. To use VectorDrawableCompat within an ImageButton or ImageView, use the app:srcCompat XML attribute or setImageResource() method. To keep referencing attribute IDs on API level 20 or lower, add the following appt flag to your build,gradle file:

docs-archive/Recipes/android/controls/imagebutton , Next, edit the layout file of the activity, and add a new ImageButton widget to the layout. Set the app:srcCompat attribute to the SVG resource  If you’re changing drawables at runtime, you’ll be able to use the same setImageResource() method as before - no changes there. Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

Comments
  • I'm using 'ImageButton' with 'app:srcCompat="@drawable/pic"' but setting to 'setImageResource(R.drawable.pic);' works fine. Thank you
  • build tools are probably replacing classes "in fly"
  • hey Marcin, do you know why we need to programmatically change the image instead of giving the path between app:srcCompa="drawable/"
  • I want to add to this answer, I had to use AppCompatImageButton instead of ImageButton.
  • @Zhang That probably because you the activity where the view was inflated didn't extend AppCompatActivity