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)
Related
In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working
I have 2 bitmaps that I saved with view.getDrawingCache(); called firstBitmap and secondBitmap. They were saved from initial drawings made by the user. Now I want to display them. My issue is that only the first picture is displayed by itself (I want both to be shown at the same time). I know that the bitmaps are correct because if I remove layout.addView(pic1), I can see the second image by itself clearly. layout is a LinearLayout.
My suspicion is that the size is an issue but I believe that by default views that are added to the layout have fill parent so this should still result in 2 images not one. I have tried dynamically changing the width and size with LayoutParams but this makes the image disappear entirely. I am open to any suggestions.
ImageView pic1 = new ImageView(this);
pic1.setImageBitmap(firstBitmap);
layout.addView(pic1);
ImageView pic2 = new ImageView(this);
pic2.setImageBitmap(secondBitmap);
layout.addView(pic2);
Try to distribute the weight in your linear layout so that both bitmaps will be visible.
you could use the below code to do that.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
layout.addView(pic1, params);
layout.addView(pic2, params);
I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)
i need to draw two imageviews, (+) and (-) symbols, on the bottom right corner of the screen, similar of the zoom objects from googlemaps.
I need to do it programatically, with Java, and without using XML files.
I'm trying to do with relativelayout, but i dont know how to do it. They must to be on the bottom right corner of the screen, with 5 or 10 pixels of separation between them.
How to do it?
Also will be cool if someone can tell me how to detect when a user has pressed each image.
You can use gravity.
http://developer.android.com/reference/android/widget/RelativeLayout.html#setGravity(int)
this might do it:
image.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
This should work
ImageView plusImage = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
pp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
pp.leftMargin = 5;
plusImage.setId(501);
plusImage.setLayoutParams(pp);
plusImage.setImageResource(R.drawable.icon);
ImageView minusImage = new ImageView(this);
RelativeLayout.LayoutParams mp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mp.addRule(RelativeLayout.LEFT_OF,plusImage.getId());
mp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
mp.rightMargin = 5;
minusImage.setId(502);
minusImage.setLayoutParams(mp);
minusImage.setImageResource(R.drawable.icon);
//Add the images to the outer layout
((RelativeLayout)findViewById(R.id.outerlayout)).addView(plusImage);
((RelativeLayout)findViewById(R.id.outerlayout)).addView(minusImage);
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