I've a fragment where I need to show progress bar on click of a button and remove the progress bar after successful completion of the task.

This is my fragment class' onCreateView method

```
private RelativeLayout view;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.fragment_note, container, false);
    view = root;

    FloatingActionButton add = root.findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectImage(context);
            displayProgressBar(true);
        }
    });

    hideKeyboard();

    return root;
}

```

This is the method I'm using to display and hide progressBar. But it's not working when I'm trying to hide the progressBar.

```
private void displayProgressBar(boolean visible) {
    RelativeLayout layout = new RelativeLayout(getContext());
    ProgressBar progressBar = new ProgressBar(MainActivity.getActivity(),null,android.R.attr.progressBarStyleLarge);
    progressBar.setIndeterminate(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);

    if (visible) {
        layout.addView(progressBar, params);
        MainActivity.getActivity().setContentView(layout);
        progressBar.setVisibility(View.VISIBLE);
    } else {
        progressBar.setVisibility(View.INVISIBLE);
        MainActivity.getActivity().setContentView(view);
    }

}

```

I've tried adding progressBar in my fragment's xml but it didn't quite work for me.
Here's the fragment xml:

```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/note"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout android:id="@+id/note_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/AppTheme"
        android:orientation="vertical">

        <EditText android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="@dimen/fab_margin_medium"
            android:background="@android:color/transparent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:hint="Double tap to type"
            android:gravity="top"
            android:textCursorDrawable="@drawable/color_cursor"
            android:inputType="textMultiLine"
            android:id="@+id/taskText" />
    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin_small"
        android:background="@color/colorPrimaryLight"
        android:backgroundTint="@color/colorPrimaryLight"
        android:outlineSpotShadowColor="@color/colorPrimaryLight"
        android:outlineAmbientShadowColor="@color/colorPrimaryLight"
        app:borderWidth="0dp"
        android:src="@drawable/plus" />
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</RelativeLayout>

```

I'm new to Android. Any help is appreciated.
Thanks in advance.

EDIT:
I've tried resetting fragment view by:

```
MainActivity.getActivity().setContentView(view);

```

It's giving me following error:
The specified child already has a parent. You must call removeView() on the child's parent first.]

EDIT2:
Main Activity:

```
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    setContentView(R.layout.main_activity);
    setBottomNavigation();
    savedState = savedInstanceState;
}

public void setBottomNavigation() {
    BottomNavigationView navView = findViewById(R.id.nav_view);
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_files)
            .build();
    NavController navController = Navigation.findNavController(MainActivity.this,
            R.id.nav_host_fragment);
    NavigationUI.setupWithNavController(navView, navController);
    navView.setVisibility(View.VISIBLE);
}

```

And MainActivity XML:

```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

```