How to Create Fragment in Android: Simple Guide
To create a fragment in Android, extend the
Fragment class and override onCreateView to define the UI. Then add the fragment to an activity using FragmentManager either in XML or programmatically.Syntax
A fragment is a reusable UI component in Android. You create one by subclassing Fragment and overriding onCreateView to inflate the layout. Then you add it to an activity using FragmentManager.
- class MyFragment extends Fragment: Defines the fragment class.
- onCreateView(): Returns the fragment's UI view.
- FragmentManager: Manages adding/removing fragments in an activity.
java
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout, container, false); } } // Adding fragment in activity getSupportFragmentManager().beginTransaction() .add(R.id.container, new MyFragment()) .commit();
Example
This example shows a simple fragment that displays a TextView. The fragment is added to an activity's layout container programmatically.
java
public class SimpleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setText("Hello from Fragment"); textView.setGravity(Gravity.CENTER); return textView; } } // In MainActivity.java inside onCreate @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, new SimpleFragment()) .commit(); }
Output
The app screen shows centered text: "Hello from Fragment" inside the fragment container.
Common Pitfalls
- Forgetting to override
onCreateViewcauses no UI to show. - Adding fragments without committing the transaction means no change appears.
- Using
getFragmentManager()in AppCompatActivity instead ofgetSupportFragmentManager()causes errors. - Not using a container with an ID in the activity layout prevents fragment placement.
java
/* Wrong: Missing commit() */ getSupportFragmentManager().beginTransaction() .add(R.id.container, new MyFragment()); // No commit() /* Right: Commit the transaction */ getSupportFragmentManager().beginTransaction() .add(R.id.container, new MyFragment()) .commit();
Quick Reference
Remember these key points when creating fragments:
- Subclass
Fragmentand overrideonCreateView. - Inflate your layout or create UI in
onCreateView. - Add fragments to activities using
FragmentManagertransactions. - Always call
commit()on transactions. - Use
getSupportFragmentManager()in AppCompatActivity.
Key Takeaways
Create a fragment by extending Fragment and overriding onCreateView to define its UI.
Add fragments to activities using FragmentManager transactions and always call commit().
Use getSupportFragmentManager() in AppCompatActivity to manage fragments.
Ensure your activity layout has a container with an ID to host the fragment.
Avoid common mistakes like forgetting commit() or onCreateView override.