fr4n
fr4n

Reputation: 755

How set an image in Android ImageView

I need to show a image, when i press a button on my application.

I'm trying with:

ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);

but it shows an error.

Anyone can help me? Thanks.

Upvotes: 0

Views: 1191

Answers (2)

Garg
Garg

Reputation: 2731

for future visitors:

sometime setImageDrawable(Drawable) method show an error like:

The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int)

can try following codes:

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(R.drawable.your_image);
imageView.setImageDrawable(image);

--------- OR can try this ------------------

imageView.setImageDrawable(ContextCompat.getDrawable(YourActivity.this, R.drawable.your_image));

hope it work for you.

Upvotes: 0

Ciprian Radu
Ciprian Radu

Reputation: 798

import package_name.R.drawable;

ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);

OR

you can set the image from the start, set it to invisible and when you want to show it just change the visibility property.

Upvotes: 6

Related Questions