> For the complete documentation index, see [llms.txt](https://opentune.gitbook.io/undefined/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://opentune.gitbook.io/undefined/basics/editor.md).

# Editor

now let's get to the most exciting part. start writing code in the editor.

code example:

<figure><img src="/files/82UUlfzxv2YIV1BlplsY" alt=""><figcaption></figcaption></figure>

### *<mark style="color:purple;">small guide and theory</mark>*

***

## **Jetpack Compose Quick Guide**

Jetpack Compose is a modern, fully declarative UI toolkit for building native Android applications. It allows you to define UI components using Kotlin code, eliminating the need for XML layouts.

### **1. Setting Up Jetpack Compose**

First, make sure you have the required dependencies in your `build.gradle` files.

#### **Project-level `build.gradle`**

```gradle
buildscript {
    ext {
        compose_version = '1.5.0'
    }
    dependencies {
        classpath "androidx.compose:compose-gradle-plugin:$compose_version"
    }
}
```

#### **App-level `build.gradle`**

```gradle
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'androidx.compose'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.example.composeapp"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    composeOptions {
        kotlinCompilerExtensionVersion '1.5.0'
        kotlinCompilerVersion '1.8.10'
    }
}

dependencies {
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material3:material3:$compose_version"
    implementation "androidx.compose.ui:tooling-preview:$compose_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.0"
    implementation "androidx.activity:activity-compose:1.5.0"
    // other dependencies
}
```

### **2. Basic Structure**

In Jetpack Compose, UI is defined using composable functions. These functions are marked with the `@Composable` annotation and describe the UI components.

#### **Creating a Simple Composable**

A basic composable function could look like this:

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

You can use the `Greeting` composable in your `setContent` block like this:

```kotlin
@Composable
fun MyApp() {
    Greeting(name = "John")
}
```

#### **Main Activity Setup**

In the `MainActivity`, you typically use `setContent` to set the initial UI.

```kotlin
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }
}
```

***

### **3. Common UI Elements**

Jetpack Compose provides a wide variety of UI elements (composables). Below are some of the most common ones.

#### **Text**

```kotlin
@Composable
fun MyText() {
    Text(text = "Welcome to Jetpack Compose!")
}
```

#### **Button**

```kotlin
@Composable
fun MyButton() {
    Button(onClick = { /* Handle click */ }) {
        Text(text = "Click Me")
    }
}
```

#### **Image**

```kotlin
@Composable
fun MyImage() {
    Image(painter = painterResource(id = R.drawable.my_image), contentDescription = null)
}
```

#### **Column and Row**

`Column` and `Row` are layout composables used to organize UI elements vertically and horizontally.

```kotlin
@Composable
fun MyColumn() {
    Column {
        Text(text = "First Item")
        Text(text = "Second Item")
    }
}

@Composable
fun MyRow() {
    Row {
        Text(text = "Left Item")
        Text(text = "Right Item")
    }
}
```

#### **Modifier**

Modifiers allow you to style and adjust the layout of your composables.

```kotlin
@Composable
fun StyledButton() {
    Button(
        onClick = { /* Handle click */ },
        modifier = Modifier.padding(16.dp).fillMaxWidth()
    ) {
        Text(text = "Styled Button")
    }
}
```

***

### **4. Handling State**

Jetpack Compose is declarative, meaning UI updates are tied to changes in state. You manage state using state variables and composable functions.

#### **State with `remember`**

```kotlin
@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize()
    ) {
        Text(text = "Count: $count")
        Button(onClick = { count++ }) {
            Text(text = "Increment")
        }
    }
}
```

In this example, the state of `count` is remembered across recompositions, and the UI updates when `count` changes.

***

### **5. Navigation**

Jetpack Compose offers an easy way to handle navigation between different screens.

#### **Navigation Setup**

You need to include the navigation library in your dependencies:

```gradle
dependencies {
    implementation "androidx.navigation:navigation-compose:2.5.0"
}
```

#### **Basic Navigation Example**

```kotlin
@Composable
fun FirstScreen(navController: NavController) {
    Button(onClick = {
        navController.navigate("second_screen")
    }) {
        Text("Go to Second Screen")
    }
}

@Composable
fun SecondScreen() {
    Text("This is the second screen!")
}

@Composable
fun NavigationGraph(navController: NavController) {
    NavHost(navController, startDestination = "first_screen") {
        composable("first_screen") { FirstScreen(navController) }
        composable("second_screen") { SecondScreen() }
    }
}
```

In the `MainActivity`, set up the `NavController`:

```kotlin
@Composable
fun MyApp() {
    val navController = rememberNavController()
    NavigationGraph(navController)
}
```

***

### **6. Theming and Material Design**

Jetpack Compose integrates well with Material Design. You can define themes for your app using `MaterialTheme`.

#### **Customizing the Theme**

```kotlin
@Composable
fun MyCustomTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colorScheme = lightColorScheme(
            primary = Color(0xFF6200EE),
            secondary = Color(0xFF03DAC6)
        ),
        typography = Typography,
        content = content
    )
}

@Composable
fun AppContent() {
    MyCustomTheme {
        // Your UI components here
        Greeting(name = "World")
    }
}
```

***

### **7. Previewing UI**

Jetpack Compose supports previewing UI directly in Android Studio. You can add a preview function like this:

```kotlin
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApp()
}
```

This allows you to view the UI without running the app on an emulator or device.

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://opentune.gitbook.io/undefined/basics/editor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
