java – Problem displaying two fragments on top of each other – replace method in android

Although I used the “replace” method on the piece, the second part of the piece runs on the first part It should be noted that my code is only in (main activity) and there is no code in the second fragment section (I mean the “about” fragment here) (there is a fragment class by default) so I sent it I did not

I also put a background in the xml file, but the following part works although it is not clear

Please guide me on this

Thanks

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    .
    .
    .
    LinearLayout lnrRecyclerNoConnection, li_home, li_share, li_about, li_logout;



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

        replace(new HomeFragment());
        ...

    }
   
    .
    .
    .

     
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivMenu) {
            mDrawer.openMenu(true);
        } else if (id == R.id.li_home) {
            replace(new HomeFragment(), "Home");
            mDrawer.closeMenu(true);
        } else if (id == R.id.li_about) {
            replace(new AboutFragment(), "About");
            mDrawer.closeMenu(true);
        } else if (id == R.id.li_share) {
            Toast.makeText(this, "share", Toast.LENGTH_SHORT).show();
            mDrawer.closeMenu(true);
        } else if (id == R.id.li_logout) {
            Toast.makeText(this, "exited", Toast.LENGTH_SHORT).show();
            finish();
            System.exit(0);
        }
    }

    private void replace(Fragment fragment, String string) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fl_main, fragment);
        fragmentTransaction.addToBackStack(string);
        fragmentTransaction.commit();
    }
    private void replace(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fl_main, fragment);
        fragmentTransaction.commit();
    }
}
    

This is also the XML code section I put “Replace” in the main activity section

