0

I've used the default bottom navigation template in Android Studio. This has been working fine so far. However, I'd like to change one of the buttons to a button which triggers a camera intent. I'm not sure how to do this since I assume the default implementation uses built-in functions which I can't change.

I believe setupWithNavController is responsible for navigating to the matching fragment. Is there any way to change this, so that I add a button that launches the camera?

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
    }
}

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
iCV
  • 547
  • 1
  • 8
  • 29

1 Answers1

1

Remove setupWithNavController and create your own menu.xml file. Then you can use your menu with adding like this:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/main" />

After that, you can implement setOnNavigationItemSelectedListener on your NavigationBar inside Kotlin class.

Aytek Sökmen
  • 474
  • 3
  • 9