setImageResource for an ImageView from another ImageView - android

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

Related

Displaying bitmaps

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

Set an imageview instance by another one

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

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

How to Make ImageView to Wrap its Contents?

I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:
//....
iv = new ImageView(context);
iv.setBackground(getResources().getDrawable(drawable));
iv.setAdjustViewBounds(true);
RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
iv.setMaxWidth(100);
iv.setMaxHeight(100);
iv.setLayoutParams(imajpara);
iv.setScaleType(ScaleType.CENTER_INSIDE);
//....
But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.
So how can I make the ImageView resize itself according to the image's dimensions?
Use setImageDrawable() instead of setBackground().
Instead of indicating "WRAP_CONTENT" in the constructor of your Layoutparams, you can set directly the right dimensions.

How to Show Image in Android using Java Code

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

Categories

Resources