Malfunction for android:color in xml for Android 10
I have a strange problem. I have made ripple_effect.xml which I use in RecyclerView.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#4F66FF" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> </shape> </item> </selector>
Single view
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:clickable="true" android:background="@color/ripple_effect"> <ImageView android:id="@+id/iv_view_category" android:layout_width="match_parent" android:layout_height="0dp" android:adjustViewBounds="true" android:clickable="false" android:focusable="false" android:padding="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Adapter
class AdapterCategory(private val list: List<Category>, private val iOnClickHandler: IonClick, private val context: Context) : androidx.recyclerview.widget.RecyclerView.Adapter<AdapterCategory.ViewHolder>() { inner class ViewHolder internal constructor(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView), View.OnClickListener { internal var picture: ImageView init { itemView.setOnClickListener(this) this.picture = itemView.findViewById(R.id.iv_view_category) } override fun onClick(v: View) { itemView.isEnabled = false itemView.postDelayed(Runnable { itemView.isEnabled = true }, 500) iOnClickHandler.onItemClick(adapterPosition) } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.view_category, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val ivImage = holder.picture val icon = ContextCompat.getDrawable(context, list[position].image) ivImage.setImageDrawable(icon) } override fun getItemCount(): Int { return list.size } interface IonClick { fun onItemClick(position: Int) } }
I have received few recording from testing devices from Google, and on few models the color #4F66FF is violet. Here is list devices where code works fine:
- Redmi 5 Plus – MIUI 11.0.2
- Motorola XT1650 – Android 7.0 Samsung
- Galaxy S9 G960U1 – Android 8
- Xperia XZ1 G8441 – Android 8
- Nokia 1 FRT -Android 8.1.0
- LG AS110 – Android 6.0.1
- Pixel 3 – Android 9
- P8 Lite – Android 5.0.1
- Pixel – Android 7.1.2
Color is wrong for these devices:
- Redmi Note 8 Pro – MUI 11
- P30 Pro – EMUI 10
- Xiaomi Mi 9t – Android 10
- Pixel 4 – Android 10
Imho as I can see problem is only on Android 10. Why? Any nice solutions to make what I want but in different way? Thanks!
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorPrimary" android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item> <item android:color="@android:color/background_light" android:state_pressed="false"> <shape android:shape="rectangle"> <solid android:color="@android:color/background_light" /> </shape> </item> </selector>
Ok, so what was missing it was attribute color for item.. Without it violet color is comming from nowhere. I have checked my project few times and I am 100% sure. I keep this topic maybe it will be valuable if not delete it. Cheers.