I'm migrating an iOS application to Android.
On iOS I have an UIImage object and I don't know which object can I use on Android. I thought I can use a Drawable.
What do you recommend me?
iOS equal to UIImageView in android is ImageView.
PROGRAMATICALLY DOING IT
ImageView imgView = new ImageView(this);
//setting image position and there are other properties to position ur imageview
imgView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to your layout(linear, relative....etc)
yourLayout.addView(imgView);
//Add this is how you set your Image
this.imgView.setImageBitmap(localBitmap);
IF YOU ARE USING XML FILES
//Get the reference of imageview
imgView = (ImageView) findViewById(R.id.imageView1);
//here you add your image
imgView.setImageResource(R.drawable.yourImage);
Related
I need help with displaying a bitmap. I am using a class which is extending Activity. Where would I put this?
Mybitmap = BitMapfactory.decode resource(getresource,R.drawable.bit);
Also how would I draw it on the screen?
you should use ImageView...
put ImageView in you layout with "myImage" id and in activity do like this :
ImageView mImg = (ImageView) findViewById(R.id.myImage);
mImg.setImageBitmap(Mybitmap);
Here is my code :
ImageView iv1 = (ImageView) findViewById(R.id.image1);
ImageView iv2 = (ImageView) findViewById(R.id.image2);
I would like to know if it's possible to set the R.id.image1 to the same as R.id.image2 dynamically.
R.id.image1 contains no image but if R.id.image2 got an image I would like that R.id.image1 display the same image as R.id.image2
Anything under R is defined at compile time based off of your resources. (Layouts, images, dimens, etc.) That means that you can't change what R.id.image1 means at runtime but you can have multiple ImageViews point to the same resource:
ImageView image1 = (ImageView)findViewById(R.id.image1);
ImageView image1Dup = (ImageView)findViewById(R.id.image1);
Alternatively you can set the image from one ImageView to another one.
image1.setImageDrawable(image2.getDrawable());
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);
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);
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());