더 친절한 코틀린 앱프로그래밍 책에 있는 코드 입니다.
This is code from the Friendlier Kotlin App Programming book
이 코드는 뷰바인딩을 사용하지 않았습니다.
This code does not use view binding.
Test : compileSdk = 35 targetSdk = 35
1.Directory & files

2.MainActivity.kt
package com.example.tabex
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
lateinit var bottom_navigation:BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
with(supportFragmentManager.beginTransaction()){
val fragment1 = Fragment1()
replace(R.id.container,fragment1)
commit()
}
bottom_navigation = findViewById(R.id.bottom_navigation)
// "bottom_navigation..setOnNavigationItemReselectedListener" was deprecated
bottom_navigation.setOnItemSelectedListener {
when(it.itemId){
R.id.tab1 -> {
showToast("첫번쨰 탭 선택됨 First tab selected")
with(supportFragmentManager.beginTransaction()) {
val fragment1 = Fragment1()
replace(R.id.container, fragment1)
commit()
}
return@setOnItemSelectedListener true
}
R.id.tab2 -> {
showToast("두번째 탭 선택됨 Second tab selected")
with(supportFragmentManager.beginTransaction()) {
val fragment2 = Fragment2()
replace(R.id.container, fragment2)
commit()
}
return@setOnItemSelectedListener true
}
R.id.tab3 -> {
showToast("세번쨰 탭 선택됨 Third tab selected")
with(supportFragmentManager.beginTransaction()) {
val fragment3 = Fragment3()
replace(R.id.container, fragment3)
commit()
}
return@setOnItemSelectedListener true
}
}
return@setOnItemSelectedListener false
}
}
fun showToast(message:String?){
Toast.makeText(applicationContext,message,Toast.LENGTH_LONG).show()
}
}
3.active_main.xml
-xml design

-xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>