John C
John C

Reputation: 517

Using BottomNavigationView outside of MainActivity in a Fragment

How do I use the bottomNavigationView variable outside of the MainActivity?

In MainActivity, I would use this to set and reove a badge

// To Add
BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
badge.setVisible(true);

// To remove
bottomNavigationView.removeBadge(menuItem.getItemId());

However it will return null in a fragment if I try the same method

public BottomNavigationView bottomNavigationView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MainActivity main = new MainActivity();
        main.bottomNavigationView.removeBadge(2131231026);

I have also tried this in the fragment, but nothing occurs

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View v =  inflater.inflate(R.layout.fragment_chat, container, false);
        final View nav =  inflater.inflate(R.layout.activity_main, container, false);
        bottomNavigationView = nav.findViewById(R.id.bottomNav);
        BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(2131231026);
        badge.setVisible(false);

Upvotes: 2

Views: 500

Answers (2)

ande
ande

Reputation: 319

I don't know if you have this in Java (requireActivity) and if you have only one Activity in your project, but It can be the solution :

(requireActivity() as MainActivity).findViewById(R.id.bottomNav)

Also you can add function in your main Activity like addBadge / removeBadge.

But I think you can't get bottomNavigationView outside the main activity because it's part of UI mainActivity and not others fragments PS : Your line " MainActivity main = new MainActivity();" isn't good because you instanciate an activity and not get actual instance so bottomNavigationView is null. May be use this.getActivity() ( this = fragment instance)

Upvotes: 2

Pooya Chavoshi
Pooya Chavoshi

Reputation: 495

If you want to use BottmNavigationView outside of MainActivity first you should create a separate activity like MenuActivity and suppose you want to use 5 items in BottmNavigationView, so:

public class MenuActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    final Fragment fragment1 = new DiscoverFragment();
    final Fragment fragment2 = new CreateFragment();
    final Fragment fragment3 = new AnalyticsFragment();
    final Fragment fragment4 = new MoreFragment();
    final FragmentManager fm = getSupportFragmentManager();
    final Fragment[] active = {fragment1};

    fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
    fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
    fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
    fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();


    bottomNavigationView=findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.search:
                    fm.beginTransaction().hide(active[0]).show(fragment1).commit();
                    active[0] = fragment1;
                    return true;
                case R.id.add:
                    fm.beginTransaction().hide(active[0]).show(fragment2).commit();
                    active[0] = fragment2;
                    return true;
                case R.id.chart:
                    fm.beginTransaction().hide(active[0]).show(fragment3).commit();
                    active[0] = fragment3;
                    return true;

                case R.id.more:
                    fm.beginTransaction().hide(active[0]).show(fragment4).commit();
                    active[0] = fragment4;
                    break;
            }
            return true;
        }
    });
  }
}

XML view:

<?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">



<FrameLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="MainActivity"
    tools:showIn="@layout/activity_main"
    android:padding="1dp"
    android:id="@+id/main_container"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    app:itemIconSize="26dp"
    app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
    app:itemTextAppearanceInactive="@style/BottomNavigationView"
    app:itemIconTint="@drawable/navigation_view_colored"
    app:itemTextColor="@drawable/navigation_view_colored"
    app:labelVisibilityMode="labeled"
    android:layout_gravity="bottom"
    android:background="@android:color/white"
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_navigation_menu" />

   </androidx.coordinatorlayout.widget.CoordinatorLayout>

be sure you created 5 fragments like DiscoverFragment, CreateFragment, and so on ... and XML view of them.

Upvotes: 1

Related Questions