0
0
Android-kotlinHow-ToBeginner ยท 3 min read

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 onCreateView causes no UI to show.
  • Adding fragments without committing the transaction means no change appears.
  • Using getFragmentManager() in AppCompatActivity instead of getSupportFragmentManager() 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 Fragment and override onCreateView.
  • Inflate your layout or create UI in onCreateView.
  • Add fragments to activities using FragmentManager transactions.
  • 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.