-2

I have created a bottom navigation, when icon is clicked fragment does not load on frame layout instead shows on bottom navigation

Here's the MainActivity.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout 
    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"
    android:id="@+id/main_drawer"
    tools:context="com.safarpar.safarpar.MainActivity">

       <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">

           <android.support.design.widget.BottomNavigationView
               android:id="@+id/main_nav"
               android:layout_width="match_parent"
               android:layout_height="@dimen/_56sdp"
               android:layout_alignParentBottom="true"
               android:layout_alignParentStart="true"
               app:menu="@menu/bottom_nav_menu">

           </android.support.design.widget.BottomNavigationView>
           <FrameLayout

               android:id="@+id/main_frame"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_above="@id/main_nav">
           </FrameLayout>

       </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/nav_view"
        android:layout_gravity="start">

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

And the MainActivity.java

mFrame=(FrameLayout)findViewById(R.id.main_frame);
mbottomNavigationView=(BottomNavigationView)findViewById(R.id.main_nav);
mDrawerLayout=(DrawerLayout)findViewById(R.id.main_drawer);
mToggle = new 
ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
//navigationdrawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//OnclickHandeler();

homeFragment= new HomeFragment();
bookingFragment= new BookingFragment();

mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home:
            setFragment(homeFragment);
            return true;
        case R.id.nav_booking:
            setFragment(bookingFragment);
        default:
            return true;
    }
}

private void setFragment(android.support.v4.app.Fragment Fragment) {
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_nav,Fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
});

And the Fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.safarpar.safarpar.HomeFragment">

        <LinearLayout
            android:id="@+id/Linear1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_200sdp"
            android:background="@drawable/front_slider"
            android:gravity="top"
            android:orientation="horizontal">   
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linear_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/Linear1"
            android:layout_marginTop="@dimen/_10sdp"
            android:orientation="horizontal"
            android:padding="@dimen/_10sdp">

            <Button
                android:id="@+id/main_flight"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_19sdp"
                android:layout_marginRight="@dimen/_7sdp"
                android:background="@drawable/flight_button_state" />

            <Button
                android:id="@+id/main_hotel"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_10sdp"
                android:layout_marginRight="@dimen/_7sdp"
                android:background="@drawable/hotel_button_state" />

            <Button
                android:id="@+id/main_bus"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_7sdp"
                android:layout_marginRight="@dimen/_10sdp"
                android:background="@drawable/bus_button_state" />

            <Button
                android:id="@+id/main_cab"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_50sdp"
                android:layout_marginLeft="@dimen/_7sdp"
                android:layout_marginRight="@dimen/_10sdp"
                android:background="@drawable/cab_button_state" />
        </LinearLayout>
</RelativeLayout>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
mDeveloper
  • 122
  • 9
  • seems like Your main layout is messed up . First check the `Framelayout` in layout its visible or not . – ADM Mar 21 '18 at 05:14
  • Check your fragment version which is used in Fragment class and also check framlayout id as well as check its visible or not – Chetan Kumar Patel Mar 21 '18 at 05:17
  • fragmentTransaction.replace(R.id.main_nav,Fragment); check this pass your frame layout id – Manthan Patel Mar 21 '18 at 05:17
  • "instead shows on bottom navigation" - That's where you're telling it to go: `fragmentTransaction.replace(R.id.main_nav,Fragment);`. If you want it in the `FrameLayout`, change `main_nav` to `main_frame`. – Mike M. Mar 21 '18 at 05:18

1 Answers1

0

Change the code for setting the Fragment like the following.

private void setFragment(android.support.v4.app.Fragment Fragment) {
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame,Fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

You need to use main_frame instead of main_nav here.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98