Can someone suggest any good references for conver...
# compose
w
Can someone suggest any good references for converting composable to android views, official documentation is pretty basic. I want to explore the best approaches to do the same .
Copy code
@Composable
fun CustomTextViewCompose(
    customText: String
) {
    Text(text = customText)
}

--------------------------------

class CustomTextView(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {

    var textString by mutableStateOf("")

    @Composable
    override fun Content() {
        CustomTextViewCompose(customText = textString)
    }
}
This is a very pretty basic sample , when it comes to button and all you may have to update the text and do other things to , how to handle those scenarious?
a
You've got the idea. 🙂 Snapshot state backed view properties for anything you would pass as a parameter to the composable
w
But is there a way to make these state backed properties as mandatory . For ex
Copy code
<app.textview.CustomTextView-->
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
android:id="@+id/tvCustom"/>
binding.tvCustom.apply{ //here how can I make this property as mandatory //,if I didn’t pass textString how }.
@ildar.i [Android], Sorry to tag you , but would like to know if you will be able to help here.
🤷‍♂️ 1
m
I dont understand the question. Can you reformulate?
a
no view properties are mandatory in the way composable function parameters are mandatory. The best you'll be able to do is make your state-backed properties nullable and null-check them during composition. If you wanted you could do something like
Copy code
val currentTextString = textString
if (currentTextString == null) {
  Text("textString wasn't set!", Modifier.background(Color.Red), style = TextStyle(color = Color.Yellow))
} else { ...
🙏 1