0

I'm creating a customview that the imageview and the text is set dynamically is like a box with an imageView and a value of that box. This is my custom view :

class MyBoxImage@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr) {


    init {
        LayoutInflater.from(context).inflate(R.layout.my_box_item, this, true)
    }

}

Here is saying

None of the following functions can be called with the arguments supplied.

Do I need to add here the two methods to set the image and text? My layout is this :

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="244"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.06"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/iv_face"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/diamonds" />

</androidx.constraintlayout.widget.ConstraintLayout>

My goal is to create different MyBoxImage but with different imageView and different value coming from a List

StuartDTO
  • 783
  • 7
  • 26
  • 72

1 Answers1

0

When you inflate any view and tell inflater to attach it to parent it basically calls addView on parent to add that created view in parent. You can see how inflater works on link.

So as your adding your new inflated view in ConstraintLayout you have to add your constraints to view. I would rather suggest to inflate the view but dont attach and attach the view with proper constraints.

To correctly add view to your constraintlayout follow link. Add a view programmatically in ConstraintLayout

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54