casillas
casillas

Reputation: 16793

Layout_weight does not work properly

I have the followin xml where I want imageview and then two textview and then imageview again, but it does not work properly.

Here is what I planned to have:

enter image description here

Here is what I am getting:

enter image description here

<LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center_horizontal"
     android:orientation="horizontal"
     android:clickable="true"
     android:onClick="userProfileDetailAction"
     android:id="@+id/userProfileDetail"
     android:paddingTop="5dp"
     android:paddingBottom="5dp">

            <com.example.eyeniaras.satdostum.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_weight="0.3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/circle_shape"
                app:civ_border_width="0.2dp"
                app:civ_border_color="@color/dark"
                android:id="@+id/userImage"
                android:scaleType="centerCrop"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.6">

                <TextView
                    android:id="@+id/userName"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:textColor="@android:color/black"
                    android:background="#ffffff"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <TextView
                    android:id="@+id/userRegistrationDate"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:textColor="@android:color/black"
                    android:background="#ffffff"
                    android:textSize="18sp"
                    android:text = "45 days ago"
                    android:layout_weight="1"/>
            </LinearLayout>

            <ImageView
                android:id="@+id/userClick"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="0.1"
                android:background="@drawable/list_arrow" />

 </LinearLayout>

Upvotes: 2

Views: 1582

Answers (12)

Rohit  Rathore
Rohit Rathore

Reputation: 402

use can following thing for weight ..i think this answer is helpful for you.. xml image this xml

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="30"
        android:background="@drawable/background"
        android:gravity="center" >

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/images" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="10" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:background="@drawable/background"
                android:gravity="center" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Firstname" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:background="@drawable/background"
                android:gravity="center" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lastname" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="30"
        android:background="@drawable/background"
        android:gravity="center" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/download" />
    </LinearLayout>
</LinearLayout>

and this code for background past in drawable/background
<?xml version="1.0" encoding="UTF-8"?>

<!-- res/drawable/rounded_edittext.xml -->

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="12dp"
    android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="0.3dp"
        android:color="#797979" />`enter code here`
</shape>

Upvotes: 0

SevenHuang
SevenHuang

Reputation: 31

<LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="100"
                android:layout_weight="60">

android:layout_width="match_parent",this layout's width is match_parent,so it filled up everything else,change to wrap_content,just like

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userProfileDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="userProfileDetailAction"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:weightSum="100" >

<ImageView  
    android:id="@+id/userImage"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="20"
    android:background="@drawable/ic_launcher"
    android:scaleType="centerCrop"
    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:layout_weight="60"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:background="#ffffff"
        android:text="46 days ago"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/userRegistrationDate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:background="#ffffff"
        android:text="45 days ago"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />
</LinearLayout>

<ImageView  
    android:id="@+id/userImage"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="20"
    android:background="@drawable/ic_launcher"
    android:scaleType="centerCrop"
    />

</LinearLayout>

Upvotes: 1

Amit Vaghela
Amit Vaghela

Reputation: 22945

you need to use android:layout_weight for both images and textview so here is solution,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userProfileDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="5dp"
    android:clickable="true"
    android:onClick="userProfileDetailAction"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.4">

        <com.example.eyeniaras.satdostum.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/userImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_shape"
            android:scaleType="centerCrop"
            app:civ_border_color="@color/dark"
            app:civ_border_width="0.2dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        android:weightSum="2">

        <TextView
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Hi"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6">

        <ImageView
            android:id="@+id/userClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/list_arrow"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Harry Sharma
Harry Sharma

Reputation: 2200

Don't use weightsum at all and then assign weight according to requirement to all main views.It will definitely work.why to make things complex.I have done it for you.Check it below.

  <LinearLayout
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:clickable="true"
  android:onClick="userProfileDetailAction"
android:id="@+id/userProfileDetail"
android:paddingTop="5dp"
android:paddingBottom="5dp">

              <com.example.eyeniaras.satdostum.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_weight="0.2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/circle_shape"
                app:civ_border_width="0.2dp"
                app:civ_border_color="@color/dark"
                android:id="@+id/userImage"
                android:scaleType="centerCrop"/>

               <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"

                android:layout_weight="0.6">

                   <TextView
                       android:id="@+id/userName"
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:layout_gravity="center"
                       android:textColor="@android:color/black"
                       android:background="#ffffff"
                       android:textSize="18sp"
                       android:textStyle="bold"
                       android:layout_weight="1"/>

                   <TextView
                       android:id="@+id/userRegistrationDate"
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:layout_gravity="center"
                       android:textColor="@android:color/black"
                       android:background="#ffffff"
                       android:textSize="18sp"
                       android:text = "45 days ago"
                       android:textStyle="bold"
                       android:layout_weight="1"/>
                </LinearLayout>

              <ImageView
                android:id="@+id/userClick"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_weight="0.2"
                android:background="@drawable/list_arrow" />

      </LinearLayout>

Upvotes: 1

Usman
Usman

Reputation: 349

