Start fragment in BottomNavigationView

Viewed 8465

I'm working with a simple app with Bottom Navigation View. I have 3 fragment with text, and i want to start their when i select a item in Botton Navigation, but i don't know what to write in the MainActivity.java; All the fragments have a .xml layout and .java. I searched in some codes, i wrote them, i search videos, but i have no sucess.

I'm learning about Fragments and UI Dynamic, so i created a new project in Android Studio with Bottom Navigation Activity. So, in my activity_main i have 3 itens in Bottom Navigation, and one framelayout above Bottom Navigation, taking all the parent. The idea is: when i select a item in Bottom Navigation, it will show another layout in the framelayout. So i created 3 xml layouts (with java class too) in layout folder and one fragment in framelayout. Now, i'm trying to show these layouts in my framelayout(that have a fragment) when i select a item. But i don't know how to do that.

Main Activity`

private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_home:
                selectedFragment = HomeFragment.newInstance();
                break;
            case R.id.navigation_dashboard:
                selectedFragment = DashboardFragment.newInstance();
                break;
            case R.id.navigation_notifications:
                selectedFragment = NotificationsFragment.newInstance();
                break;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.content, HomeFragment.newInstance());
    transaction.commit();
}

activity_main xml

<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"
tools:context="bandeira.thalisson.barradenavegacaoembaixo.MainActivity">

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

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation"/>

navigation xml

<item
    android:id="@+id/Fragment_one"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home"/>

<item
    android:id="@+id/Fragment_two"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_dashboard"/>

<item
    android:id="@+id/Fragment_three"
    android:icon="@drawable/ic_notifications_black_24dp"
    android:title="@string/title_notifications"/>

Fragment.java example

public class HomeFragment extends Fragment {

public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();
    return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.navigation_home, container, false);
    return inflater.inflate(R.layout.navigation_home, container, false);
}
4 Answers

alone with create this methor

protected boolean openFragment(Fragment fragment){
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content, fragment)
            .commit();
    return true;
}

to edit in the switch

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home: return openFragment(OneFragment.newInstance("",""));
            case R.id.navigation_beehive: return openFragment(twoFragment.newInstance("",""));
            case R.id.navigation_notifications: return openFragment(otherFragment.newInstance("",""));
        }
        return false;
    }

and for to finalize you can to initialize with a fragment in

 protected void onCreate(Bundle savedInstanceState) {
    ....
    openFragment(OneFragment.newInstance("",""));
}
Related