juztcode
juztcode

Reputation: 1345

adjusting button size to a certain proportion

Consider the following diagram:

enter image description here The blue buttons are buttons and the red is a textview. I am trying to place them side by side as shown in the diagram but am having confusion because the app should be compatible with different screen sizes and densities.

Basically I want all the buttons to be of the same sizes(square typically) and TextView larger and say when the screen gets bigger(e.g. rotating) only the middle(red) textView should expand and the button size should be the same while they stay in their positions.

what I have tried

I could also take a certain percentage of the screen width and apply it to the button size, but how do I make sure that icon for the button renders okay with the scaled buttons and the buttons are aligned as so:
enter image description here

i.e. their centers are aligned but different in height, equidistant from each other. also I'd have to do that programmatically instead of using the design editor or the xml.

So for this type of purposes what layout should I use and how should I set up my views?

Upvotes: 1

Views: 671

Answers (2)

Arnold Brown
Arnold Brown

Reputation: 1433

If you want this design in xml, then try this

Note : Use the dimensions from dimens folder for different screen size. Here for LinearLayout of TextView Height use from dimens folder

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:weightSum="0.9">

        <Button
            android:layout_width="0dp"
            android:background="@color/colorAccent"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="100dp"
            android:layout_margin="2dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ff00ff" />
        </LinearLayout>

        <Button
            android:layout_width="0dp"
            android:background="@color/colorAccent"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"/>
        <Button
            android:layout_width="0dp"
            android:background="@color/colorAccent"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"/>
        <Button
            android:layout_width="0dp"
            android:background="@color/colorAccent"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"/>

    </LinearLayout>

</LinearLayout>

enter image description here

Upvotes: 2

Ashish
Ashish

Reputation: 77

Use linear layout and use layout_weight only to TextView and for Buttons give fix width e.g.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<Button
    android:layout_width="@dimen/length_50"
    android:background="@color/colorPrimary"
    android:layout_height="@dimen/length_50"/>
<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    android:layout_height="@dimen/length_50"/>
<Button
    android:layout_width="@dimen/length_50"
    android:layout_marginEnd="@dimen/_10dp"
    android:background="@color/colorPrimary"
    android:layout_height="@dimen/length_50"/>
<Button
    android:layout_width="@dimen/length_50"
    android:layout_marginEnd="@dimen/_10dp"
    android:background="@color/colorPrimary"
    android:layout_height="@dimen/length_50"/>
<Button
    android:layout_width="@dimen/length_50"
    android:background="@color/colorPrimary"
    android:layout_height="@dimen/length_50"/>
</LinearLayout>

and result

5 inch screen 7 inch screen 10 inch screen

Upvotes: 1

Related Questions