0

I have a layout with a Parent LinearLayout with weightSum of 100 and two child layouts with weights 40 and 60.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:id="@+id/cv_container"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:weightSum="100"
    android:background="@android:color/white">
    <RelativeLayout
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout1"
        android:layout_weight="40">
        <Droid.SquareImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imgPlaceholderImage"
            android:layout_centerInParent="true"
            android:scaleType="fitXY" />
        <Droid.CustomTextView
            android:text="Category"
            app:customFont="Fonts/Roboto-Bold.ttf"
            android:textSize="12sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:color="@android:color/white"
            android:id="@+id/txtCategory"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:ellipsize="end" />
    </RelativeLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout2"
        android:layout_weight="60"
        android:padding="10dp">
        <Droid.CustomTextView
            android:text=""
            app:customFont="Fonts/Roboto-Bold.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:color="@android:color/black"
            android:id="@+id/txtTitle" />
        <Droid.CustomTextView
            android:text=""
            android:textSize="14sp"
            app:customFont="Fonts/Roboto-Regular.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:color="#d5d5d5"
            android:id="@+id/txtDate" />
        <Droid.CustomTextView
            android:text=""
            app:customFont="Fonts/Roboto-Regular.ttf"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:color="@android:color/black"
            android:id="@+id/txtDetail"
            android:maxEms="4"
            android:ellipsize="end" />
    </LinearLayout>
</LinearLayout>

With this I get the layout as so :- enter image description here

My second row looks fine, but the first one looks weird. Iam using a RecyclerView.
How can I make my first row look like the second one?

Sreehari
  • 5,621
  • 2
  • 25
  • 59
hello world
  • 797
  • 1
  • 9
  • 31
  • 2
    try to replace relative layout with linear layout – Awadesh Feb 06 '16 at 05:10
  • Just set 0dp width to RelativeLayout.... – Haresh Chhelana Feb 06 '16 at 05:16
  • and also use adjustviewbounds attribute in squareimageview.if the custom squareview had this attribute.and remove scaletype attribute – Vishwa Feb 06 '16 at 05:18
  • @Awadesh I tried doing tha but it gives the same result – hello world Feb 06 '16 at 05:20
  • Make the two siblings inside your parent linearlayout as linearlayouts with "horizontal orientation" and assign weights to them. This will place them propotionately – cafebabe1991 Feb 06 '16 at 05:32
  • @cafebabe1991 I want the second layout to have vertical orientation – hello world Feb 06 '16 at 05:34
  • @HareshChhelana Tried it and gives same result. Also My second row looks fine with the same layout – hello world Feb 06 '16 at 05:37
  • @Vishwa It didnt work and my second row looks fine with my layout above. Its just the problem with first row – hello world Feb 06 '16 at 05:38
  • See this representation i made, this is what you need to do https://docs.google.com/drawings/d/172u8X2zhyqBhDLLyVi3EbxzOqByMCPDWokWHtZlN6TE/edit?usp=sharing – cafebabe1991 Feb 06 '16 at 05:51
  • @helloworld did u try to make relativelayout width as 0dip and use default imageView instead of squareimageview.So that you can get an idea on whats wrong with it.. – Vishwa Feb 06 '16 at 05:54
  • @cafebabe1991. I dont want to divide my parent linearlayout into two smaller horiontal layouts. Its a simple layout with one parent horizontal layout with two layouts with 40:60 ratio. Its a row for a recycler view which gets inflated. So In my image the second row is the same layout inflated on the second position – hello world Feb 06 '16 at 06:01
  • try below solution http://stackoverflow.com/a/35238014/2826147 @helloworld – Amit Vaghela Feb 06 '16 at 06:17
  • Inside the Parent LinearLayout, replace first child (RelativeLayout) "android:layout_width="match_parent"" to "android:layout_width="0dp"" – Sabari Feb 06 '16 at 08:37

3 Answers3

1

Try to 0dp width to RelativeLayout and whenever you have used weight properties to any layout or views you have to set width or height 0dp on respective of parent layout orientation like if parent layout is horizontal then set width 0dp or if parent layout is vertical then set height 0dp.

