Skip to content

Instantly share code, notes, and snippets.

@c4software
Created January 25, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c4software/6eb4bfca2d82bc04962b0818f676d8df to your computer and use it in GitHub Desktop.
Save c4software/6eb4bfca2d82bc04962b0818f676d8df to your computer and use it in GitHub Desktop.
Fragment + Navigation Graph
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.bottom.BottomActivity">
<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"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/page_1"
android:enabled="true"
android:icon="@drawable/ic_baseline_all_inclusive_24"
android:title="@string/app_name"/>
<item
android:id="@+id/page_2"
android:enabled="true"
android:icon="@drawable/ic_baseline_all_inclusive_24"
android:title="@string/app_name"/>
</menu>
class BottomActivity : AppCompatActivity() {
companion object {
fun getStartIntent(context: Context): Intent {
return Intent(context, BottomActivity::class.java)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom)
setUpNavigation()
}
fun setUpNavigation() {
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
NavigationUI.setupWithNavController(bottom_navigation, navHostFragment!!.navController)
}
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/mobile_navigation.xml"
app:startDestination="@id/page_1">
<fragment
android:id="@+id/page_1"
android:name="com.eseo.myapplication2.ui.bottom.fragment.Page1"
android:label="Page1" />
<fragment
android:id="@+id/page_2"
android:name="com.eseo.myapplication2.ui.bottom.fragment.Page2"
android:label="Page2" />
</navigation>
class Page1 : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_page1, container, false)
}
companion object {
fun newInstance() = Page1()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment