androidxmlkotlinandroid-custom-viewandroid-custom-attributes

Update custom view custom properties programmatically


I have custom view named CustomView which has two custom attributes att1 and att2 defined.

Inside XML I can update the attributes easily like below:

<com.example.myapp.CustomView
     android:id="@+id/custom_view"
     app:att1="value_1"
     app:att2=value_2"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

But how do I update them programmatically?

Similar to this:

val customView: CustomView = findViewById(R.id.custom_view)
customView.att1 = "value_1"
customView.att2 = "value2"

Solution

  • As mentioned by @DarShan in the comment, creating setter and getter and invalidating the view was the solution. You can refer to this question.