android

Thursday, 20 October 2016

Difference between ImageView.setBackgroundResource and ImageView.setImageResource in android


In android there are two methods of ImageView to set image.


 1. ImageView.setBackgroundResource and
 2. ImageView.setsetImageResource.


                          Let's try to understand the difference between these two methods.
  The first method ImageView.setBackgroundResource is use to set the background of an     ImageView.

  The second method ImageView.setImageResource is use  to set the src image of the           ImageView. 

   for example:
          ImageView iv=(ImageView)findViewById(R.id.imageview1);
          iv.setBackgroundResource(R.drawable.yourimagename);
  
   It will fit the image for the entire background,it means that it will stretch the image to fill the       background entirely even if the image size is  small.

   Then, iv.setImageResource(R.drawable.yourimagename);
   
  It will take the size of the image in ImageView.for this,we have to set:
  
       android:layout_width="wrap_content" and
       android:layout_height="wrap_content

  If the size of the image is smaller than ImageView,then the remaining border will be left           blank and  the background will be visible.


 Note:The image must be put in res/drawable/ folder.
                                                

No comments:

Post a Comment