Kotlin Android Development
Build Android apps with Kotlin — Activities, Intents, layouts, and View Binding in Android Studio
Table of Contents
1Kotlin in Android Studio
Android Studio is the official IDE for Android development. It is built on IntelliJ IDEA and ships with first-class Kotlin support — syntax highlighting, refactoring, lint checks, and templates for Activities, Fragments, and Compose.
Create a Kotlin Android project
- Open Android Studio → New Project
- Choose a template (e.g. Empty Views Activity or Empty Compose Activity)
- Set Language to Kotlin
- Select minimum SDK (e.g. API 24+) and click Finish
Project structure (Kotlin app)
Enable Kotlin features in build.gradle.kts
| Feature | Kotlin benefit in Android Studio |
|---|---|
| Convert Java to Kotlin | Code → Convert Java File to Kotlin File |
| Live templates | toast, logd, const shortcuts |
| Lint | Kotlin null-safety and Android best-practice warnings |
| Gradle KTS | Build scripts in Kotlin — type-safe configuration |
If Android Studio is not installed yet, follow Kotlin Setup and the Android Installation Guide.
2Activities
An Activity represents a single screen with a user interface. In Kotlin, Activities subclass AppCompatActivity and override lifecycle methods to respond to user interaction and system events.
Activity lifecycle
| Callback | When called |
|---|---|
onCreate() | Activity created — set up UI |
onStart() | Activity becoming visible |
onResume() | Activity in foreground, user interacting |
onPause() | Partially obscured — save lightweight state |
onStop() | No longer visible |
onDestroy() | Activity destroyed — release resources |
3Intents
An Intent is a messaging object used to request an action — start another Activity, open a URL, share content, or launch a system service. Kotlin makes Intent construction concise with named parameters and extension helpers.
Explicit Intent — start another Activity
Receive data in target Activity
Implicit Intent — system chooses app
| Intent type | Purpose | Example |
|---|---|---|
| Explicit | Start known component in your app | Intent(this, DetailActivity::class.java) |
| Implicit | System resolves matching apps | Intent(ACTION_SEND) |
| putExtra | Pass primitive/String data | putExtra("KEY", value) |
| Activity Result API | Get result back from Activity | registerForActivityResult |
For complex apps, use Jetpack Navigation Component instead of manual Intents — supports graphs, deep links, and safe args.
4Layouts
Layouts define UI structure in XML under res/layout/. Android inflates layout files into View objects at runtime. Kotlin Activities reference layouts via View Binding or setContentView.
activity_main.xml
Common layout containers
| Layout | Behavior | Use case |
|---|---|---|
LinearLayout | Single row or column | Simple stacked UI |
RelativeLayout | Position relative to siblings/parent | Legacy — prefer ConstraintLayout |
ConstraintLayout | Flexible constraints, flat hierarchy | Recommended for complex screens |
FrameLayout | Stack views on top of each other | Fragments container, overlays |
RecyclerView | Scrollable list of items | Feeds, lists — see Android tutorial |
ConstraintLayout example
res/values/strings.xml and colors to colors.xml — reference with @string/app_name for localization and theming. More in Layouts & Widgets.
5View Binding
View Binding generates a binding class for each XML layout (e.g. ActivityMainBinding for activity_main.xml). It replaces findViewById with type-safe, null-safe references — the recommended way to access views in Kotlin Android projects.
Enable View Binding
Use in Activity
View Binding in Fragment
| Approach | Type safe? | Status |
|---|---|---|
findViewById | No (casts required) | Legacy — avoid |
| View Binding | Yes | Recommended |
| Data Binding | Yes + binding expressions | When binding UI to ViewModel in XML |
| Kotlin synthetics | Partial | Deprecated — removed |
Layout file activity_main.xml → binding class ActivityMainBinding. Underscores are removed and words are PascalCase.
6Kotlin Android Extensions
Kotlin Android Extensions (also called Kotlin synthetics) allowed direct access to views by ID without findViewById — e.g. textView.text = "Hello" after importing kotlinx.android.synthetic. This plugin is deprecated and removed; migrate to View Binding.
Old synthetics approach (deprecated)
Why synthetics were removed
| Issue | Problem |
|---|---|
| No null safety | Could crash if view missing from layout |
| Fragment leaks | Synthetics could hold Activity view references after destroy |
| No compile-time binding | IDs not verified as strongly as View Binding |
| Maintenance | Google/JetBrains replaced with View Binding |
Migration: synthetics → View Binding
Other useful Kotlin Android extensions
While layout synthetics are gone, Kotlin extension functions on Android types remain very useful:
Use View Binding
For all new XML-based screens — type-safe view access.
Use KTX
core-ktx, activity-ktx, fragment-ktx for concise APIs.
Custom extensions
Add helpers like showToast(), hideKeyboard().
Avoid synthetics
Remove kotlin-android-extensions plugin from Gradle if present.
kotlin("android.extensions") or id("kotlin-android-extensions") from build.gradle and enable View Binding instead. Details in ViewBinding & DataBinding.
7Summary Cheatsheet
| Topic | Key Takeaway |
|---|---|
| Kotlin in Android Studio | New project → Language: Kotlin; enable View Binding in Gradle |
| Activities | AppCompatActivity; onCreate → inflate layout → set listeners |
| Intents | Explicit (in-app); Implicit (system); putExtra for data |
| Layouts | XML in res/layout/; ConstraintLayout for complex UI |
| View Binding | ActivityMainBinding.inflate() — recommended, type-safe |
| Kotlin Android Extensions | Synthetics deprecated — migrate to View Binding + KTX |
| Next lesson | Jetpack Compose — declarative UI in Kotlin |