0

I am learning Android + Kotlin and trying to make an app to show a list of countries with current COVID stats. Right now it's just a main screen with a Recyclerview that lists individual items in LinearLayouts containing Textviews. Below are my layout files:

fragment_overview.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="viewModel"
            type="com.example.coronastats.overview.OverviewViewmodel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/overview_recyclerview"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:listData="@{viewModel.stats}"
            app:layoutManager="LinearLayoutManager"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

overview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">

    <data>
        <variable
            name="country"
            type="com.example.coronastats.network.CountryData" />
    </data>

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

        <TextView
            android:id="@+id/country_name_tv"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFEB3B"
            android:gravity="center"
            android:text="@{country.country}"
            tools:text="country" />

        <TextView
            android:id="@+id/infected_tv"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FF9800"
            android:gravity="center"
            android:text="@{country.infected}"
            tools:text="infected" />

    </LinearLayout>
</layout>

And my layout is not what I expect it to be. Fields are all moved to the left as if I was using wrap_content instead of match_parent. I have tried all possible combinations of constraints and width/height. The weird thing is that if I change the app:layoutManager to "GridLayoutManager", the result is what I expect it to be using LinearLayoutManager. I don't see the logic here. Can anyone explain?

Anton V
  • 21
  • 2
  • would be easier to resolve when you post `adapter`s code, but seems like above link should resolve your problem – snachmsm Feb 11 '21 at 06:29

1 Answers1

0

You have given layout_weight to the textviews but you have not given the weight_sum to the LinearLayout in overview_item.xml

Aquib Ali
  • 55
  • 4