Codementor Events

Getting Started with Jetpack Compose

Published Apr 13, 2021Last updated Apr 14, 2021
Getting Started with Jetpack Compose

Jetpack Compose is a New UI Toolkit replaces / introp with tradition XML code for android UI Development, it has wide range of support with android architecture component, lifecycle, viewmodel...

Let start with the basic example,

Note: Jetpack compose is not stable yet, it was in beta at the time of writing

I am using Android Studio Artic Fox | 2020.3.1 canary 14
Since Jetpack compose is not stable yet, you won`t get this option in stable version of android studio

2021-04-13_07-19.png

Start new jetpack compose project
2021-04-13_07-21.png

Name the Project and Package Name
2021-04-13_07-21_1.png

Finally Hit Finish

MainActivity.kt

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SampleTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
    }
}


@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    SampleTheme {
        Greeting("Android")
    }
}

you will get nice preview in android studio

2021-04-13_07-27.png

Thats all for now. meet you later with onather jetpack compose example

Thanks. stay safe, Keep Code

Discover and read more posts from Poovarasan
get started
post commentsBe the first to share your opinion
Show more replies