Imran Shafqat
Imran Shafqat

Reputation: 518

Align buttons evenly on bottom in Android for all screen sizes

I have a simple view with one image and one bottom navigation bar. I am struggling to fit all buttons for all screen sizes.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/simpleImageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:src="@drawable/image1" />

    <LinearLayout
        android:id="@+id/navigationBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp">

        <ImageButton
            android:id="@+id/icon_left"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/icon_left" />

        <TextView
            android:id="@+id/text_spinner"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:textAlignment="center"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="20sp"
            android:text="Page: " />

        <Spinner
            android:id="@+id/page_dropdown"
            android:layout_height="50dp"
            android:layout_width="110dp"
            android:dropDownWidth="110dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:gravity="center"
            android:textAlignment="center" />

        <ImageButton
            android:id="@+id/bookmark"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/star_hollow" />

        <ImageButton
            android:id="@+id/icon_right"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/icon_right" />

    </LinearLayout>

</RelativeLayout>

Here is how it appears in 480 x 800 resolution

Here is how it appears in 768 x 1280 resolution

Here is how it appears in 1080 x 1920 resolution

I need a solution where all of these buttons, controls are evenly distributed on all screen sizes.

Upvotes: 0

Views: 275

Answers (2)

luckyhandler
luckyhandler

Reputation: 11329

Don't use RelativeLayout. It's deprecated and should be replaced with ConstraintLayout

In ConstraintLayout align the outer bottons with the attributes

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" // right side of button
app:layout_constraintStart_toStartOf="parent" // left side of button

and the inner ones with

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/leftButton"
app:layout_constraintEnd_toStartOf="@id/rightButton"

Find more about ConstraintLayout

Upvotes: 1

bbqapps
bbqapps

Reputation: 49

Remove LinearLayout, and keep using RelativeLayout for the parent layout. use the layout_below attribute to place buttons and icons below the image, and use the layout_toEndOf attribute to place the component next to the right of other components.

Reference : https://developer.android.com/guide/topics/ui/layout/relative

Upvotes: 1

Related Questions