how do I change the solid color of a ripple drawable?

北战南征 提交于 2019-12-02 08:53:12

You should add an id to items to access them via java/kotlin.
check this background XML file

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#e0e0e0">
   <item android:id="@+id/fab_shape">
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle">
         <corners android:radius="25dp" />
        <solid android:color="@color/colorAccent" />
     </shape>
  </item>
</ripple>

to change the solid color of this, on constraintLayout background, this drawable XML is applied

val background = constraintLayout.background as RippleDrawable
val bgShape = background.findDrawableByLayerId(R.id.fab_shape) as GradientDrawable
bgShape.color = color

for reference read this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!