4

After searching for a few hours, I was unable to find the exact answer to my situation. I'm currently using a RelativeLayout and all I have is a background image and a button. The problem I'm having is placing the button in the exact location I want it to be (a little offset from the center).

My first attempt was to modify the layout_margins of the button. While this worked for the current emulator I was working with (3.7in WVGA), the positioning of the button was slightly/way off for a different screen size (such as 3.2in HVGA).

I then attempted to modify the padding of the parent layout but got the same issue. My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background">
    <Button android:id="@+id/starttimer" 
            android:background="@drawable/button" 
            android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/>
</RelativeLayout>

Please let me know if I'm completely off with my approach. It seems like an extremely simple problem, and I'm bummed out that it's taken me so long to try to get it. Thanks for any help!

Miuranga
  • 2,463
  • 10
  • 51
  • 81
marcuspfister
  • 41
  • 1
  • 1
  • 2

2 Answers2

2

I take it that you want to center the button on the bottom of the parent with a little offset, try layout_centerHorizontal then add your preferred margin.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background">
    <Button android:id="@+id/starttimer" 
            android:background="@drawable/button" 
            android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android_marginLeft="5dp"/>
</RelativeLayout>
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • Andreas & Sandy - I appreciate the super-quick responses. Unfortunately, the problem still seems to exist that the position is changing on different screen sizes. Also, after adding the layout_centerHorizontal property, my horizontal margins no longer take effect. – marcuspfister Dec 07 '10 at 08:57
  • yep that's expected, hm, it's not recommended, but try using px for your padding / margin instead of dip. – Andreas Wong Dec 07 '10 at 08:59
  • @andreas - I gave it a shot and ironically, my current layout got screwed up, but it fixed a different screen size I was having problems with ;). – marcuspfister Dec 07 '10 at 09:02
  • Sorry for screwing out your layout :p, you can just ignore my code and use your code but with px. see if that works. For info about measurement units, see http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – Andreas Wong Dec 07 '10 at 09:06
0

Absolute Layout is used for absolute positions of the control.

Sandy
  • 6,285
  • 15
  • 65
  • 93
  • 4
    since it'sdeprecated you can always use FrameLayout with layout_gravity="top|left" and then give your items marginTop ad marginLeft that are equal to your (x,y) coordinates. In any case of course on differen scren with different DPI it will be a bit different, that'swhat dp (dependable pixel) means... you can use px or make your own calculation relative to the screen size in onLayout if it's not enough. – codeScriber Dec 07 '10 at 09:38
  • 2
    @codeScriber: Thanks for your input. I'm considering creating different layouts for different screen-sizes and using different dip values for each one. Would that be a good idea? – marcuspfister Dec 07 '10 at 09:46