Simpli f ies and accelerates UI development on Android by writing less code. As app state changes, your UI automatically changes. Written in Kotlin. WHAT IS JETPACK COMPOSE?
but annotated with @Composable. This enables your function to call other @Composable functions within it. Able to make generic containers, that can take another @Composable functions as parameters. COMPOSABLE
$name!" ) } class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState ) setContent { AndDev360Theme { Surface(color = MaterialTheme.colors.background) { Greeting("360|AnDev" ) } } } } } Instead of passing XML as you’d do in traditional View system, you call Composable functions within it.
{ Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }
{ Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }
{ Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }
{ Text ( text = "Hello $name!" , modi fi er = Modi fi e r .padding(24.dp ) .background(color = Color.Yellow, shape = RectangleShape ) .clickable { Toast.makeText ( this , "Click event" , Toast.LENGTH_SHORT ).show( ) } . fi llMaxSize( ) ) } }
Row(modi fi er = Modi fi er. fi llMaxWidth()) { Text ( text = "Android" , modi fi er = Modi fi er.background(color = Color.Red ) ) Text(text = "loves compose" ) } } }
> <TextVie w android:id="@+id/hello_world" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello Android!" / > <androidx.compose.ui.platform.ComposeVie w android:id="@+id/compose_view" android:layout_width="match_parent" android:layout_height="match_parent" / > </LinearLayout> class ExampleFragment : Fragment() { override fun onCreateView ( in fl ater: LayoutIn fl ater , container: ViewGroup? , savedInstanceState: Bundle ? ): View { // In fl ate the layout for this fragmen t return in fl ater.in fl ate ( R.layout.fragment_example, container, fals e ).apply { fi ndViewById<ComposeView>(R.id.compose_view).setContent { // In Compose worl d MaterialTheme { Text("Hello Compose!" ) } } } } }
> <TextVie w android:id="@+id/hello_world" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello Android!" / > <androidx.compose.ui.platform.ComposeVie w android:id="@+id/compose_view" android:layout_width="match_parent" android:layout_height="match_parent" / > </LinearLayout> class ExampleFragment : Fragment() { override fun onCreateView ( in fl ater: LayoutIn fl ater , container: ViewGroup? , savedInstanceState: Bundle ? ): View { // In fl ate the layout for this fragmen t return in fl ater.in fl ate ( R.layout.fragment_example, container, fals e ).apply { fi ndViewById<ComposeView>(R.id.compose_view).setContent { // In Compose worl d MaterialTheme { Text("Hello Compose!" ) } } } } }
{ mutableStateOf(0) } // Adds view to Compose AndroidView ( modi fi er = Modi fi er. fi llMaxSize(), // Occupy the max size in the Compose UI tree factory = { context - > // Creates custom view CustomView(context).apply { // Sets up listeners for View -> Compose communication myView.setOnClickListener { selectedItem.value = 1 } } } , update = { view - > // View's been in fl ated or state read in this block has been updated // Add logic here if necessary } ) }
androidx-main/compose/docs/compose-api-guidelines.md) Adopting Jetpack compose in your app(https://developer.android.com/jetpack/ compose/interop) RESOURCES