0

I am trying to create a ListView for my android app. I created an adapter cell's XML to be used to inflate into the ListView. However I can't fit more than two elements.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">


    <TextView
        android:id="@+id/serviceItemId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_marginLeft="16dp"
        android:layout_weight="70"
        android:text="TextView"
        android:layout_marginStart="16dp" />

    <TextView
        android:id="@+id/serviceName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_marginLeft="16dp"
        android:layout_weight="60"
        android:text="TextView"
        android:layout_marginStart="16dp" />

      <TextView
        android:id="@+id/serviceDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_marginLeft="16dp"
        android:layout_weight="60"
        android:text="TextView"
        android:layout_marginStart="16dp" />   
</LinearLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Yash Jain
  • 442
  • 7
  • 19

2 Answers2

2
  • Remove android:weightSum="100" in the parent
  • Use android:layout_width="0dp" in the children.

You can check the doc about android:weightSum:

Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

Use:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

      <TextView
        android:id="@+id/serviceItemId"
        android:layout_width="0dp"
        android:layout_weight="70"
        ..>

    <TextView
        android:layout_width="0dp"
        android:layout_weight="60"
        ../>

    <TextView
        android:layout_width="0dp"
        android:layout_weight="60"
        .. />

</LinearLayout>

enter image description here

Here you can find more info about the weight.

It is not important to set android:weightSum and you can use the weight as you prefer.

If you want to realize something like 50%-25%-25% just use:

  • android:layout_weight="2"
  • android:layout_weight="1"
  • android:layout_weight="1"
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 2
    `android:weightSum="100"` is actually good, because he can work with it like with actual percentage, but yeah while using weights there should always be `0dp` for dimension in which your Layout is oriented. – SkypeDogg Jun 24 '20 at 07:17
  • 1
    But the weightSum didn't let me work with 3 or more elements. That was my problem – Yash Jain Jun 24 '20 at 07:21
  • 2
    Because you've had set your Views to `match_parent`. I've never used percentage from `ConstraintLayout` but instead of this I was using weights with `weightSum` from `LinearLayout` and usually for more than 2 Views – SkypeDogg Jun 24 '20 at 07:23
  • 2
    @SkypeDogg It could be good (as 1.0) if you want to specify a maximum. If you use 70+60+60 in the children views it means that only some of them will be displayed. – Gabriele Mariotti Jun 24 '20 at 07:24
  • 2
    @Gabriele Mariotti yes that's true, it's obvious it's impossible to use more than 100% when whole Layout is 100% – SkypeDogg Jun 24 '20 at 07:27
  • 1
    @GabrieleMariotti I see. So I should've changed the weight to 200? – Yash Jain Jun 24 '20 at 16:53
  • 2
    @YashJain In your case 190. But simply you can remove it since it is automatic. It means that the first view is 70/200, than 60/200 and finally 60/200. But you can use the weights as you prefer, If you want 50-25-25, just use weight=2, weight=1, weight=1 – Gabriele Mariotti Jun 24 '20 at 16:56
1

in response to your comment (apologies I can't post comments yet, so will leave this explanation here) "Yes that fixed it. But how do you specify the width each takes since I can't use a scale of 100 anymore?"

you can use layout_weight as such, for your 3 TextViews, if u set them all to layout_weigh = 1 this effectively means that they will all occupy the same amount of space.

lets say that you want the first TextView to be perhaps 2x bigger than the other two TextViews, you can simply set it as such

TV1 , layout_weight =2

TV2 , layout_weight =1

TV3 , layout_weight =1

so instead of big percentages, you can play around with the weights with this understanding. Hope this helps!

Found a great stackoverflow post on this https://stackoverflow.com/a/4517358/4377908