mayyyo
mayyyo

Reputation: 535

width of lines in linear layout in percentage

Is there any option for percentage width in android? I'm trying to set my 2 lines to 40% and text to 20% but i really don't know how to achieve this. i have for now fixed width.

            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="14dp"
                android:id="@+id/line2"
                android:background="@drawable/line"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/line" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/or"
                android:textAllCaps="true"
                android:gravity="center"
                android:id="@+id/textView"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/imageView"
                android:background="@drawable/line"
                android:layout_gravity="center_horizontal"
                android:layout_weight="2.18"
                android:contentDescription="@string/line" />

        </LinearLayout>

This is screen from android studio for better understanding : screen

Upvotes: 1

Views: 2533

Answers (2)

MarchingHome
MarchingHome

Reputation: 1204

You can use the attribute android:layout_weight for that. Like this:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="5" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="14dp"
            android:id="@+id/line2"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="2"
            android:contentDescription="@string/line" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/or"
            android:textAllCaps="true"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/textView"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="2"
            android:contentDescription="@string/line" />

    </LinearLayout>

Note that I've added a weight for each element under LinearLayout and that I've changed android:weightSum in LinearLayout itself to 5.

Upvotes: 1

Akash Moradiya
Akash Moradiya

Reputation: 3322

try like this,

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="14dp"
            android:id="@+id/line2"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.40"
            android:contentDescription="@string/line" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/or"
            android:textAllCaps="true"
            android:gravity="center"
            android:layout_weight="0.20"
            android:id="@+id/textView"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="14dp"
            android:id="@+id/imageView"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.40"
            android:contentDescription="@string/line" />

    </LinearLayout>

Upvotes: 0

Related Questions