Saurabh
Saurabh

Reputation: 41

How to set these four buttons in one line even if app runs on any screen size?

I tried this code but text on these buttons are messed in my phone; although it is fine in Nexus 5 emulator....! How can I put these 4 buttons one line dynamically so that they can look same on any screen size...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:weightSum="8" >

    <Button
        android:id="@+id/bback"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Back" />

    <Button
        android:id="@+id/bforward"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Forward" />

    <Button
        android:id="@+id/brefresh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Refresh" />

    <Button
        android:id="@+id/bclear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Clear History" />
</LinearLayout>

Upvotes: 2

Views: 170

Answers (3)

Amitabha Biswas
Amitabha Biswas

Reputation: 3291

EDITTEED Please try with this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        >

        <Button
            android:id="@+id/bback"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Back" />

        <Button
            android:id="@+id/bforward"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Forward" />

        <Button
            android:id="@+id/brefresh"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Refresh" />

        <Button
            android:id="@+id/bclear"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Clear History" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Myat Min Soe
Myat Min Soe

Reputation: 807

set android:layout_width to 0dp instead of fill_parent for all buttons.

Upvotes: 3

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3688

Try this, it will keep the button size as same in any device , also spread the buttons with equal distance on any device,

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

            <LinearLayout
                android:id="@+id/1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/bback"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:text="Back"
                    android:layout_gravity="center" />
            </LinearLayout>

            //same as layout 2,3,4 and respective button with in it


        </LinearLayout>

Upvotes: 0

Related Questions