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
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Custom Row in a Listpreference
Android: Wrap_Content Is Not Working with Listview
Check If Battery Optimization Is Enabled or Not for an App
How to Show Multiple Markers on Mapfragment in Google Map API V2
How to Get Current Flavor in Gradle
Need an Example About Recyclerview.Adapter.Notifyitemchanged(Int Position, Object Payload)
How to Transfer an Image from Its Url to the Sd Card
Android Facebook Sdk 3.0 Gives "Remote_App_Id Does Not Match Stored Id" While Logging In
Android: Change Shape Color in Runtime
Disable the Notification Panel from Being Pulled Down
Emulator in Android Studio Doesn't Start
How to Set Multiple Alarms Using Alarm Manager in Android
How to Add Footer to Navigationview - Android Support Design Library
Android Fade in and Fade Out with Imageview
How to Programmatically Enable and Disable Flight Mode on Android 4.2