Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
44 views1 page

Android Navigation CheatSheet

This document provides a cheat sheet for implementing Android navigation using Kotlin and XML. It includes a MainActivity class that sets up a navigation drawer, view pager, and tab layout, along with the corresponding XML layout for the activity. The setup allows for navigation between three fragments: Home, Profile, and Settings, using a menu file for navigation items.

Uploaded by

mahichowdary353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views1 page

Android Navigation CheatSheet

This document provides a cheat sheet for implementing Android navigation using Kotlin and XML. It includes a MainActivity class that sets up a navigation drawer, view pager, and tab layout, along with the corresponding XML layout for the activity. The setup allows for navigation between three fragments: Home, Profile, and Settings, using a menu file for navigation items.

Uploaded by

mahichowdary353
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Android Navigation: One-Page Exam Cheat Sheet (Kotlin + XML)

MainActivity.kt (Kotlin Code)

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.open,
R.string.close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
view_pager.adapter = ViewPagerAdapter(this)
TabLayoutMediator(tab_layout, view_pager) { tab, pos ->
tab.text = listOf("Home", "Profile", "Settings")[pos]
}.attach()
navigation_view.setNavigationItemSelectedListener {
view_pager.currentItem = when (it.itemId) {
R.id.nav_home -> 0
R.id.nav_profile -> 1
else -> 2
}
drawer_layout.closeDrawers()
true
}
}
}

activity_main.xml (Layout XML)

<DrawerLayout>
<CoordinatorLayout>
<MaterialToolbar android:id="@+id/toolbar"/>
<TabLayout android:id="@+id/tab_layout"/>
<ViewPager2 android:id="@+id/view_pager"/>
</CoordinatorLayout>
<NavigationView android:id="@+id/navigation_view" app:menu="@menu/nav_menu"/>
</DrawerLayout>

Fragments used: HomeFragment, ProfileFragment, SettingsFragment


Adapter: ViewPagerAdapter(this) handles all 3 fragments.
Menu file: res/menu/nav_menu.xml with nav_home, nav_profile, nav_settings.

You might also like