Change Image of Imageview Programmatically in Android

Change Image of ImageView programmatically in Android

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.

Set the image dynamically in imageView in android

I found the problem.

It must be: if ( position.equals( "0" ) ) In addition i am using only: imageView.setImageResource( R.drawable.stone_a1 )

Changing ImageView source

Changing ImageView source:

Using setBackgroundResource() method:

  myImgView.setBackgroundResource(R.drawable.monkey);

you are putting that monkey in the background.

I suggest the use of setImageResource() method:

  myImgView.setImageResource(R.drawable.monkey);

or with setImageDrawable() method:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));

*** With new android API 22 getResources().getDrawable() is now deprecated. This is an example how to use now:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));

and how to validate for old API versions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
}

How to set the image from drawable dynamically in android?

Try this:

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

change image programmatically in ANDROID

You are not setting any image onClick of your image. You are just pushing the dialog. If you want to change image after onClick of the ImageView
then add this line in your onClick method

imageView.setImageResource(resId);

Set ImageView Drawable programmatically

You are setting the image resource to a random number.
You need to do it like this:

int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);

How to change ImageView source in android

whoamiwith.setImageResource(R.drawable.loginbtn);

How to change imageview content between color and image programmatically?

If you want to have your bitmap replaced with solid color, just do ordinary setImageResource(), i.e.

iv.setImageResource(Color.BLACK);


Related Topics



Leave a reply



Submit