You not required given weightSum 100 to any layout or view which by default considering weightSum 100.

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

You must set value of width to 0dp for RelativeLayout and then weight will work. in your case because you have used a horizontal LinearLayout you must change the width of RelativeLayout to 0dp:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:id="@+id/cv_container"
android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="100"
android:background="@android:color/white">
<RelativeLayout
    android:padding="10dp"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1"
    android:layout_weight="40">
    <Droid.SquareImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imgPlaceholderImage"
        android:layout_centerInParent="true"
        android:scaleType="fitXY" />
    <Droid.CustomTextView
        android:text="Category"
        app:customFont="Fonts/Roboto-Bold.ttf"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:color="@android:color/white"
        android:id="@+id/txtCategory"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:ellipsize="end" />
</RelativeLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout2"
    android:layout_weight="60"
    android:padding="10dp">
    <Droid.CustomTextView
        android:text=""
        app:customFont="Fonts/Roboto-Bold.ttf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:color="@android:color/black"
        android:id="@+id/txtTitle" />
    <Droid.CustomTextView
        android:text=""
        android:textSize="14sp"
        app:customFont="Fonts/Roboto-Regular.ttf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:color="#d5d5d5"
        android:id="@+id/txtDate" />
    <Droid.CustomTextView
        android:text=""
        app:customFont="Fonts/Roboto-Regular.ttf"
        android:textSize="15sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:color="@android:color/black"
        android:id="@+id/txtDetail"
        android:maxEms="4"
        android:ellipsize="end" />
</LinearLayout>

To learn more about how Layout_weight works please take a look at this answer .

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
  • I tried doing this and it gives the same result. Notice that the second row has the same layout but it looks exactly like I want unlike the first row – hello world Feb 06 '16 at 05:34
  • I guess its because you have a custom image view with android:layout_width="match_parent" in your RelativeLayout. – Milad Faridnia Feb 06 '16 at 05:39
  • So is there a solution for this? Im confused as the second row of the list has the same layout but still looks fine – hello world Feb 06 '16 at 06:05
  • I think something wrong with imageView size. you have different image view sizes. – Milad Faridnia Feb 06 '16 at 06:18
  • The Images sizes are different for sure. But shouldnt the ImageView be restricted to match the parent and take only 40% of the screen irrespective of the image size? – hello world Feb 06 '16 at 06:20
  • No it does not because you've set the image view width to match_parent it does not allow its parent(RelativeLayout) to take 40% of it's parent(LinearLayout) – Milad Faridnia Feb 06 '16 at 06:24
  • layout_weight specifies how much of the "extra" space in the layout to be allocated to the View. – Milad Faridnia Feb 06 '16 at 06:28
0

check this solution, will work

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv_container"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40"
        android:orientation="vertical"
        android:padding="5dp">

        <Droid.SquareImageView
            android:id="@+id/imgPlaceholderImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@android:drawable/ic_menu_gallery" />

        <Droid.CustomTextView
            android:id="@+id/txtCategory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:color="@android:color/white"
            android:ellipsize="end"
            android:text="Category"
            android:textSize="12sp"
            app:customFont="Fonts/Roboto-Bold.ttf" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:orientation="vertical"
        android:padding="5dp">

        <Droid.CustomTextView
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:color="@android:color/black"
            android:text=""
            android:textSize="16sp"
            app:customFont="Fonts/Roboto-Bold.ttf" />

        <Droid.CustomTextView
            android:id="@+id/txtDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:color="#d5d5d5"
            android:text=""
            android:textSize="14sp"
            app:customFont="Fonts/Roboto-Regular.ttf" />

        <Droid.CustomTextView
            android:id="@+id/txtDetail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:color="@android:color/black"
            android:ellipsize="end"
            android:maxEms="4"
            android:text=""
            android:textSize="15sp"
            app:customFont="Fonts/Roboto-Regular.ttf" />
    </LinearLayout>
</LinearLayout>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142