I have an application where I have to place a set of ImageViews in different parts of the screen, each ImageView placed at a position by specifying the co-ordinates(x,y) Is this possible in android? If so, pls let me know how to do it.
It is possible but not recommended since it doesn't work well across resolutions/densities/aspect-ratios.
This is possible by setting the positions in their xml layout in an absolutelayout. Though, if you want to change the position of the imageview in your code dynamically you can only change the positions by setting their margins and padding to move them left/right and up/down. The following receives the layout params of the imageviews so that you can change the views' margins.
ImageView image;
MarginLayoutParams params = (MarginLayoutParams)image.getLayoutParams();
param.setMargins(int left, int top, int right, int bottom);
Related
EDIT: Answer found through Emil's comments. Code ended up like this:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);
Thanks all :D
I'm currently writing an Android application and I have a dynamically created tree which displays ImageView elements within it. However, to arrange these images I've used padding left to line them up correctly, dynamically changing how the padding is calculated so that everything turns out evenly. However when I do this, the padding covers up some of the images to the left, and even though you can see the images behind this padding, when you click on that image, it will evaluate the image who's padding is being used (the furthest left element). I'm basically wondering if there is a way to programatically set something that does what padding does to images but isn't clickable? Margins would make sense, but you cannot set margins on ImageViews.
The tree set up is like this:
-----A-----
--B--C--D--
-E-F----G--
Imagine every letter is an ImageView, dynamically placed with padding. But if I click on B or C, D's code will be evaluated because it's left padding covers B and C. The same goes for E or F, click those, and G will be evaluated. I cannot figure out a different way to place these ImageViews. Any help is greatly appreciated.
Consider replacing your padding with layout_margin property. that way the area that separates the padded view is not considered as part of the view but ratter as part of the layout it sits in.
So use
android:layout_margin="10dp"
or:
android:layout_marginLeft="10dp"
for your needs.
is there a way to move the whole screen content outside the screen bounds, with titlebar and all, on the left for example, i tried
content = ((LinearLayout) act.findViewById(android.R.id.content)
.getParent());
to get the screen content and add a margin to it
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
.getLayoutParams();
pr.rightMargin = xOffset;
content.setLayoutParams(pr);
but it doesnt work, the margin appears but the content is not moved just resized to fit the screen, anyone know how to do this the simple way?
you can set the margin in negative(-) values.
for example if your want the layout to move upper use this:
android:marginTop = "-50dip or more u can put"
Are you trying to flip between views? As in, moving that layout off the screen to show another one? If so you should look at using a widget called ViewFlipper which will do exactly as you need. It can also be animated etc.
http://developer.android.com/reference/android/widget/ViewFlipper.html
I have a GridView populated with ImageButtons at runtime. Some buttons are smaller than others and they hug the top of their row; I want them centered in their row. I think I could accomplish this with a margin (or maybe even gravity) but programmatically I don't see any way to do this.
The usual advice is to use LayoutParams, but the layout params available via GridView's LayoutParams are very limited and don't include margin or gravity.
Thanks in advance for any suggestions!
If your images are a part of a linearlayout of any other layout view, on the image, you could use something like :
<View
android:gravity="center-vertical"/>
This will handle the view to be in the center of the vertical stretch. If you use :
android:layout_gravity
then it will position it according to its parent.
I'm moving a view after an animation completes(to slide out a menu from the left). However, I don't seem to be able to achieve the effect I'm looking for. What I'd like is for the view to extend to the right past the parent's bounds, like this:
but what's happening is actually this:
The view resets itself to stay within the bounds of the parent. Even if I set an absolute pixel value (by looking at the display's width, or even a randomly large value).
The reason I need to do this is detailed in this SO question about a view's actual position after an animation has completed:
TranslateAnimated ImageView not clickable after animation [Android]
any thoughts? Thanks!
So, here is a related question. It explains how to move a view out of the screen.
I had same problem and I found the solution. I hope it can be helpful.
You need to set fixed width if you want to achieve this effect. To find real width of the view you need to measure it.
So full solution is:
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
(Use LayoutParams of the type corresponding the type of the parent layout)
I have an image view whose background is a xml shape drawable i.e. a rectangle shape drawable.My app's orientation is fixed to landscape view.
The imageview is in relative layout.
I am trying to move it to the right of the screen by setting the appropriate value of layout_marginRight but this does not work .The imageView always stays in its's original position.
I have tried the following other options also but none helped.
The other options which I tried are:
Creating a new relative layout params and setting the right margin
Creating new margin layout params and setting the position
Trying padding option
Setting the imageview to right position relative to another imageview...
Using display metrics to get width of screen and accordingly setting the margin....
I am stuck since a week setting the position of this imageview...
I was thinking the best approach is to set this imageview in between two imageview as I am not able to move it by setting margin but that does not work either...
Here is the current xml of my imageview in main.xml:-
<ImageView
android:id="#+id/rect1"
android:background="#drawable/rect"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="70dp"
android:scaleType="fitCenter"/>
I don't see why adding a margin to the right of the image would help move the image to the right, it just extends the right side bounds of the imageview by the margin. As long as the imageview isn't actually on the right side of the parentview, it would be allowed to grow without changing position. I suggest using layout_alignParentRight="true" on the imageview if you want it on the right of your relativelayout, and then you can use the marginRight to control how far off the right side you want it.
Make the layout_width attribute of the RelativeLayout to fill_parent and see it work :)
My issue was that my parent layout's width was set to wrap_content, which doesn't have a defined width during the editor, I think. Thus, I couldn't use marginRight to get an offset. I had to change the width to a fixed size like match_parent or a constant dp size.
put the image view in a linearlayout, then use android:layout_marginRight="70dp