According to Image.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userProfileDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:clickable="true"
    android:onClick="userProfileDetailAction"
    android:orientation="horizontal"
    android:padding="5dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.example.eyeniaras.satdostum.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/userImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_shape"
            android:scaleType="centerCrop"
            app:civ_border_color="@color/dark"
            app:civ_border_width="0.2dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        android:weightSum="2">

        <TextView
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/userClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/list_arrow"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

MikeT
MikeT

Reputation: 56938

I've taken your code and stripped it down. The following, at least in studio design, has the layout you've described. Perhaps progressively change/adapt this; checking as you go along that it maintains the desired layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal"
    android:clickable="true"
    android:weightSum="100"
    android:onClick="userProfileDetailAction"
    android:id="@+id/userProfileDetail"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

  <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="500dp"
        android:layout_height="100dp"
        android:weightSum="100"
        android:layout_weight="60">

        <TextView
            android:id="@+id/userName"
            android:layout_width="500dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:textColor="@android:color/black"
            android:background="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_weight="50"/>

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="500dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:textColor="@android:color/black"
            android:background="#ffffff"
            android:textSize="18sp"
            android:text = "45 days ago"
            android:textStyle="bold"
            android:layout_weight="50"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/userClick"
        android:layout_width="100dp"
        android:layout_height="100dp" />

</LinearLayout>

Note I've done away with things like width="match_parent", which may have been an issue, as an example.

Upvotes: 1

Aritra Roy
Aritra Roy

Reputation: 15615

This is the perfect scenario to make use of PercentageRelativeLayout. The best thing about this layout is that you can use percentage based dimensions which is super easy.

Here is a quick example of it,

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout>

This is very simple and intuitive to use and is optimized for various use cases too.

Upvotes: 1

user5912566
user5912566

Reputation:

You can use The following Code:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical"
        android:weightSum="1" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="hello" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="hello" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Bajirao Shinde
Bajirao Shinde

Reputation: 1354

it is because of width make it 0dp..copy the bellow code it works perfect

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:clickable="true"
        android:weightSum="100"
        android:onClick="userProfileDetailAction"
        android:id="@+id/userProfileDetail"
        android:paddingTop="5dp"
        android:paddingBottom="5dp">

                      <com.example.eyeniaras.satdostum.CircleImageView
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        android:layout_weight="20"
                        android:layout_width="0dp"
                        android:layout_height="10dp"
                        android:background="@drawable/circle_shape"
                        app:civ_border_width="0.2dp"
                        app:civ_border_color="@color/dark"
                        android:id="@+id/userImage"
                        android:scaleType="centerCrop"/>

                       <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="0dp"
android:orientation="vertical"
                        android:layout_height="match_parent"
                        android:weightSum="100"
                        android:layout_weight="60">

                           <TextView
                               android:id="@+id/userName"
                               android:layout_width="wrap_content"
                               android:layout_height="0dp"
                               android:layout_gravity="center"
                               android:textColor="@android:color/black"
                               android:background="#ffffff"
                               android:textSize="18sp"
                               android:textStyle="bold"
                               android:layout_weight="50"/>

                           <TextView
                               android:id="@+id/userRegistrationDate"
                               android:layout_width="wrap_content"
                               android:layout_height="0dp"
                               android:layout_gravity="center"
                               android:textColor="@android:color/black"
                               android:background="#ffffff"
                               android:textSize="18sp"
                               android:text = "45 days ago"
                               android:textStyle="bold"
                               android:layout_weight="50"/>
                        </LinearLayout>

                      <ImageView
                        android:id="@+id/userClick"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_weight="20"
                        android:background="@drawable/list_arrow" />

     </LinearLayout>

Upvotes: 1

Vishal Thakkar
Vishal Thakkar

Reputation: 2127

i was changed your code check it

change image View as you require and replace your code with this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:clickable="true"
android:weightSum="100"
android:onClick="userProfileDetailAction"
android:id="@+id/userProfileDetail"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<ImageView
    android:layout_weight="20"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/userImage"
    android:src="@android:drawable/btn_star_big_on"
    android:scaleType="centerCrop"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:gravity="center_vertical"
    android:layout_weight="60">

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center" android:text="hey hi"
        android:textColor="@android:color/black"
        android:background="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_weight="50"/>

    <TextView
        android:id="@+id/userRegistrationDate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:textColor="@android:color/black"
        android:background="#ffffff"
        android:textSize="18sp"
        android:text = "45 days ago"
        android:textStyle="bold"
        android:layout_weight="50"/>
</LinearLayout>

<ImageView
    android:id="@+id/userClick"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:src="@android:drawable/btn_star"
    android:layout_weight="20"  /></LinearLayout>

Upvotes: 1

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

Inside linearlayout second chance follow code.

            <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="60">

Upvotes: 1

masoud vali
masoud vali

Reputation: 1536

inside linearlayout must have 0 width

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:weightSum="100"
                android:layout_weight="60">

Upvotes: 2

Related Questions