Layout doesn't inflate in Android Studio MainActivity when it contains a Fragment Container View

111 Views Asked by At

I have the same problem like in this post, but in other way:

I use a button from inside a fragment to inflate a different layout in MainActivity using the interface communication. It works when the layout contains only a TextView, but when I replace it with a Fragment Container View, it crashes. I used @ianhanniballake answer changing the context to (this) and it removed the crash, but it still doesn't inflate anything..

Here is my code:

public class MainActivity extends AppCompatActivity implements FragmentB.FragmentBListener {

    ViewGroup main_layout;

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

        main_layout = findViewById(R.id.main_layout);
    }

    //fullScreen method comes from a button click listener from FragmentB
    @Override
    public void fullScreen() {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view = inflater.inflate(R.layout.fullscreen, main_layout,false);
        main_layout.removeAllViews();
        main_layout.addView(view);
    }
}

this is my activity_main.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"
    android:id="@+id/main_layout"
    tools:context=".MainActivity">

    //some basic initial code

</RelativeLayout>

and this is the layout I want to inflate - fullscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerViewB"
        android:name="com.example.testglide.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="500dp"
        tools:layout="@layout/fragment_b" />

</RelativeLayout>

Thanks a lot for any help! :)

1

There are 1 best solutions below

2
On

try this:

activity_main.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"
    android:id="@+id/main_layout"
    tools:context=".MainActivity">

    <include android:id="@+id/layout_fullscreen"
      layout="@layout/fullscreen"/>

</RelativeLayout>

java class

View view = findViewById(R.id.layout_fullscreen);  
main_layout.removeAllViews();
main_layout.addView(view);