Open In App

Android Jetpack Compose – Interoperability Using Compose in XML Layouts

Improve
Improve
Like Article
Like
Save
Share
Report

Writing UI in jetpack compose can be fun, but can be difficult to migrate your whole project into Compose at one time. Fortunately, Jetpack Compose provides Interoperability api to use compose in existing XML views so that you can migrate to compose slowly.

Prerequisites:

  1. Knowledge of Jetpack Compose.
  2. Knowledge of Kotlin
  3. Knowledge of Android’s View.

Jetpack compose provides a ComposeView which we can use in XML and then write compose in our View.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Add a ComposeView in xml layout wherever we want to use the compose in our XML view. For simplicity, we will be using it in a Linear Layout with just a text in it.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/black" />
  
    <!-- Compose view provided by jetpack compose -->
    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
</LinearLayout>


Step 3: Working with the MainActivity.kt File

Create a simple composable function that we want to use in the existing XML layout and write the following code.

Kotlin




@Composable
fun Greeting(text: String) {
    Text(
        text = "$text!", color = Color(0xFF0F9D58), 
          fontStyle = FontStyle.Italic, 
        fontSize = 30.sp
    )
}


Create a variable named composeView in the top level of MainActivity class

Kotlin




lateinit var composeView: ComposeView


then assign it after inflating the view (after setContentView call)

Kotlin




composeView = findViewById(R.id.compose_view)


and then use this setContent method in composeView context to use composable in our existing XML layout

Kotlin




composeView.setContent {
            Box(
                modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center
            ) {
                Greeting("Hello Geeks for geeks")
            }
        }


Final MainActivity code.

Kotlin




import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.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.font.FontStyle
import androidx.compose.ui.unit.sp
  
class MainActivity : AppCompatActivity() {
    lateinit var composeView: ComposeView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        composeView = findViewById(R.id.compose_view)
        composeView.setContent {
            Box(
                modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center
            ) {
                Greeting("Hello Geeks for geeks")
            }
        }
    }
}
  
@Composable
fun Greeting(text: String) {
    Text(
        text = "$text!", color = Color(0xFF0F9D58), fontStyle = FontStyle.Italic, fontSize = 30.sp
    )
}


When we run the app we can see both XML’s Textview and Jetpack compose’ Text in the same page.

Output:

Composeview in XML layout

 



Last Updated : 15 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads