Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Navigation View not visible in the default fragment Android Studio

My bottom navigation bar is not visible in my default start fragment, SearchFragment. If i change the start fragment to another e.g. MoreFragment, it appears on that and all the rest, except the SearchFragment, which is my ideal start fragment.

Here is my code:

Main.java

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.MenuItem;



public class Main extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
           getSupportFragmentManager().beginTransaction().replace(R.id.activity_main, new SearchFragment()).commit();
    }

    BottomNavigationView bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);

    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()){
                case R.id.seach_tab:
                   SearchFragment sf=new SearchFragment();
                    FragmentManager fragmentmanager1=getSupportFragmentManager();
                    fragmentmanager1.beginTransaction()
                            .replace(R.id.activity_main, sf)
                            .commit();
                    break;


                case R.id.bookings_tab:
                    BookingsFragment bf=new BookingsFragment();
                    FragmentManager fragmentmanager2=getSupportFragmentManager();
                    fragmentmanager2.beginTransaction()
                            .replace(R.id.activity_main, bf)
                            .commit();
                    break;

                case R.id.account_tab:
                    ProfileFragment pf=new ProfileFragment();
                    FragmentManager fragmentmanager3=getSupportFragmentManager();
                    fragmentmanager3.beginTransaction()
                            .replace(R.id.activity_main, pf)
                            .commit();
                    break;

                case R.id.more_tab:
                   MoreFragment mf=new MoreFragment();
                    FragmentManager fragmentmanager4=getSupportFragmentManager();
                    fragmentmanager4.beginTransaction()
                            .replace(R.id.activity_main, mf)
                            .commit();
                    break;

            }
return false;
        }


    });
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/activity_main"
android:layout_height="match_parent"
android:background="@color/white">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/white"
        android:layout_gravity="bottom"
        app:itemIconTint="@drawable/tab_bar"
        app:itemTextColor="@drawable/tab_bar"
        app:menu="@menu/bottombarmenu" />

</FrameLayout>

bottombarmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:title="Search"
    android:enabled="true"
    android:id="@+id/seach_tab"
    android:icon="@drawable/icon_search"
    app:showAsAction="ifRoom|withText"
    >

</item>

<item
    android:id="@+id/bookings_tab"
    android:enabled="false"
    android:icon="@drawable/icon_bookings"
    android:title="My bookings"></item>

<item
    android:title="Profile"
    android:enabled="false"
    android:id="@+id/account_tab"
    android:icon="@drawable/icon_account"
    app:showAsAction="ifRoom|withText">
</item>

<item
    android:id="@+id/more_tab"
    android:enabled="false"
    android:icon="@drawable/icon_more"
    android:title="More"
    app:showAsAction="ifRoom|withText"></item>

</menu>
like image 272
Fiona Muthoni Avatar asked Mar 09 '23 08:03

Fiona Muthoni


1 Answers

You are replacing the complete view, so you can try layout like this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">



    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemBackground="@color/white"
        android:layout_gravity="bottom"
        app:itemIconTint="@drawable/tab_bar"
        app:itemTextColor="@drawable/tab_bar"
        app:menu="@menu/bottombarmenu"/>

</LinearLayout>
like image 75
Muthukrishnan Rajendran Avatar answered Apr 25 '23 12:04

Muthukrishnan Rajendran