<?xml version="1.0" encoding="utf-8"?>
<com.mxn.soul.flowingdrawer_core.FlowingDrawer xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    app:edPosition="2"
    app:edMenuSize="260dp"
    app:edMenuBackground="#F1F1F1">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="0dp"
        android:animateLayoutChanges="true"
        android:background="@color/colorBackground">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/mainAppBar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/mainCollapsingToolbar"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleGravity="start|top"
                app:expandedTitleMarginTop="56dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


                <RelativeLayout
                    android:id="@+id/mainHeader"
                    android:layout_width="match_parent"
                    android:layout_height="200dp">

                    <ImageView
                        android:id="@+id/imgChannelCover"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="fitXY"
                        android:src="@drawable/cover_placeholder" />

                    <TextView
                        android:id="@+id/txtChannelFollower_cnt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentBottom="true"
                        android:layout_marginStart="32dp"
                        android:layout_marginEnd="32dp"
                        android:layout_marginBottom="32dp"
                        android:fontFamily="@font/yekan"
                        android:gravity="center_horizontal"
                        android:textColor="@color/colorPrimary"
                        android:textSize="16sp"
                        tools:ignore="NotSibling" />

                    <TextView
                        android:id="@+id/txtVideo_cnt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignStart="@+id/txtChannelFollower_cnt"
                        android:layout_alignEnd="@+id/txtChannelFollower_cnt"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="16dp"
                        android:fontFamily="@font/yekan"
                        android:gravity="center_horizontal"
                        android:textColor="@color/colorPrimary"
                        android:textSize="16sp" />


                </RelativeLayout>


                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/mainToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:paddingStart="16dp"
                    android:paddingEnd="16dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    <ImageView
                        android:id="@+id/imgShowChannelDescription"
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_gravity="start"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="24dp"
                        android:layout_marginEnd="16dp"
                        android:src="@drawable/person_outline_24" />

                    <ImageView
                        android:id="@+id/ivMenu"
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_gravity="end"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="24dp"
                        android:layout_marginEnd="8dp"
                        android:src="@drawable/menu_open_24" />



                </androidx.appcompat.widget.Toolbar>

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <LinearLayout
            android:id="@+id/lnrRecyclerNoConnection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="gone">

            <pl.droidsonroids.gif.GifImageView
                android:id="@+id/gifVideoLoading"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center"
                android:src="@drawable/no_connection"
                android:tint="@color/colorPrimary" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/yekan"
                android:gravity="center_horizontal"
                android:text="@string/noConnection" />

            <Button
                android:id="@+id/btnRecyclerTryConnection"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="16dp"
                android:background="@drawable/btn_selector"
                android:fontFamily="@font/yekan"
                android:text="refresh"
                android:textColor="@color/colorWhiteTextColor"
                android:textStyle="bold" />

        </LinearLayout>

        <com.facebook.shimmer.ShimmerFrameLayout
            android:id="@+id/shimmerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include layout="@layout/shimmer_screen" />

                <include layout="@layout/shimmer_screen" />

                <include layout="@layout/shimmer_screen" />

                <include layout="@layout/shimmer_screen" />

            </LinearLayout>


        </com.facebook.shimmer.ShimmerFrameLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/mainRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/CircleImageChannelProfile"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:src="@drawable/image_placeholder"
            app:civ_border_color="@color/colorPrimary"
            app:civ_border_width="1dp"
            app:layout_anchor="@id/mainAppBar"
            app:layout_anchorGravity="center_vertical|center_horizontal" />

        <!--<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/CircleImageChannelProfile"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:src="@drawable/image_placeholder"
        app:civ_border_color="@color/colorPrimary"
        app:civ_border_width="0.5dp"
        app:layout_anchor="@id/mainAppBar"
        app:layout_anchorGravity="bottom|end" />-->

        <!--content-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/fl_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </RelativeLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <!--menu-->
    <com.mxn.soul.flowingdrawer_core.FlowingMenuLayout
        android:id="@+id/menulayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/id_container_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/header">

                <ImageView
                    android:id="@+id/imgChannelCoverDrawer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/bg_card_video_main"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:background="@color/colorPrimary">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/CircleImageChannelProfileDrawer"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_marginTop="50dp"
                        android:src="@drawable/image_placeholder"
                        app:civ_border_color="@color/colorPrimary"
                        app:layout_anchor="@id/mainAppBar"
                        app:layout_anchorGravity="center_vertical|center_horizontal"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="title"
                        android:textSize="28sp"
                        android:fontFamily="@font/yekan"
                        android:textColor="@color/cardview_dark_background"
                        android:gravity="center_horizontal|center_vertical"/>
                </LinearLayout>
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/li_home"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:layoutDirection="rtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header"
                android:layout_marginHorizontal="24dp"
                android:layout_marginTop="48dp" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/home_24"
                    android:padding="4dp"
                    app:tint="@color/cardview_dark_background"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="home"
                    android:textColor="@color/cardview_dark_background"
                    android:fontFamily="@font/yekan"
                    android:textSize="16sp"
                    android:padding="4dp"/>
            </LinearLayout>

            <!--<LinearLayout
                android:id="@+id/li_about"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:layoutDirection="rtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/li_share"
                android:layout_marginHorizontal="24dp"
                android:layout_marginTop="24dp" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/person_outline_24"
                    android:padding="4dp"
                    app:tint="@color/cardview_dark_background" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="about"
                    android:textColor="@color/cardview_dark_background"
                    android:fontFamily="@font/yekan"
                    android:textSize="16sp"
                    android:padding="4dp"/>
            </LinearLayout>-->

            <LinearLayout
                android:id="@+id/li_share"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:layoutDirection="rtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/li_home"
                android:layout_marginHorizontal="24dp"
                android:layout_marginTop="24dp" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/share_24"
                    android:padding="4dp"
                    app:tint="@color/cardview_dark_background" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="اشتراک گذاری"
                    android:textColor="@color/cardview_dark_background"
                    android:fontFamily="@font/yekan"
                    android:textSize="16sp"
                    android:padding="4dp"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/li_logout"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:layoutDirection="rtl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/li_share"
                android:layout_marginHorizontal="24dp"
                android:layout_marginTop="24dp" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_baseline_power_settings_new_24"
                    android:padding="4dp"
                    app:tint="@color/cardview_dark_background" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="exit"
                    android:textColor="@color/cardview_dark_background"
                    android:fontFamily="@font/yekan"
                    android:textSize="16sp"
                    android:padding="4dp"/>
            </LinearLayout>
        </RelativeLayout>



    </com.mxn.soul.flowingdrawer_core.FlowingMenuLayout>


</com.mxn.soul.flowingdrawer_core.FlowingDrawer>

Leave a Comment