5

I am try to show a image in android using java code not xml. I have done it using xml file but my requirement is using java code to get more funcionality .

thanks in advance for help........

2

8 Answers 8

5

IF you want to load image from drawable folder, you can using:

ImageView imgView=(ImageView) findViewById(R.id.imgView);
Drawable  drawable  = getResources().getDrawable(R.drawable.img);
imgView.setImageDrawable(drawable);
Sign up to request clarification or add additional context in comments.

Comments

5

You have to use one default imageview in android xml when you are running app that time in activity you have to write this code so it will replace you image with another image. Check following code

ImageView imgView=(ImageView) findViewById(R.id.default_imgView);
imgView.setImageResource(R.drawable.yourimagename);

Comments

2

Using Drawable class you can show Image

Drawable  drawable  = Drawable.createFromPath(imagePath);
image.setImageDrawable(drawable);

Comments

1

I hope you are using ImageView to display the image in XML file with id iv.

In java file

ImageView iv = (ImageView)findViewById(R.id.iv);
iv.setImageResource(R.drawable.image1); // image1 is image file available in drawables folder

Comments

0

use ImageView and learn more about it from here imageview referecne

you can set image from bitmap,uri, from resources, etc...

Comments

0

frist add the images to drawable folder and using XML image view

 image = (ImageView) findViewById(R.id.imageView1);
  image.setImageResource(R.drawable.image_1);
  image.setImageResource(R.drawable.image_2);
  ..

Comments

0

You can use the following code

 ImageView img = new ImageView(this);  /*In new version of Android Studio you don't need to do castling.*/
    img = findViewById(R.id.android_image);
    Drawable img_drawable=getResources().getDrawable(R.drawable.draw_img);
    img_cookie.setImageDrawable(img_drawable);

Comments

0

The code below worked fine for me.

ImageView imageViewVar = (ImageView) findViewById(R.id.imageViewId);
imageViewVar.setImageResource(R.drawable.yourImgFile);

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.