Changing ImageView Image Programmatically by Android

When I change the image programmatically, does it show a new image on top of the old image that was originally set in the layout file?

Here is a snippet of my layout file:

<LinearLayout android:layout_width="match_parent" android:layout_height="39dp" android:gravity="center_vertical" > <ImageView android:id="@+id/qStatusImage" android:layout_width="16dp" android:layout_height="16dp" android:layout_margin="5dp" android:background="@drawable/thumbs_down" /> <TextView android:id="@+id/grp_child" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/radio_colors" android:textStyle="normal" android:background="@color/grey" /> </LinearLayout> 

And the code that sets the view image:

  @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //Answers if(answersGroup != null) answersGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // int index = answersGroup.indexOfChild(findViewById(answersGroup.getCheckedRadioButtonId())); qImageView = (ImageView) V.findViewById(R.id.qStatusImage); if(ans ==0 || ans == 5){ // qSV.setImageResource(0); qImageView.setImageResource(R.drawable.thumbs_up); } else qImageView.setImageResource(R.drawable.thumbs_down); } }); 

What am I missing?

+54
android imageview
Jun 03 '13 at 21:58
source share
5 answers

This is because you set src ImageView instead of background.

Use this instead:

 qImageView.setBackgroundResource(R.drawable.thumbs_down); 

Here's a thread that talks about the differences between the two methods.

+113
Jun 03 '13 at 22:03
source share

Use in XML:

 android:src="@drawable/image" 

Source use:

 imageView.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.your_image)); 
+36
Jan 19 '16 at 13:28
source share
 qImageView.setImageResource(R.drawable.img2); 

I think this will help you.

+13
Feb 29 '16 at 9:07
source share

In your XML to represent the image where you have android:background="@drawable/thumbs_down change this to android:src="@drawable/thumbs_down"

Currently, it puts this image as the background for the presentation, not the actual image in it.

+12
Jun 03 '13 at 22:03
source share

In XML Design

 android:background="@drawable/imagename android:src="@drawable/imagename" 

Hand-drawn image using code

 imageview.setImageResource(R.drawable.imagename); 

Server image

 Glide.with(context).load(url) .placeholder(R.drawable.image) .into(imageView); Picasso.with(context).load(url) .placeholder(R.drawable.image) .into(imageView); 
+2
Apr 17 '17 at 6:40
source share



All Articles