![]() |
VOOZH | about |
Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools.
A sample video is given below to get an idea about what we are going to do in this article.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio using Jetpack Compose please refer to: How to Create a New Project in Android Studio Canary Version with Jetpack Compose.
Step 2: Letâs first review the build.gradle(module level)
Now go to the module-level build.gradle file & check on dependencies. If any of the dependencies are missing add them from the below snippet. Remember to double-check this file that everything is included. If something is missing just add those blocks from the below snippets.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.textvariant'
compileSdk 33
defaultConfig {
applicationId "com.example.textvariant"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation 'androidx.appcompat:appcompat:1.5.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC"
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
}
Step 3: Review the build.gradle(project level)
Now go to the project-level build.gradle file & check on the buildscript code block. If any of the requirements are missing then add them from the below snippet.
buildscript {
ext {
kotlin_version = '1.0.1-2'
compose_version = '1.1.0-rc01'
}
// Requirements
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" //dependency
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.0-alpha01' apply false
id 'com.android.library' version '7.3.0-alpha01' apply false
id 'org.jetbrains.kotlin.android' version '1.6.0' apply false
}
Step 4: Rename MainActivity.kt to CustomTextActivity.kt
We can put the same code to MainActivity.kt as well, but it's a good idea to create or rename the file to reflect its role. Once you change this we also need to modify the AndroidManifest.xml activity tag to the renamed file since the default is MainActivity. You can refer to the below snippet of AndroidManifest.xml.
Step 5: Importing necessary modules
It's good practice to import only the necessary modules rather than importing all the modules and using only a few.
Step 6: Implement AppCompatActivity() to class CustomTextActivity
SetContent() block will set the activity component function as the root view of the all activities that we declared. The separate composable functions for each text variant will have their respective logic.
Step 7: Create composable functions for making unique customizations of texts in different styles
Before we go, there are a few things you should be aware of:
Step 7: If you want to preview your CustomTextActivity then continue else you can skip it
Significance of @preview and @composable annotations :
Step 8: Complete Code Snippet
As we can see with the help of jetpack compose we can make different customization with their own unique style of formatting.