Static Fragment is a type of fragment that is defined in an XML layout file only. It is a part of the Activity and its lifecycle depends on the lifecycle of its container activity.
Properties of Static Fragment:
- Defined using <fragment> tag in XML layout file.
- Having a fixed position in the Activity’s layout.
- Cannot be removed or replaced at runtime.
- Created when the Activity is created and destroyed when the activity is destroyed.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:weightSum = "2" android:padding = "15dp" tools:context = ".MainActivity" > < fragment android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/fragUpper" android:name = "com.anas.staticfragment.UpperFragment" android:layout_weight = "1" /> < fragment android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/fragLower" android:name = "com.anas.staticfragment.LowerFragment" android:layout_weight = "1" /> </ LinearLayout > |
Step 3: Working with Fragment layout (e.g. fragment_upper.xml)
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#f8bbd0" android:gravity = "center" tools:context = ".UpperFragment" > < TextView android:id = "@+id/txtUpperFrag" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Upper Fragment" android:textSize = "22sp" android:textColor = "#e91e63" android:textStyle = "italic|bold" /> </ LinearLayout > |
Step 4: Working with Fragment layout (e.g. fragment_lower.xml)
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#b2ebf2" android:gravity = "center" tools:context = ".LowerFragment" > < TextView android:id = "@+id/txtLowerFrag" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Lower Fragment" android:textSize = "22sp" android:textColor = "#00bcd4" android:textStyle = "italic|bold" /> </ LinearLayout > |
Step 5: Working with Fragment (e.g. UpperFragment.java)
Java
package com.anas.staticfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class UpperFragment extends Fragment { public UpperFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_upper, container, false ); TextView txtUpperFrag = view.findViewById(R.id.txtUpperFrag); return view; } } |
Step 6: Working with Fragment (e.g. LowerFragment.java)
Java
package com.anas.staticfragment; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class LowerFragment extends Fragment { public LowerFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_lower, container, false ); TextView txtLowerFrag = view.findViewById(R.id.txtLowerFrag); return view; } } |
Output: