Last active
July 4, 2023 06:08
-
-
Save senamit2708/09d26887b87fda18aab6a4220370e4c1 to your computer and use it in GitHub Desktop.
Navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Gradle dependencies | |
| xml file -> nav_graph | |
| Create two fragment for testing | |
| add both fragment in nav_graph, assign one fragment as home. | |
| activity_main xml code changes -> use coordinate layout, appbar layout, toolbar, navhostfragment. | |
| add navigation code to activityMain. | |
| //notes | |
| tools:layout="@layout/dashboard" -> used to dispaly fragment layout in nav_graph |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <androidx.coordinatorlayout.widget.CoordinatorLayout | |
| 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:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <com.google.android.material.appbar.AppBarLayout | |
| android:id="@+id/appbar" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:backgroundTint="@android:color/holo_orange_light" | |
| app:liftOnScroll="true" | |
| tools:ignore="UnusedAttribute"> | |
| <androidx.appcompat.widget.Toolbar | |
| android:id="@+id/toolbar" | |
| android:layout_width="match_parent" | |
| android:layout_height="?actionBarSize"/> | |
| </com.google.android.material.appbar.AppBarLayout> | |
| <androidx.fragment.app.FragmentContainerView | |
| android:id="@+id/nav_host_fragment" | |
| android:name="androidx.navigation.fragment.NavHostFragment" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:layout_marginTop="?actionBarSize" | |
| app:defaultNavHost = "true" | |
| app:navGraph="@navigation/nav_graph"/> | |
| </androidx.coordinatorlayout.widget.CoordinatorLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| private lateinit var navHostFragment: NavHostFragment | |
| private lateinit var appBarConfiguration: AppBarConfiguration | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| val binding = ActivityMainBinding.inflate(layoutInflater) | |
| setContentView(binding.root) | |
| initViews(binding) | |
| } | |
| private fun initViews(binding: ActivityMainBinding) { | |
| setSupportActionBar(binding.toolbar) | |
| supportActionBar!!.setDisplayShowTitleEnabled(false) | |
| navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return | |
| with(navHostFragment.navController) { | |
| appBarConfiguration = AppBarConfiguration(graph) | |
| setupActionBarWithNavController(this, appBarConfiguration) | |
| } | |
| } | |
| override fun onSupportNavigateUp(): Boolean { | |
| navHostFragment.navController.navigateUp() | |
| return super.onSupportNavigateUp() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nav_version = "2.6.0" | |
| //navigation | |
| implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" | |
| implementation "androidx.navigation:navigation-ui-ktx:$nav_version" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment