machinery
machinery

Reputation: 6290

Android layout with constant height on each screen

I have created the Android layout as shown below. It displays a small toolbar with 2 buttons. The toolbar is located at the bottom. The problem is that on smaller screens the height of the toolbar and the buttons is much bigger than on bigger screen which makes sense because I'm using dp values. How do I have to change it so that the toolbar has visually always the same size on screens with bigger and smaller height? That means if the height is 1cm on big screens it should also be 1cm on small screens.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

    <View
        android:id="@+id/keyboard_toolbar_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/buttonsLayout"
        android:layout_marginBottom="5dp"
        android:background="#c0c0c0"/>

    <RelativeLayout
        android:id="@+id/buttonsLayout"
        android:orientation="horizontal"
        android:alpha="0.5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/privacyButton"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:gravity="center"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginStart="30dp"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/privacyButtonImage"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:scaleType="fitXY"
                android:clickable="false"
                android:focusable="false"
                android:src="@drawable/ic_lock_open_light_24dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ratingButton"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:gravity="center"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginEnd="30dp"
            android:layout_alignParentEnd="true">

            <ImageView
                android:id="@+id/ratingButtonImage"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:scaleType="fitXY"
                android:clickable="false"
                android:focusable="false"
                android:src="@drawable/ic_star_gray_24" />

        </LinearLayout>

    </RelativeLayout >

</RelativeLayout >

Upvotes: 0

Views: 118

Answers (1)

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

For that, you need to set your view height based on dimen in the values folder. Using that dimen value you can archive this thig.

Otherwise, you can use this lib that will provide dp value based on device screen size and resolution

Link: https://github.com/intuit/sdp

Upvotes: 1

Related Questions