Thuta Learning
ရှာဖွေရန်
BasicMobile Developmentintermediate

Jetpack Compose Basics

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Jetpack Compose Basics ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Android Studio/Compose code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

အရင်တုန်းက Android UI ကို XML file သီးခြားရေးပြီး, Kotlin code ကနေ ချိတ်ဆက်ရပါတယ် (imperative, 'ဒီ view ကို ဒီလို update လုပ်ပါ' ဆိုတဲ့ ပုံစံ)။ Jetpack Compose ကတော့ declarative UI toolkit ပါ — `@Composable` function ထဲမှာ 'UI ဘယ်လို ပုံစံ ဖြစ်စေချင်တယ်' ဆိုတာကို Kotlin code နဲ့ တိုက်ရိုက် ရေးပါတယ်, data ပြောင်းလဲတိုင်း Compose က UI ကို automatic re-draw ပေးပါတယ် — React/Flutter ရဲ့ declarative UI concept နဲ့ ဆင်တူပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`@Composable fun Greeting(name: String) { Text("Hello, $name!") }` လို့ ရေးလိုက်ရင်, `Greeting("Aye Aye")` ကို ခေါ်တာနဲ့ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို screen ပေါ် ပြပေးပါတယ် — XML layout file တစ်ခုမှ ရေးစရာ မလိုပါဘူး။ `@Preview` annotation ကို ထည့်ရင် Android Studio ထဲမှာ emulator run စရာ မလိုဘဲ UI ကို ချက်ချင်း ကြည့်လို့ရပါတယ်.

အတူတူ ကြည့်မယ်

kotlin
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

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

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Greeting(name = "Aye Aye")
}
You should see
Android Studio Preview panel ထဲမှာ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို emulator run စရာမလိုဘဲ ချက်ချင်း တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

`Greeting` composable ကို ကိုယ်တိုင်ရေးပြီး `@Preview` panel ထဲ ကြည့်ကြည့်ပါ — name parameter ကို ပြောင်းကြည့်ပြီး preview auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

`@Preview` က UI layout ကို quick ကြည့်ဖို့ပါ — button click, navigation စတဲ့ interactive behavior ကို emulator (ဒါမှမဟုတ် physical device) ပေါ်မှာသာ တကယ်တမ်း test လုပ်နိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `@Composable` annotation ကို function ပေါ်မှာ ထည့်ဖို့ မေ့ခြင်း — Compose UI function အဖြစ် recognize မလုပ်တော့ပါ
  • Composable function ကို XML-era imperative style (view reference ကို manual update) နဲ့ ရေးဖို့ ကြိုးစားခြင်း — Compose ရဲ့ declarative pattern ကို လိုက်နာသင့်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`Greeting` composable ကို ကိုယ်တိုင်ရေးပြီး `@Preview` panel ထဲ ကြည့်ကြည့်ပါ — name parameter ကို ပြောင်းကြည့်ပြီး preview auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

You'll know it worked when: Android Studio Preview panel ထဲမှာ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို emulator run စရာမလိုဘဲ ချက်ချင်း တွေ့ရမည်။

Jetpack Compose Basics | Thuta Learning