Android Studio Fragment ein und wieder ausblenden?

Hallo ich habe eine App erstellt bei dem man auf den Text "Einblenden" Klickt und dann ein Fragment eingeblendet wird. Und ich will es aber auch ausblenden, aber ich weiß nicht wie könntet ihr mir da helfen?

Das ist der Code von meinem MainActivity

package com.example.user.fragmenteinblenden;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;


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

        textView = (TextView) findViewById(R.id.text_einblenden);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        TestFragment testFragment = new TestFragment();
        fragmentTransaction.replace(R.id.fragment_container,testFragment);
        fragmentTransaction.commit();


        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                TextFragment textFragment = new TextFragment();
                fragmentTransaction.add(R.id.fragment_container,textFragment);
                fragmentTransaction.commit();
            }
        });

    }



}

Das ist der Code von activity_main.xlm

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
    xmlns:app="<a href="http://schemas.android.com/apk/res-auto">http://schemas.android.com/apk/res-auto</a>"
    xmlns:tools="<a href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.fragmenteinblenden.MainActivity">

    <TextView
        android:id="@+id/text_einblenden"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Einblenden"
        android:textAlignment="center"
        android:textAppearance="@android:style/TextAppearance.Medium"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="25dp" />

    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="370dp"
        android:layout_height="228dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="77dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="6dp" />

</android.support.constraint.ConstraintLayout>

Hoffe ihr könnt mir helfen.

Danke im voraus

...zum Beitrag

Falls ihr weiter Info benötigt einfach anschreiben.

...zur Antwort