-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathComposeInClassicAndroidActivity.kt
102 lines (96 loc) · 4.71 KB
/
ComposeInClassicAndroidActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.example.jetpackcompose.interop
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.height
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.jetpackcompose.R
import com.example.jetpackcompose.core.colors
class ComposeInClassicAndroidActivity : AppCompatActivity() {
private lateinit var containerLayout: ComposeView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_compose_in_classic_android)
containerLayout = findViewById(R.id.compose_view)
// We make use of the setContent extension function that's available on the ComposeView
// view that is capable of hosting a @Composable function inside it. This allows us to
// render composables inside classic android views.
containerLayout.setContent {
CardComponentWithMessage()
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun CardComponentWithMessage() {
// Column is a composable that places its children in a vertical sequence. You
// can think of it similar to a LinearLayout with the vertical orientation.
// In addition we also pass a few modifiers to it.
// You can think of Modifiers as implementations of the decorators pattern that are
// used to modify the composable that its applied to. In this example, we configure the
// Column composable to occupuy the entire available width and height using
// Modifier.fillMaxSize() and center the content inside the Column using the appropriate
// veritical arrangement & horizontal alignment.
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
Card(
modifier = Modifier.fillMaxWidth()
.height(200.dp)
.padding(16.dp),
backgroundColor = colors[1],
) {
// Text is a predefined composable that does exactly what you'd expect it to - display text on
// the screen. It allows you to customize its appearance using style, fontWeight, fontSize, etc.
Text(
"This is an example of a Jetpack Compose composable inside a classic Android " +
"view",
style = TextStyle(
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.W900,
fontSize = 14.sp,
color = Color.Black
),
modifier = Modifier.padding(16.dp).fillMaxWidth()
)
}
}
}
/**
* Android Studio lets you preview your composable functions within the IDE itself, instead of
* needing to download the app to an Android device or emulator. This is a fantastic feature as you
* can preview all your custom components(read composable functions) from the comforts of the IDE.
* The main restriction is, the composable function must not take any parameters. If your composable
* function requires a parameter, you can simply wrap your component inside another composable
* function that doesn't take any parameters and call your composable function with the appropriate
* params. Also, don't forget to annotate it with @Preview & @Composable annotations.
*/
@Composable
@Preview
fun CardComponentWithMessagePreview() {
CardComponentWithMessage()
}