Android Change Fragment
Change Fragment:
Changing a Fragment generally means swapping, appending, or deleting fragments in an activity.
It can be helpful for designing the UI according to the user interactions or screen sizes or any condition.
Example:
// activity_change_fragment_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ChangeFragmentExample"
android:padding="16dp"
android:orientation="vertical">
<Button
android:id="@+id/change_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Fragment"
android:textColor="@color/colorWhite"
android:background="@color/colorAccent"
/>
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
</LinearLayout>
// fragment_fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".FragmentOne"
android:background="@color/colorOrange"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Fragment"
android:textColor="@color/colorWhite"
android:textSize="40sp"/>
</LinearLayout>
// fragment_fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".FragmentTwo"
android:background="@color/colorAccent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textColor="@color/colorWhite"
android:textSize="40sp"/>
</LinearLayout>
Output