0

I want to implement BottomNavigationView in my Android app but my code does not displays this BottomNavigationView. Here is my code.

MainActivity.java

public class MainActivity extends AppCompatActivity {


private BottomNavigationView bottomNavView;
private int startingPosition, newPosition;
private final FragmentHome fragmentHome = new FragmentHome();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

    bottomNavView = findViewById(R.id.bottom_navigation);
    bottomNavView.setItemIconTintList(null);
    bottomNavView.setOnItemSelectedListener(item -> {
        showFragment(item.getItemId());
        return true;
    });
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragmentHome);
    bottomNavView.setSelectedItemId(R.id.action_home);
    ft.commit();
}

public void showFragment(int viewId) {
    Fragment fragment = null;

    switch (viewId) {
        case R.id.action_home:
            if (bottomNavView.getSelectedItemId() != R.id.action_home) {
                fragment = fragmentHome;
                newPosition = 0;
            }
            break;
    }

    if (fragment != null) {
        if (startingPosition > newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_frame, fragment).commit();
        }
        if (startingPosition < newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_frame, fragment).commit();
        }

        startingPosition = newPosition;
    }
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_action_bar, menu);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View arg0) {
            setItemsVisibility(menu, searchItem, false);
        }

        @Override
        public void onViewDetachedFromWindow(View arg0) {
            setItemsVisibility(menu, searchItem, true);
        }
    });
    return super.onCreateOptionsMenu(menu);
}

private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem item = menu.getItem(i);
        if (item != exception) item.setVisible(visible);
    }
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    return super.onOptionsItemSelected(item);
}
}

main_activity.xml

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/menu_bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

menu_bottom_navigation

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_home"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/home" />
    <item
        android:id="@+id/action_words_ranking"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/words_ranking" />
    <item
        android:id="@+id/action_users_ranking"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/users_ranking" />
    <item
        android:id="@+id/action_messages"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/messages" />
    <item
        android:id="@+id/action_notifications"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/notifications" />
</menu>

Not only this BottomNavigationView not displays it even not occupies any vertical space.

Help me please!

EDIT

I also simplified my day/night themes but no effect.

EDIT 2

I know where! There is a problem with ft.replace(R.id.content_frame, fragmentHome); in onCreate. It shows things from fragment but skip BottomNavView from MainActivity But how to solve that?

FragmentHome.java

public class FragmentHome extends Fragment {

    private MyViewModel loginViewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initBinding();

        loginViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String string) {
                Toast.makeText(requireActivity(), "cześć " + string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initBinding() {
        FragmentHomeBinding binding = DataBindingUtil.setContentView((requireActivity()), R.layout.fragment_home);
        loginViewModel = new ViewModelProvider(this).get(MyViewModel.class);
        binding.setMyViewModel(loginViewModel);
        binding.setLifecycleOwner(this);
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="myViewModel"
            type="pl.jawegiel.abc.viewmodel.MyViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:onClick="@{ ()-> myViewModel.setText() }"/>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={myViewModel.text}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Qbi
  • 445
  • 4
  • 13
  • does this crashes or does not display anything. cause I repopulate your code and it shows bottom nav. – Rudra Rokaya Feb 03 '22 at 13:50
  • No crash just not shows up both in Android Studio layout preview and in pthone – Qbi Feb 03 '22 at 13:56
  • bottomNavView.setOnItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }); --------------> replace this code with ::::::::> bottomNavView.setOnNavigationItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }); – Rudra Rokaya Feb 03 '22 at 14:32
  • Thank you, but the problem does not concern clicking on item but displaying onStart – Qbi Feb 03 '22 at 14:33
  • Are you overriding onStart() method? – Rudra Rokaya Feb 03 '22 at 14:44

4 Answers4

0

Does it return any errors? There is no problem in your code. Could you check the log?

Nisa Efendioglu
  • 901
  • 3
  • 12
  • 22
  • It does not return anything. No errors just not shows up both in phone and inAndroid Studio layout preview. – Qbi Feb 03 '22 at 13:55
  • please write these things in comments @Nisa – Sambhav Khandelwal Feb 03 '22 at 14:04
  • answer section is to give solution. not to ask. to ask please use comments – Sambhav Khandelwal Feb 03 '22 at 14:04
  • you're right, I forgot. thank you. @SambhavKhandelwal – Nisa Efendioglu Feb 03 '22 at 14:07
  • I tested your application, but there is no problem with me, your menus appear. If you want, try to invalid/restart android studio. @qbi – Nisa Efendioglu Feb 03 '22 at 14:08
  • @NisaEfendioglu but did you delete onCreateOptionsMenu and onOptionsItemSelected before running? Could you attach screenshot? – Qbi Feb 03 '22 at 14:12
  • I only removed the redirect page, since it wasn't in my code either. For example, I removed the Search fragment. You can proceed with the following logic after clicking on the menus, but there is no problem in your code anyway. @qbi – Nisa Efendioglu Feb 03 '22 at 14:17
  • bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.recentSearches: fragment = new RecentSearches(); break; case R.id.persons: fragment = new Persons(); break; return true; } }); – Nisa Efendioglu Feb 03 '22 at 14:18
  • Constraint Layout oldugu icin FramLayout BottomNavigation-u kapliyor bence. Bu nedenle FrameLayout-a limit constraint verilmeli ki BottomNavigationdan asagiya gecmesin. Yani hata var aslinda. – Samir Alakbarov Feb 03 '22 at 14:27
  • @NisaEfendioglu I updated edit 2. If you saw I would be gratefull. – Qbi Feb 03 '22 at 14:28
  • sorry, just saw it. did you solve your problem? @qbi – Nisa Efendioglu Feb 03 '22 at 18:38
  • 1
    Yes as I mentioned in one of posts @NisaEfendioglu :) now I have another problem in new topic https://stackoverflow.com/questions/70976615/android-moving-bottomnavigationview-labels :D – Qbi Feb 03 '22 at 18:40
0

try to add

layout_constraintTop_toBottomOf="@+id/content_frame"
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

Add app:layout_constraintBottom_toTopOf="@id/bottom_navigation" line to FrameLayout and change it layout_height attribute to 0dp. You must specify limit for FrameLayout, otherwise it can overlay BottomNavigationBar

Samir Alakbarov
  • 1,120
  • 11
  • 21
0

SOLVED. The problem was with DataBinding in fragment. I needed to move initBinding() and return binding.getRoot() in onCreateView().

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Qbi
  • 445
  • 4
  • 13