Hossame Makhlof
Hossame Makhlof

Reputation: 1

Pass drawable image in an ImageView variable

how to convert an image from drawable file to an Image view because this won't work

ImageView image= R.drawable.pic;

Upvotes: 0

Views: 156

Answers (3)

Star_Man
Star_Man

Reputation: 1091

Try this

ImageView image = new ImageView(this);
image.setImageResource(R.drawable.pic);

or

ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.pic));

Upvotes: 0

Ramesh sambu
Ramesh sambu

Reputation: 3539

Try this

ImageView imageview = findViewById(R.id.imageView);
imageview.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pic));

Upvotes: 1

Gowthaman M
Gowthaman M

Reputation: 8272

Try this

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         fab.setImageDrawable(getResources().getDrawable(R.drawable.pic,this.getTheme()));
     }else {
         fab.setImageDrawable(getResources().getDrawable(R.drawable.pic));

Upvotes: 0

Related Questions