Search code examples
javaandroidandroid-activityfragment

open fragment under parent activity


I want to open a fragment in activity but under objects of Activity. in my code RelativeLayout is parent and LinearLayout is child but when open fragment on relativelayout I lose linearlayout :( this is my code:

Main Activity Java:

    public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
private Context context;
private Button  fragment1 , fragment2;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    fragment1 = this.findViewById(R.id.fragment1);
    fragment2 = this.findViewById(R.id.fragment2);
    relativeLayout = this.findViewById(R.id.relativeLayout);

    fragment1.setOnClickListener(this);
    fragment2.setOnClickListener(this);
  }


@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.fragment1:
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.relativeLayout , new fragment1());
            ft.commit();
            break;

        case R.id.fragment2:
            FragmentManager fm2 = getSupportFragmentManager();
            FragmentTransaction ft2 = fm2.beginTransaction();
            ft2.replace(R.id.relativeLayout , new fragment2());
            ft2.commit();
            break;
           }
    }
}

Main Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
  tools:context=".MainActivity"
  android:id="@+id/relativeLayout">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/layout"
    android:background="#F00"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment 1"
        android:id="@+id/fragment1"
        android:gravity="center"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="fragment 2" />

</LinearLayout>

</RelativeLayout>

fragment1 XML:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">

 </android.support.constraint.ConstraintLayout>

fragment2 XML:

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#00F">
 </android.support.constraint.ConstraintLayout> 

This is Main Activity: enter image description here

and when click on fragment1: enter image description here

Plz Help Me :)

TNX


Solution

  • Add a framelayout in relativelayout before the linearlayout and inflate the fragment in the framelayout.

     <RelativeLayout 
         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"
              tools:context=".MainActivity"
              android:id="@+id/relativeLayout">
    
             <FrameLayout
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>
    
            <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="60dp"
              android:id="@+id/linearLayout"
              android:background="#F00"
              android:orientation="horizontal">
    
           <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="fragment 1"
             android:id="@+id/fragment1"
             android:gravity="center"
             android:layout_gravity="center"/>
    
            <Button
              android:id="@+id/fragment2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="fragment 2" />
    
          </LinearLayout>
    
        </RelativeLayout>