Quorrin Quorrin - 5 months ago 16
Android Question

Align a button below the bottom of the screen

Currently my code places a button at the bottom of the screen:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|bottom">

<ImageButton
android:id="@+id/button1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_centerInParent="true"
android:gravity="center"
android:src="@drawable/main_button"
/>
</LinearLayout>
</RelativeLayout>


However, I wish to align it slightly below the bottom of the screen, so a small part of the button is clipped off (Say, 20%). Ultimately, my end result is that when I tap and drag the button up, it will move up and reveal the entire button, but before that, the bottom part of it is clipped.




There are many questions on how to align a button to the bottom of a screen, but I can't seem to find any that aligns it to below the bottom of the screen.

Is there any way to do so, or am I not using the correct search terms?




EDIT: From the answers, I tried android:translationY="20dp" and it did not work, but I tried android:layout_marginBottom="-20dp" and it worked for me.

Answer

You can add android:translationY="Xdp" to your button to move it down by X dp.

Alternatively you can create two LinearLayouts. One to fill the screen and hold other components and another that contains the button. You can then add android:layout_below="@id/layout0" and android:margin_top="Xdp" to control how far below the screen the layout with the button should be.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true">

        </LinearLayout android:id="@+id/layout0"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_below="@id/layout0"
            android:margin_top="Xdp"
            android:gravity="center|bottom">

                <ImageButton
                    android:id="@+id/button1"
                    android:layout_width="95dp"
                    android:layout_height="95dp"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:src="@drawable/main_button"/>
       </LinearLayout>
</RelativeLayout>
Comments