skip to Main Content

I’m a beginner Android Dev and I can’t make the card layout display all habits. I tried swapping walk and water and I got only walk.

Am I missing something or maybe something is incorrect?
HabitsAdapter class

    class HabitAdapter(private val habits: List<Habit>) :
    RecyclerView.Adapter<HabitAdapter.HabitViewHolder>() {

    class HabitViewHolder(val card: View) :
        RecyclerView.ViewHolder(card) {
        fun bind(habit: Habit) {
            card.findViewById<TextView>(R.id.text_view_title).text = habit.title
            card.findViewById<TextView>(R.id.text_view_description).text =
                habit.description
            card.findViewById<ImageView>(R.id.image_view_icon)
                .setImageResource(habit.image)

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HabitViewHolder {
        return HabitViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.single_item, parent, false)
        )
    }

    override fun onBindViewHolder(holder: HabitViewHolder, position: Int) {
        holder.bind(habits[position])

    }


    override fun getItemCount(): Int = habits.size
}

Habits class

    data class Habit(val title: String, val description: String, val image: Int)

fun getSampleHabits(): List<Habit> {
    return listOf(
        Habit(
            "Drink Water",
            "A refreshing glass of water gets you ready for a long day ahead",
            R.drawable.water
        ),
        Habit(
            "Go for a walk",
            "A nice walk helps clear your mind for a long day ahead",
            R.drawable.walk
        )
    )
}

MainActivity class

    class MainActivity : AppCompatActivity() {
    private lateinit var mainBinding: ActivityMainBinding
    private lateinit var itemBinding: SingleItemBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)

        mainBinding.recyclerViewList.layoutManager = LinearLayoutManager(this)
        mainBinding.recyclerViewList.adapter = HabitAdapter(getSampleHabits())

    }
}

MainActivity XML

<androidx.recyclerview.widget.RecyclerView 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/single_item"
    android:id="@+id/recycler_view_list"/>

XML layout file for a single card which holds the contents of the habit (i.e. title, description and icon)

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        app:cardCornerRadius="12dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_view_icon"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:scaleType="centerCrop"
                tools:src="@drawable/water" />

            <TextView
                android:id="@+id/text_view_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="12dp"
                android:layout_toEndOf="@id/image_view_icon"
                android:textSize="28sp"
                tools:text="Drink Water" />
            <TextView
                android:id="@+id/text_view_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Drink Water"
                android:layout_toEndOf="@id/image_view_icon"
                android:layout_below="@id/text_view_title"
                android:layout_marginStart="8dp"/>

        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</ScrollView>

2

Answers


  1. Check single_item layout, its height might be match_parent and that might cause the problem.

    Login or Signup to reply.
  2. This is how your single_item.xml should look like . Since you had kept height of ScrollView as match parent it was occupying the whole view and there was no place to recycle hence making it wrap content solves the issue .

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            app:cardCornerRadius="12dp">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:id="@+id/image_view_icon"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:scaleType="centerCrop" />
    
                <TextView
                    android:id="@+id/text_view_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="12dp"
                    android:layout_toEndOf="@id/image_view_icon"
                    android:textSize="28sp"
                    tools:text="Drink Water" />
                <TextView
                    android:id="@+id/text_view_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="Drink Water"
                    android:layout_toEndOf="@id/image_view_icon"
                    android:layout_below="@id/text_view_title"
                    android:layout_marginStart="8dp"/>
    
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
    </ScrollView>
    ```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search