I want to move all the contents of my page down by about 10 pixels. I have tried to select all and drag down but the layout gets all messy. This is due to the relative layout. Is it possible to just shift everything down by a bit without ruining the layout? Thanks
You can use the android:paddingTop attribute of the RelativeLayout. It accepts values in px, dp, and sp (so you're looking at android:paddingTop="10px" though the convention is to use dp for "density-independent pixels"). If it doesn't work, you can try adding another View at the top of your layout whose android:layout_height is 10 pixels.
Related
I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.
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)
..
..
When making the android application layouts we have to define the layout width , what is the meaning of the android:layout_width="wrap_content" ?
Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
fill_parent
Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in.
Setting a top level layout or control to fill_parent will force it to take up the whole screen.
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
see the official docs for more details!
android:layout_width="wrap_content" means that the layout is fixed it expand up to exactly as your content size .If your content is more it expand up to that limit and same as in short content.
Here is an example..
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button ABC"/>
</RelativeLayout>
wrap_content Image:
wrap_content means that width is determined by the width of the widgets, respectively not the whole display' width but only how widgets need.
Referring to the official Android documentation:
android:layout_width
Specifies the basic width of the view. This is a required attribute
for any view inside of a containing layout manager. Its value may be a
dimension (such as "12dip") for a constant width or one of the special
constants. May be a dimension value, which is a floating point number
appended with a unit such as "14.5sp". Available units are: px
(pixels), dp (density-independent pixels), sp (scaled pixels based on
preferred font size), in (inches), mm (millimeters). This may also be
a reference to a resource (in the form "#[package:]type:name") or
theme attribute (in the form "?[package:][type:]name") containing a
value of this type. May be one of the following constant values.
fill_parent -1 The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
match_parent -1 The view should be as big as its parent (minus padding). Introduced in API Level 8.
wrap_content -2 The view should be only big enough to enclose its content (plus padding).
android:layout_width="wrap_content"
means that layout width is not determined or fixed, it will take space according to its components.
Suppose in a textField u have only one word then your textField will take space for a word only and then it will take space for two word if you have two word in your textField when you will declare your width as wrap_content.
Same thing is applicable for layout_height also.
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.
Can I using xml layout attributes put image to the following position? Or I must calculate position on src?
http://i.stack.imgur.com/gox4d.png
You can do it using the XML attributes.
Just use follwing attribute in your ImageView:
Android:layout_marginTop="20dip"
Android:layout_height="20dip"
Only use DIP as these are density independent Pixels
dont use PX.
Yes, you can, for the simple example you posted I would use a vertical LinearLayout, with a FrameLayouts to 'fill' the blank space. Set the LinearLayout weightSum to 1, then set the first FrameLayout weight attribute to 0.2 (1/5), and imageview to 0.2. Also set both the frame layout and imageview layout_height values to 0px.
While this solution works, I'm sure there is a better and cleaner way out there. Hopefully someone will post it.
You can use AbsoluteLayout, calculate exact coordinates based on screen size and orientation and then position the image with absolute coordinates.
You can just use weights for different containers in a vertical linear layout.