Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning Buttons Dynamically - Android

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>
like image 402
Evorlor Avatar asked Jun 10 '26 04:06

Evorlor


1 Answers

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!

like image 93
Evos Avatar answered Jun 13 '26 07:06

Evos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!