0

I am creating navigation drawer.

When I replace any fragment on it, it does replaces but the frame layout is running in back..

So when I press at empty screen on the fragment, the contents of the main layout are shown. So how can I completely remove the main layout when some fragment replaces it ?

android.support.v4.app.FragmentManager fm=getSupportFragmentManager();
if (id == R.id.nav_categories)
{
    Categories cat=new Categories();
    FragmentTransaction transaction=fm.beginTransaction().replace(R.id.relative_layout,cat);
    transaction.addToBackStack(null);
    transaction.commit();
    /* fm.beginTransaction().replace(R.id.relative_layout,cat).commit();
           Toast.makeText(this,"Categories",Toast.LENGTH_LONG).show();*/
}
else if (id == R.id.nav_rating)
     {
         Fragment_Rating rat=new Fragment_Rating();

         fm.beginTransaction().replace(R.id.relative_layout,rat).commit();
         Toast.makeText(this,"Rating",Toast.LENGTH_LONG).show();
         FragmentTransaction transaction=fm.beginTransaction().replace(R.id.relative_layout,rat);
         transaction.addToBackStack(null);
         transaction.commit();
     }

I have tried removing addtobackstack...

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • 3
    Possible duplicate of [Android - How to change fragments in the Navigation Drawer](https://stackoverflow.com/questions/24006181/android-how-to-change-fragments-in-the-navigation-drawer) – Thecave3 Jun 12 '17 at 12:34
  • set background for the parent layout – MathankumarK Jun 12 '17 at 12:36

3 Answers3

0

in your fragment xml file

just add to the parent View

android:background="#ffffff"
android:clickable="true"
Shubham Goel
  • 1,962
  • 17
  • 25
0

Add background to your FrameLayout

    <FrameLayout
        android:id="@id/fragment_B"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#B4B4B4"
         >
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
0
FrameLayout layout = (FrameLayout) findViewById(R.id.frame_layout);
android.support.v4.app.FragmentManager fm=getSupportFragmentManager();
if (id == R.id.nav_categories)
{
    Categories cat=new Categories();
    FragmentTransaction 
    transaction=fm.beginTransaction().replace(R.id.relative_layout,cat);
    transaction.addToBackStack(null);
    transaction.commit();
    layout.serVisibility(View.GONE)    // add this line
}
else if (id == R.id.nav_rating)
    {
     Fragment_Rating rat=new Fragment_Rating();

     fm.beginTransaction().replace(R.id.relative_layout,rat).commit();
     Toast.makeText(this,"Rating",Toast.LENGTH_LONG).show();
     FragmentTransaction 
     transaction=fm.beginTransaction().replace(R.id.relative_layout,rat);
     transaction.addToBackStack(null);
     transaction.commit();
     layout.serVisibility(View.GONE); // add this line
 }
Jai
  • 3,211
  • 2
  • 17
  • 26