I have one Imageview in my activity_main.xml and I'd like to draw two from this in a different position. I tried this without succes:
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView1);
LayoutParams params = (LinearLayout.LayoutParams) image.getLayoutParams();
LayoutParams params2 = (LinearLayout.LayoutParams) image2.getLayoutParams();
params.topMargin = 50;
params.leftMargin = 50;
image.setLayoutParams(params);
params2.topMargin = 100;
params2.leftMargin = 100;
image2.setLayoutParams(params2);
It draws just one piece. I also tried the setImageBitmap:
image2.setImageBitmap(((BitmapDrawable)image.getDrawable()).getBitmap());
How should I solve this?
You are only seeing one ImageView because there is only one ImageView in your layout. Both image and image2 reference the same id (R.id.imageView1) in your layout:
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView1);
You can solve this 2 ways as I see it:
Inflate your ImageViews one at a time and add them to your layout at runtime. This is a bit more complicated for what you are trying to do.
Add another ImageView to your xml layout (set the id to R.id.imageView2) with the appropriate margins you want. Then just reference each ImageView in code and edit them separately.
Solution #2 would look something like this in your code (after adding a 2nd ImageView to your xml layout):
image = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageView2);
You can't re-use the same ImageView like that. You need to add another ImageView to your layout xml file:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView2" />
and then change this line:
image2 = (ImageView)findViewById(R.id.imageView1);
to this:
image2 = (ImageView)findViewById(R.id.imageView2);
also on a side note, you should avoid using names like imageView1 and imageView2. Those are not descriptive at all, they make it harder to understand what it is that your code is actually doing. Consider using variable names that are more descriptive of what the variable does. It will make your code easier to understand and maintain.
Related
this is like the x-time that I am doing this however today nothing works.
Im basically doing this:
ImageButton btnComments = new ImageButton(this);
ImageButton btngreenLikes = new ImageButton(this);
ImageButton btnblueLikes = new ImageButton(this);
btnComments.SetBackgroundResource(Resource.Drawable.comments_small);
btngreenLikes.SetBackgroundResource(Resource.Drawable.upvote_green);
btnblueLikes.SetBackgroundResource(Resource.Drawable.upvote_blue);
LinearLayout.LayoutParams lpWrap = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
btnComments.LayoutParameters = lpWrap;
btngreenLikes.LayoutParameters = lpWrap;
btnblueLikes.LayoutParameters = lpWrap;
linlayForImages.AddView(btnComments);
linlayForImages.AddView(btngreenLikes);
linlayForImages.AddView(btnblueLikes);
Set add thre imagebuttons in within my code. Give all three a background resource, then set their layout to WRAP CONTENT in HEIGHT and WIDTH.
And then add their views into my layout.
The result ist, they are everything BUT wrapped content. While the comment img is correct, th other two are distored. greenlikes is too big in total size and blue like is top big in width? I use those imagebutton resources in another activity where I set those with the xml and they are fine. So the resource is all clear. Can someone tell me what the HELL is going on here?
Update: Naming my Imagebuttons Imageviews solved this issue. Don't!
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 have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)
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());
I currently have an activity that does not have an xml layout file. All items are added when needed. I am not in need to add a image to it and it doesnt seem to do anything...
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/Pics/Pic_1.jpg");
iv.setImageBitmap(bm);
ll.addView(iv);
The image is in the correct folder...11 at the end is the Linear view i am adding the image view too...am i missing something?
You can use debugger to see if image is loaded correctly.
And have you tried to set layout params on that ImageView. Because in your case it wouldn't take any space (I think). Try something like:
ll.addView(iv, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Or use exact sizes instead of LayoutParams.WRAP_CONTENT