I'm trying to create a list of lessons. I have a RecyclerView of lesson items. I specified custom parameters like cardBackgroundColor and cardCornerRadius of CardView in lesson_item but when I run app these parameters are ignored.
This is how cardView should look
This is what I'm getting when I run the app:
Here is lesson_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/lesson_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
card_view:cardBackgroundColor="#E8DFDF"
card_view:cardCornerRadius="30dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:id="@+id/lesson_item_title"
style="@style/TextAppearance.MaterialComponents.Headline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/lesson_name"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lesson_item_desc"
style="@style/TextAppearance.MaterialComponents.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="4dp"
android:padding="10dp"
android:text="@string/lesson_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/lesson_item_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here is fragment_lessons.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".presentation.fragments.LessonsFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button_back_from_lessons_to_menu"
android:layout_width="38dp"
android:layout_height="48dp"
android:background="#ED8C3E"
android:contentDescription="button to back"
android:src="@drawable/ic_back_24"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/hello_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="#ED8C3E"
android:paddingStart="120dp"
android:text="@string/lessons"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lessons_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is LessonAdapter:
package com.example.englishease.presentation.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.example.englishease.R
class LessonAdapter(val list: List<String>, val lessonDescs: List<String>, val listener: LessonAdapter.OnItemClickListener) :
RecyclerView.Adapter<LessonAdapter.MyLesson>(){
class MyLesson(item: View) : RecyclerView.ViewHolder(item) {
val cardView: CardView = item.findViewById(R.id.lesson_card)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LessonAdapter.MyLesson {
val myItem = LayoutInflater.from(parent.context).inflate(R.layout.lesson_item, parent, false)
return LessonAdapter.MyLesson(myItem)
}
override fun onBindViewHolder(holder: LessonAdapter.MyLesson, position: Int) {
val lessonName = list[position]
val lessonDescs = lessonDescs[position]
holder.cardView.findViewById<TextView>(R.id.lesson_item_title).text = lessonName
holder.cardView.findViewById<TextView>(R.id.lesson_item_desc).text = lessonDescs
holder.itemView.setOnClickListener {
listener.onItemClickListener(list[position])
}
}
override fun getItemCount() = list.size
interface OnItemClickListener {
fun onItemClickListener(lessonName: String)
}
}
So You doing a basic mistake here
xmlns:card_view="http://schemas.android.com/tools"
You have declared card_view
as tools. Tools is not deafult name space and is meant for the developers to see the changes but it is not applied when the app is launched.
Hence you can see the background and radius in xml but not in the app when it is launched.
Do this and your problem will be solved.
app:cardBackgroundColor="#E8DFDF"
app:cardCornerRadius="30dp">