iCV
iCV

Reputation: 607

Android Studio change default bottom nav behaviour

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)
    }
}

Upvotes: 0

Views: 548

Answers (1)

Aytek Sökmen
Aytek Sökmen

Reputation: 474

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.

Upvotes: 1

Related Questions