Mohanish
Mohanish

Reputation: 469

ImageView src and background

How to change the imageview's src as well as background programatically not in xml?

setBackgroundResource() : same behaviour as setBackground().

But in xml, we got the option to set the src as well as background.

Any help appreciated. Thanks.

Upvotes: 0

Views: 1602

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Try setImageResource() method:

.setImageResource(R.drawable.your_image);

Upvotes: 2

PankajAndroid
PankajAndroid

Reputation: 2707

Below is the code

For Src:

img.setImageResource(R.drawable.icon);

For Background:

img.setBackgroundResource(R.drawable.icon);

Upvotes: 2

SebastianSalvatore
SebastianSalvatore

Reputation: 47

Try this

imageView.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.imageID, null));

https://stackoverflow.com/questions/29041027

Upvotes: 0

Related Questions