skip to Main Content

I’m working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try

pic.setImageDrawable(R.drawable.image_name);

However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).


How may I resolve this?

2

Answers


  1. You can use setImageResource() for this:

    pic.setImageResource(R.drawable.image);
    

    More about it here

    Login or Signup to reply.
  2. Use this for java

    ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
    

    and then set this drawable to your image view

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search