Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

Tags:

I have seen these different approaches in setting images but I don't get the difference.

Why there two methods?

like image 600
Lukap Avatar asked Aug 28 '11 14:08

Lukap


2 Answers

setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView. Given:

ImageView iv = new ImageView(this); 

Then:

iv.setBackgroundResource(R.drawable.imagedata); 

Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.

imageView.setImageResource(R.drawable.imagedata); 

Will occupy only the size of the image in ImageView. For that you want to also set

android:layout_width="wrap_content" android:layout_height="wrap_content" 

for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.

like image 119
Dimitris Makris Avatar answered Sep 21 '22 02:09

Dimitris Makris


SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.

so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example . This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).

like image 25
user370305 Avatar answered Sep 25 '22 02:09

user370305