Master Kotlin and Android development. Learn modern Android app development with Jetpack Compose and essential Android components.
Kotlin is Google's preferred language for Android development. Learn Kotlin fundamentals and start building Android apps with modern tools.
// Kotlin Basics
fun main() {
// Variables
val appName = "My Android App" // Immutable
var version = 1.0 // Mutable
println("Welcome to $appName v$version")
// Functions
fun greet(name: String): String {
return "Hello, $name!"
}
println(greet("Developer"))
// Lists
val frameworks = listOf("Jetpack Compose", "Room", "Retrofit")
frameworks.forEach { println(it) }
// Null safety
var nullableString: String? = null
println(nullableString?.length ?: "String is null")
}Basic Kotlin syntax and features
Learn how to create Activities and design user interfaces using XML layouts in Android.
// MainActivity.kt
package com.example.myapp
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button.setOnClickListener {
textView.text = "Button Clicked!"
}
}
}Basic Activity with button click handler
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Android!"
android:textSize="24sp"
android:textStyle="bold"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginTop="20dp"/>
</LinearLayout>XML layout with TextView and Button
Jetpack Compose is Android's modern toolkit for building native UI with Kotlin. Learn declarative UI development.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp()
}
}
}
@Composable
fun MyApp() {
var count by remember { mutableStateOf(0) }
MaterialTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Count: $count",
style = MaterialTheme.typography.headlineLarge
)
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { count++ }) {
Text("Increment")
}
}
}
}
}Jetpack Compose counter app
Display scrollable lists efficiently using RecyclerView, the standard component for lists in Android.
// Item data class
data class Item(val id: Int, val title: String, val description: String)
// Adapter
class ItemAdapter(private val items: List<Item>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val titleText: TextView = view.findViewById(R.id.titleText)
val descText: TextView = view.findViewById(R.id.descText)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.titleText.text = item.title
holder.descText.text = item.description
}
override fun getItemCount() = items.size
}
// In Activity
val items = listOf(
Item(1, "Kotlin", "Modern Android language"),
Item(2, "Jetpack", "Android libraries"),
Item(3, "Compose", "Declarative UI")
)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = ItemAdapter(items)RecyclerView with custom adapter
Implement navigation between fragments using Android's Navigation Component.
// Add to build.gradle
dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:2.7.0"
implementation "androidx.navigation:navigation-ui-ktx:2.7.0"
}
// In Fragment
import androidx.navigation.fragment.findNavController
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button = view.findViewById<Button>(R.id.navigateButton)
button.setOnClickListener {
findNavController().navigate(R.id.action_home_to_details)
}
}
}Fragment navigation setup
Learn how to make network requests and fetch data from APIs using Retrofit library.
// Add to build.gradle
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
// Data model
data class Post(
val id: Int,
val title: String,
val body: String
)
// API interface
interface ApiService {
@GET("posts")
suspend fun getPosts(): List<Post>
}
// Retrofit instance
object RetrofitClient {
private const val BASE_URL = "https://jsonplaceholder.typicode.com/"
val apiService: ApiService by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiService::class.java)
}
}
// In ViewModel or Activity
lifecycleScope.launch {
try {
val posts = RetrofitClient.apiService.getPosts()
// Update UI with posts
} catch (e: Exception) {
// Handle error
}
}Retrofit setup for API calls
Android Studio - Official IDEJetpack Compose - Modern UI toolkitRetrofit - HTTP clientRoom - Database library