How to Show Image in Android using Java Code - android

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........

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);

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);

Using Drawable class you can show Image
Drawable drawable = Drawable.createFromPath(imagePath);
image.setImageDrawable(drawable);

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

use ImageView and learn more about it from here imageview referecne
you can set image from bitmap,uri, from resources, etc...

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);
..

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);

The code below worked fine for me.
ImageView imageViewVar = (ImageView) findViewById(R.id.imageViewId);
imageViewVar.setImageResource(R.drawable.yourImgFile);

Related

how to play .svg animation file android activity

I have an animated *.svg file. I cannot find a way to play it in my android activity. I also need to know when the loop is over.
how to achieve this?
You could use https://shapeshifter.design/ in order to create an AnimatedVectorDrawable from plain SVG (see: https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable)
After creating the drawable and including it to the Assets Folder you can load the drawable and assing it to an ImageView, which is defined in your xml Layout.
See the following code:
final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
AnimatedVectorDrawableCompat.create(getApplicationContext(),
R.drawable.animatedvector);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageDrawable(animatedVectorDrawableCompat);
final Animatable animatable = (Animatable) imageView.getDrawable();
animatable.start();
R.drawable.animatedvector is the AnimatedVectorDrawable you want to load, while R.id.imageView is the ImageView, which should show the AnimatedVectorDrawable.
Hope this helps ;-)

Set an image in imageView

I'm pretty new in Android and I am trying to make a Mancala (it's kind of an African game) app. I am looking for a way to update an Image View from one image to another programmatically (for those who are familiar with the game- I want the images of the empty holes to update to images with balls every time the player plays).
I tried to look for an answer but I didn't find one. Here is what I've tried:
imgHome2=(ImageView)findViewById(R.drawable.home0);
You can do same by using Drawable:
private ImageView imageView;
on onCreate()
imageView = (ImageView) findViewById(imageView);
Create new method for set drawable on ImageView.
private void setDrawableOnImageView(Drawable drawable){
imageView.setImageDrawable(drawable);
}
How to call above method:
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
setDrawableOnImageView(drawable);
You can change drawable as your requirement.
in findViewById you should pass an id not a drawable
imgHome2=(ImageView)findViewById(R.id.myimageviewid)
and than you can change the image by
imgHome2.setBackground(R.drawable.home0);

Imageview Src path in Android in Appium

In our Android app, we use placehold images in our thumbnails until the server returns a valid image. The Android devs change the src of the ImageView when an image is found. I am unable to find the src of the ImageView because when i do a get_attribute('src'), nothing comes up. Is there a way to get the src of an ImageView?
first you need to set tag and then you can surely get the resource of ImageView.
myImageView.setTag(R.drawable.currentImage); //set this along when you set your image source
int drawableId = (Integer)myImageView.getTag(); //get the resource
I solved this by setting the ImageView description like .setContentDescription({image_resource})
and then getting it on appium with
.getAttribute("name")

Converting bitmap to drawable in Android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);

setImageResource for an ImageView from another ImageView

Strange request here, but is there any way you set an imageview to hold the same drawable as another imageview by getting it from the original imageview. what i mean is:
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
//now i want something like this, which doesn't work. i want to get it from image1
image2.setImageResource(image1.getDrawable());
so please how can i achieve this from image1?.. Thank you :)
Use setImageDrawable instead of setImageResource
For setting Image Resources from another image view
You need to use two methods of ImagView
setImageDrawable()
getDrawable()
Your code is all perfect except one error
You need to use setImageDrawable() in place of setImageResource()
following is your code implemention using above methods with correction
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
// just change this to setImageDrawable
image2.setImageDrawable(image1.getDrawable());

Categories

Resources