3

I am trying to make my button centered horizontally and 2/3 down from the top vertically. I want it to remain 2/3 from the top no matter what the screen size/density. Here is the button I am trying to move:

<Button
    android:id="@+id/btnResume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="250dp" />

Thanks!

Working Code:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <Button
        android:id="@+id/btnResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <TextView
        android:id="@+id/tvCounter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • which layout used ?used layout_weight property for linear layout – Hemantvc Dec 19 '12 at 04:34
  • using relative layout, but i can always put a linear layout inside of teh relative layout if need be, i think. (sorry about delayed response...didnt see comment) – Evorlor Dec 19 '12 at 04:49

2 Answers2

2

To archive this you need to use LineareLayout and explore weight feature. To get it work it must be something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

<Button
    android:id="@+id/btnResume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</FrameLayout>

</LinearLayout>

Hope it helps!

Evos
  • 3,915
  • 2
  • 18
  • 21
  • i am not sure how i would use this to make my button 2/3 of the way down the screen. could u be more specific, please? – Evorlor Dec 19 '12 at 04:56
  • ah nevermind. i just interpreted it. thanks! let me try implementing it – Evorlor Dec 19 '12 at 04:59
  • Ok, you can not make your button 2/3 of way down only using attributes of your button. The only thing you can do is to use `LineareLayout` and set `weign` attributes to it's childs to set you button in proper position. If you have any other element in your UI you can use my code as a layer an put all your stuff in `RelativLayout`. Check this link for more info about `weight` attribute https://developer.android.com/guide/topics/ui/layout/linear.html#Weight – Evos Dec 19 '12 at 05:00
  • set `android:orientation="vertical"` tag for your root `LineareLayout`.It's horizontaly orientated by default. – Evos Dec 19 '12 at 05:09
  • excellent thanks! i posted the working code above for others' future reference – Evorlor Dec 19 '12 at 05:18
  • You can try to set `gravity="center_horizontal"` for your root layout and remove button orientation tag. You're welcom! – Evos Dec 19 '12 at 05:21
0

You can use the attribute "layout_weight"

HeyNine
  • 21
  • 2