Android relative layout align center of background - android

I have a background image, which is simply colored panel such as silver purple, etc.,
This background image is set as andorid:background of RelativeLayout, which consist of a ImageView
I want that imageview's image resource to be appear like in center of the background image.
I tried alignParentTop and marginTop to position the combo image in center.
It works as expected, but when trying on different devices, as marginTop is hardcoded such as 10dp,, or 5dp, the alignment looks differently on different devices with different resolution.
I have even created various dimens.xml files for various configuration with various marginTop dp values. Still it only works on specific devices and doing fro all devices is obviously impossible. So i need to set or align the background and foreground images such that foreground image is centered over background image.
Note: Using Framelayout is not of my concern. I want this by RelativeLayout.

On the ImageView and the parent RelativeLayout, remove all of the padding and margin stuff you have, then add android:layout_centerInParent="true" to the ImageView. If you know the height and width of the image in the ImageViewbeforehand, set the ImageView to match it identically (in dp's). Otherwise, try setting the ImageView height and width to wrap_content and potentially android:adjustViewBounds="true".

Related

ImageView won't match the parent instead showing white beside the image

I've often seen that I've you place an ImageView in a RelativeLayout in Android, and set its layout:width to match_parent, it won't size the image and make it bigger to fill the width, instead it show some white space besides the image. Why is that happening and how can I handle that?

CardView vs LinearLayout - images scalling issue

Please look at following 2 layouts.
First, cardview:
http://pastebin.com/iYzUiDQ7
Second, LinearLayout only (layout is actually the same as the one inside my CardView):
http://pastebin.com/xijHZCPw
So, second layout contains WebView component + single LinearLayout under it that should represent the same layout as single CardView item.
It works, however... I'm having problems with scaling images. If I set very large image as image for my ImageView placed on CardView, it will be scaled nicely to fit on my CardView. CardView's height won't be changed, instead, image will be scaled proportionally to fit.
But when I'm trying to do the same with second layout I linked, my LinearLayout changes its height if I set large image. How should I change my second layout to get the same effect for my LinearLayout, placed at bottom of my screen?
I get the difference between the two layout.
you had set the layout_height with 100dp in CardView. so the height of LinearLayout in CardView will does not beyond that. But you set the layout_height with wrap_content in you second LinearLayout.
Try to set maxHeight or layout_height with digit to deal with you problem.
Hope this can help you.

how to create android UI so that it does not change with screen resolution?

I have an android app with a background image,which I would deploy on emulator and see its behavior.
Initially I was using emulator with skin WVGA800.In the UI all the textView declared in the layout were properly placed.
But when I change the emulator skin to 500x600 then in UI few of the textView are misplaced.
So could anyone please suggest how to handle this.
The good, but difficult thing about Android is that you don't know what size or aspect ratio the device your code runs on will be. If you want full-screen images, you need to be willing to have them scaled (proportionally or disproportionally) or cropped.
If you're trying to position text in a particular place on a background image, you're going to have a bad time. I've tried this before and quickly changed my mind.
Here's my suggestion: Read the screens support docs (as #abhilasha said) and use adaptive UI elements that scale nicely (like 9-patch images, relative layouts, etc).
Then if you want to place text in an image, instead of trying to place a TextView over an ImageView, set the image you want as the background of the TextView. Then you know where the text will be relative to the image.
My options:
1)for simple ui,i think u can use many attars with view,the following;
android:layout_centerHrizontal
android:layout_centerVertical
android:layout_centerInparent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop
android:layout_alignLeft
android:layout_alignBottom
android:layout_alignRight
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
that is helpful to use layout or view attars to set its position.
2)u can use some layouts xml to fit it.like that
if(screen width < 480)
setcontentview(r.layout.for480lay)
else if(screen width > 480&&screen width <640)
setcontentview(r.layout.for640lay)
..
..

Scaling ImageViews For higher res phones

I have a few ImageViews located near the bottom of my application. When I run in WVGA (Nexus One), everything lines up nicely with the bottom.
When running on another higher res, such as FWVGA, as well as on my Droid 2, there is a space that is left on the bottom of the application.
Is there a way to scale imageviews so that they stretch to fill all the space needed in one or multiple directions?
At the moment I am using an AbsoluteView and my imageview code looks like. I have tried layout_x and layout_y.
<ImageView android:layout_height="400px"
android:id="#+id/widget33"
android:layout_width="300px"
android:background="#drawable/picture1"
android:layout_x="-45dp"
android:layout_y="216dp">
</ImageView>
I have also tried a few other things such as
android:baselineAlignBottom
android:padding
You had fixed the height and width of the imageview.
If you want to set imageview in your whole screen you can use fill_parent or match_parent if above Android2.2 .
And also I am giving you advice to use relative layout instead of Absolute layout.
In relative layout you can set Views in relative position so that your view will remains same even if the resolution of the device will increase.
And also use unit of length in dp instead of px so that your view will remains same in all screen density.

marginRight property of imageview not working

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

Categories

Resources