Fiona Muthoni
Fiona Muthoni

Reputation: 85

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>

Upvotes: 4

Views: 7273

Answers (2)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

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>

Upvotes: 7

Sai Jayant
Sai Jayant

Reputation: 376

public class Main extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
           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;
        }


    });
}
}

Note: Saved instance will load if the state null but in your case it not null so it wont load...but if you want it as default you have exclude that if statement

Upvotes: 0

Related Questions