Search code examples
javaandroidlayoutbottomnavigationviewappcompatactivity

BottomNavigationView with AppCompat


dear developers!

I’m now learning to develop apps on java and i have just recently discovered that BottomNavigationView exists. Before this I actually had just buttons lined up at the bottom of layouts (shame). I faced such problem: almost all activities extend AppCompatActivity, but navigation view only works with fragments (sorry if I’m wrong). Can I somehow use it with AppCompat, or probably use another widget? Would be very grateful for the answer, especially if it would be open. Thanks in advance.


Solution

  • At the beginning, thanks for your suggestions, I realy appreciate it!

    To use BottomNavigationView with AppCompat activities, besides code given by @AnantaRaha, I also used this in .java files:

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
        bottomNavigationView.setSelectedItemId(R.id.home);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.dashboard:
                        startActivity(new Intent(getApplicationContext()
                                , Dashboard.class));
                        overridePendingTransition(0, 0);
                        return true;
                    case R.id.home:
                        return true;
                    case R.id.about:
                        startActivity(new Intent(getApplicationContext()
                                , Aboutt.class));
                        overridePendingTransition(0, 0);
                        return true;
                }
                return false;
            }
        });
    

    overridePendingTransition helped to get rid of blinks and small delays that Intent cause when you are using it (Another activity now starts immediately) :D