https://kotlinlang.org logo
e

elye

11/16/2021, 11:21 AM
Can I wrap Jetpack Compose inside an Android Custom View like below?
Copy code
class CustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr) {

    // Some Jetpack Compose drawn item

}
I have also check in https://stackoverflow.com/q/69987040/3286489
d

Dmitrii Smirnov

11/16/2021, 12:08 PM
You have to inherit from
AbstractComposeView
and override
Content
function
e

elye

11/16/2021, 12:27 PM
@Chris Fillmore, thanks. The link only mentioned putting the Compose in Activity/Fragment/ViewHolder…. but not in a View. Maybe I miss something?
d

Dmitrii Smirnov

11/16/2021, 12:28 PM
Copy code
class CustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : AbstractComposeView(context, attrs, defStyleAttr) {

    @Composable
    override fun Content {
    //compose code here
    }

}
🤩 1
☝️ 1
e

elye

11/16/2021, 12:29 PM
That looks promising… @Dmitrii Smirnov. Let me check that out 🙌 … Thank you so much!! 🙏