Aligning Android Button To Bottom in RelativeLayout? - android

I'm trying to align a button to the bottom right and bottom left of my screen, using a RelativeLayout. I want to do this to keep the same relative layout across different screen sizes. Currently, the buttons on my screen move up/down depending on the resolution of the screen. 320x480 puts the buttons higher on the screen versus 480x800. I'm trying to get my screens to look the same between the two sizes.

I know this is an old thread but it shows up at the top of search results so I figure it can't hurt to confirm that
android:layout_alignParentBottom="true"
worked for me in RelativeLayout.

Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:panel="http://schemas.android.com/apk/res/it.kronplatz"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/ButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:baselineAligned="false" >
<ImageButton
android:id="#+id/locateMeButton"
android:layout_width="70px"
android:layout_height="70px"
android:layout_marginLeft="5px"
android:src="#drawable/me" >
</ImageButton>
<ImageButton
android:id="#+id/MapCenterButton"
android:layout_width="70px"
android:layout_height="70px"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginRight="5px"
android:src="#drawable/fadenkreuz_klein" />
<Button
android:id="#+id/MapNextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/androidmarker" >
</Button>
</RelativeLayout>
</RelativeLayout>

Are you using android:layout_alignParent* ? That should justify it to a side regardless of screen size.
Where * is Bottom or Top or Left or Right

On addition what AedonEtLIRA said you should make sure that the RelativeLayout you have always filled the whole display area. ie. that you don't have any size defined in any place in the view hierarchy but instead match_parent is used. If you then used the layout definitions given by AedonEtLIRA you should get exactly what you want.

Related

RelativeLayout - can't make views the same vertical size if they are horizontally aligned

