Android-Fragment
A fragment is a sub-activity. A single activity can contain more than one fragment. Inside a single activity, fragments represent multiple screens.
Some Features of Fragment:
- The Android framework is responsible for managing a fragment’s layout, views, and lifecycle. It can be considered an independent section of a user interface.
- A single fragment is used in multiple activities and combines multiple fragments in a single activity to build a multi-pane UI.
- Fragments are frequently utilized in tasks when various UI layouts are required for various device form factors or various screen orientations. In order to respond to these changes, fragments can be dynamically added, removed, or changed within an activity.
- Similar to activities, fragments include callbacks for each stage of their lifecycle, including onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). You can control the state and behavior of the fragment using these callbacks.
There are main three types of fragment
- Single Fragment
- List Fragment
- Transactions Fragment
Single Fragment: On the device screen, only show one view. Mobile phones are the primary use for this kind of fragment.
List Fragment: List fragments are a particular kind of fragment that shows a list of objects. They frequently work with ListView, RecyclerView, or GridView to provide a scrollable list of data.
Transaction Fragment: These fragments enable the switching from one fragment to another during operation. Users can navigate between various fragments by clicking on different tabs.
Fragment Example:
1.Create a new project go to activity_main.xml,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
2.After that add this code in MainActivity.java
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment myFragment = new MyFragment();
// Get the fragment manager
FragmentManager fragmentManager = getSupportFragmentManager();
// Begin the fragment transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Replace the fragment_container with your fragment
fragmentTransaction.replace(R.id.fragment_container, myFragment);
// Commit the transaction
fragmentTransaction.commit();
}
}
3.Now create a java class named MyFragment.java and add this code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.example.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.fragment.app.Fragment; public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_my, container, false); // Access views within the fragment TextView textView = view.findViewById(R.id.text_view); textView.setText("Hello, Fragment!"); return view; } } |
4. and now add this code in fragment_my.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:text="This is a Fragment" android:layout_height="wrap_content" android:textSize="24sp" android:layout_gravity="center" /> </LinearLayout> |
There are main three types of fragment
- Single Fragment
- List Fragment
- Transactions Fragment
Single Fragment: On the device screen, only show one view. Mobile phones are the primary use for this kind of fragment.
List Fragment: List fragments are a particular kind of fragment that shows a list of objects. They frequently work with ListView, RecyclerView, or GridView to provide a scrollable list of data.
Transaction Fragment: These fragments enable the switching from one fragment to another during operation. Users can navigate between various fragments by clicking on different tabs.