1+ package com.pureswift.swiftandroid
2+
3+ import android.view.ViewGroup.LayoutParams.MATCH_PARENT
4+ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
5+ import android.widget.ImageView
6+ import android.widget.LinearLayout
7+ import android.widget.TextView
8+ import androidx.compose.foundation.layout.Column
9+ import androidx.compose.foundation.layout.padding
10+ import androidx.compose.runtime.Composable
11+ import androidx.compose.runtime.mutableStateOf
12+ import androidx.compose.runtime.remember
13+ import androidx.compose.material3.Text
14+ import androidx.compose.material3.Button
15+ import androidx.compose.runtime.mutableIntStateOf
16+ import androidx.compose.ui.Modifier
17+ import androidx.compose.ui.tooling.preview.Preview
18+ import androidx.compose.ui.unit.dp
19+ import androidx.compose.ui.viewinterop.AndroidView
20+ import androidx.core.content.ContextCompat
21+
22+ @Preview(showBackground = true )
23+ @Composable
24+ fun EmbeddedAndroidViewDemo () {
25+ Column {
26+ val state = remember { mutableIntStateOf(0 ) }
27+
28+ // widget.ImageView
29+ AndroidView (factory = { ctx ->
30+ ImageView (ctx).apply {
31+ val drawable = ContextCompat .getDrawable(ctx, R .drawable.ic_launcher_background)
32+ setImageDrawable(drawable)
33+ }
34+ })
35+
36+ // Compose Button
37+ Button (onClick = { state.value++ }) {
38+ Text (" MyComposeButton" )
39+ }
40+
41+ // widget.Button
42+ AndroidView (factory = { ctx ->
43+ // Here you can construct your View
44+ android.widget.Button (ctx).apply {
45+ text = " MyAndroidButton"
46+ layoutParams = LinearLayout .LayoutParams (MATCH_PARENT , WRAP_CONTENT )
47+ setOnClickListener {
48+ state.value++
49+ }
50+ }
51+ }, modifier = Modifier .padding(8 .dp))
52+
53+ // widget.TextView
54+ AndroidView (factory = { ctx ->
55+ // Here you can construct your View
56+ TextView (ctx).apply {
57+ layoutParams = LinearLayout .LayoutParams (MATCH_PARENT , WRAP_CONTENT )
58+ }
59+ }, update = {
60+ it.text = " You have clicked the buttons: " + state.value.toString() + " times"
61+ })
62+ }
63+ }
0 commit comments