I have a layout which has two CardView's and looks like this:
I wanted the right card to be fixed width (90dp) and the left one to automatically resize and fill the remaining space. I have currently done it like this:
<RelativeLayout
android:id="#+id/inventory_host_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<android.support.v7.widget.CardView
android:id="#+id/inventory_last_scanned_wrapper_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/inventory_scanned_infobox_wrapper_card">
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/inventory_scanned_infobox_wrapper_card"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="false"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp">
...
</android.support.v7.widget.CardView>
</RelativeLayout>
Left one can grow, so I also wanted the right card to always be the same height as the left one. Usually I do that kind of alignment by setting both layout_alignTop and layout_alignBottom properties to other View but those properties are not available this time, probably because that would create a circular reference (although I don't really understand how horizontal alignment could affect vertical). What to do? I have explored several other options but always get back to the same point except hardcoding heights or setting them programmatically which of course I want to avoid.
Where and why are you getting circular reference errors? Just set
android:layout_alignBottom="#id/inventory_last_scanned_wrapper_card"
on the card at the right side and everything should work.

Android Image Button Layout

Could someone please explain how android uses image button sizes? I seem to be getting odd behavior with my buttons.
I have the following code as an example. I have two buttons that sit at the bottom of my layout. These buttons share 50% of the total width as they sit side-by-side.
Within Abode PS, the two images (used for these two buttons) are actually 2" x 38" or 495x94 pixels. This size is of course larger than the available space in the layout.
I am using edge effects on my buttons to give them definition. Android is cutting the edges off my buttons in order to center then in the available layout space.
This particular layout that I am working on will only allow vertical orientation, in case that helps.
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/ImageButton1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/map4" >
</ImageButton>
<ImageButton
android:id="#+id/ImageButton3"
android:layout_width="0dip"
android:layout_marginRight="1dip"
android:layout_weight="1"
android:background="#FF000000"
android:src="#drawable/buy"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip">
</ImageButton>
</LinearLayout>
</RelativeLayout>
Try using an ImageView and android:scaleType:
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
Experiment the values available to see what is the best combination!
Then add a listener to behave like a button...
try use the button and make it with empty text after this set the background .
or :
use the image button and put the source and the background the same image to get button exactly like the image
**you can use the selector to make some beauty for application buttons
Google it it's easy to use ;)

How to create layout with android's different display size

My problem born when i create layout in xml file from graphics tool. RelativeLayout is awful! i don't know which layout to use.. When i put an imageview and a imagebutton, in emulator will displayed in a bad way, one apon the other. what can i do? is there a good tutorial on which i can learn how to resize image and layout in percent of different displays size?
i Try this :
<RelativeLayout android:layout_width="match_parent" android:id="#+id/relativeLayout1" android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
<ImageView android:id="#+id/imageView1" android:src="#drawable/testotrova" android:layout_width="243dp" android:layout_height="48dp"></ImageView>
</LinearLayout>
<ImageButton android:layout_width="wrap_content" android:src="#drawable/info" android:layout_height="wrap_content" android:id="#+id/imageButton1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></ImageButton>
</RelativeLayout>
but when i display it isn't good
Regarding support for multiple screens, have a look here.
You will need to add different layouts for screen size small, normal and large.
They are displayed one upon the other because you are using RelativeLayour and they are both (Image and text) anchored to the upper left:
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
I strongly suggest you to use LinearLayout if you have only a few items. (http://www.learn-android.com/2010/01/05/android-layout-tutorial/4/)
For your image size, if you want a percent, please use:
android:layout_height="0dip"
android:layout_weight="1"
http://developer.android.com/resources/articles/layout-tricks-efficiency.html

Scale layout items equally on large screens

I've got a 4-item start screen in my app, which looks like the following:
What's important to me there:
- All items do have the same width (not regarding how much text is actually in it)
- Look the same on all devices (small-screen, mdpi, large-screen, etc.)
Im just wondering if there is a easy solution about this problem?
I've tried using 3 LinearLayouts but thats really awkward..
(1 presenting the layout root[vertical] and two which do each contain 2 buttons[horizonal]).
Making this layout ready for multiple screens would require a lot of fixed-width and fixed-margin hacking. Just like "button margin = 30dp on xlarge, 20 on large, 15 on normal,...".
My layout-xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:id="#+id/main_root"
android:orientation="vertical"
android:gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_learn"
android:text="#string/mainBtn_learn"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_quiz"
android:text="#string/mainBtn_quiz"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btn_search"
android:text="#string/mainBtn_search"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
<Button
android:id="#+id/btn_more"
android:text="#string/mainBtn_more"
style="#style/mainBtn"
android:onClick="handleBtnClick"
android:layout_margin="20dip" />
</LinearLayout>
Is there a view which "auto-scales" these Buttons or still any other easier solution?
Edit:
So, in special, you need something like
button:
android:layout_width="15%" // 15% of screen width / height depending on the orientation
android:layout_marginBottom="10%" // see above
I'm pretty new to Android development but I can show you what worked for me in a similar case. I defined my layout as follows:
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:editable="false" />
<Spinner
android:id="#+id/outputSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="#string/OutputBaseOptionsPrompt" />
</LinearLayout>
I have a horizontal layout with two items. The LinearLayout has a width of "match_parent" so that it is as wide as the screen. Both items in the layout have the following:
android:layout_width="0dp"
android:layout_weight="1"
Since both items have a layout_weight of 1, they will be drawn at the same width. In this case, each item takes up half of the available space. If you change the weight of one of these items to "2" then it will be twice as wide as the item with a weight of "1".
Do you already have xml that makes it work on one screen size? If so post what you have so far.
I would suggest using a RelativeLayout for your root though. You can use the alignCenter attributes to float your children towards the middle. Then you just have to hard code the inner margins (how far apart you want the buttons) rather than the margin from yourself to the wall.
You could also avoid having to hard code the inner margin by making your own button 9 patch images. You can just add a border of transparent pixels in your image to represent the margin. You'll probably still want to supply an image for each density you wish to support though.
The solution is you dont use hardcoded values any where
Put three images with same name in hdpi mdpi and ldpi folders in drawables
an run the code

Android - Laying out a screen with fixed top and bottom elements in a resolution independent way?

I have a layout like this
Button
MapView
Button
I want the buttons to be a fixed size & anchored to the top and bottom of the screen respectively. I want the mapview to be in the middle & fill up the space.
What's the best way to achieve this?
Thanks
Use LinearLayout with vertical orientation and set Mapview's weight as 1
Something like this should do the trick:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<WhateveerMapLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/button"
android:layout_above="#+id/button2"
/>
<Button
android:id="#id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

Categories

Resources