Search code examples
androidkotlincardviewanko

Anko CardView radius not work


I want to create cardView with anko and set cornerRadius param to it. But When I try to do - no such differents come. In main class I do this:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
    with(applicationContext!!) {
        listView = listView {
            layoutParams = ViewGroup.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
            dividerHeight = 20
        }
    }
    listView?.adapter = CustomAdapter(forms)

    return listView!!
}

In CustomAdapter I return cardView like this:

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val currentForm = getItem(position)
    return convertView ?: createCardView(parent!!.context, currentForm)
}

private fun createCardView(context: Context, form: FormField): View =
        with(context) {
            frameLayout {
                cardView {
                    layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT).apply {
                        leftMargin = dip(10)
                        rightMargin = dip(10)
                        topMargin = dip(5)
                        bottomMargin = dip(5)

                    }
                    backgroundColor = Color.WHITE
                    radius = dip(8).toFloat()

                    verticalLayout {
                        // title
                        textView {
                            text = form.title
                            textColor = ContextCompat.getColor(context, R.color.colorPrimary)
                            textSize = 20f
                        }.lparams(width = matchParent) {
                            leftMargin = dip(15)
                            topMargin = dip(10)
                            bottomMargin = dip(10)
                        }
                        // subtitle
                        textView {
                            if (form.subTitle != null) {
                                text = form.subTitle
                                textColor = ContextCompat.getColor(context, R.color.colorPrimary)
                                textSize = 12f
                                visibility = View.VISIBLE
                            } else {
                                visibility = View.GONE
                            }
                        }.lparams(width = matchParent) {
                            leftMargin = dip(15)
                            topMargin = dip(10)
                            bottomMargin = dip(10)
                        }

                    }.lparams(width = matchParent, height = matchParent)
                }
            }
        }

I try to call 'radius' setter in different ways and values, but result is always like this enter image description here

As you can see - the corners are always rectangle. What I want - to round the corners with Anko enter image description here

Small p.s. - when I return from getView inflated xml layout with same cardview - it has rounded corners.


Solution

  • So, the problem was in

    backgroundColor = Color.WHITE
    

    It was set the default background DRAWABLE param to ColorDrawable instead inner RoundRectDrawable. So, when I change this row to :

    background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
    

    All start to work and corners become to be rounded