Reputation: 3756
When I change the image programmatically, it shows new image on top of the old image which is set originally in 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 imageView:
@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?
Upvotes: 158
Views: 361810
Reputation: 1012
This code is working in my case:
imageView.setImageResource(R.drawable.logo)
Upvotes: 0
Reputation: 369
In Java, we can change ImageView programmatically like this.
ImageView imageView= (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.imagename);
In Kotlin,
val imageView: ImageView = findViewById(R.id.imageView)
imageView.setImageResource(R.drawable.imagename)
Upvotes: 2
Reputation: 149
If the above solutions are not working just delete this entire line from XML
android:src="@drawable/image"
& only try
imageView.setBackgroundResource(R.drawable.image);
Upvotes: 3
Reputation: 4022
You can use
val drawableCompat = ContextCompat.getDrawable(context, R.drawable.ic_emoticon_happy)
or in java java
Drawable drawableCompat = ContextCompat.getDrawable(getContext(), R.drawable.ic_emoticon_happy)
Upvotes: 2
Reputation: 617
In XML Design
android:background="@drawable/imagename
android:src="@drawable/imagename"
Drawable Image via code
imageview.setImageResource(R.drawable.imagename);
Server image
## Dependency ##
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
Glide.with(context).load(url) .placeholder(R.drawable.image)
.into(imageView);
## dependency ##
implementation 'com.squareup.picasso:picasso:2.71828'
Picasso.with(context).load(url) .placeholder(R.drawable.image)
.into(imageView);
Upvotes: 20
Reputation: 511558
Just copy an image into your res/drawable
folder and use
imageView.setImageResource(R.drawable.my_image);
The variety of answers can cause a little confusion. We have
setBackgroundResource()
setBackgroundDrawable()
setBackground()
setImageResource()
setImageDrawable()
setImageBitmap()
The methods with Background
in their name all belong to the View
class, not to ImageView
specifically. But since ImageView
inherits from View
you can use them, too. The methods with Image
in their name belong specifically to ImageView
.
The View
methods all do the same thing as each other (though setBackgroundDrawable()
is deprecated), so we will just focus on setBackgroundResource()
. Similarly, the ImageView
methods all do the same thing, so we will just focus on setImageResource()
. The only difference between the methods is they type of parameter you pass in.
Here is a FrameLayout
that contains an ImageView
. The ImageView
initially doesn't have any image in it. (I only added the FrameLayout
so that I could put a border around it. That way you can see the edge of the ImageView
.)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="@drawable/border"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</RelativeLayout>
Below we will compare the different methods.
If you use ImageView's setImageResource()
, then the image keeps its aspect ratio and is resized to fit. Here are two different image examples.
imageView.setImageResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
Using View's setBackgroundResource()
, on the other hand, causes the image resource to be stretched to fill the view.
imageView.setBackgroundResource(R.drawable.sky);
imageView.setBackgroundResource(R.drawable.balloons);
The View's background image and the ImageView's image are drawn separately, so you can set them both.
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
Upvotes: 88
Reputation: 641
qImageView.setImageResource(R.drawable.img2);
I think this will help you
Upvotes: 31
Reputation: 1849
Use in XML:
android:src="@drawable/image"
Source use:
imageView.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.your_image));
Upvotes: 102
Reputation: 17850
That happens because you're setting the src of the ImageView
instead of the background.
Use this instead:
qImageView.setBackgroundResource(R.drawable.thumbs_down);
Here's a thread that talks about the differences between the two methods.
Upvotes: 238
Reputation: 186
In your XML for the image view, where you have android:background="@drawable/thumbs_down
change this to android:src="@drawable/thumbs_down"
Currently it is placing that image as the background to the view and not the actual image in it.
Upvotes: 14