Create Fragments From XML File In Android

Introduction

 
Fragments are the most important part of an Android Activity. Fragments have their own life cycle apart from Activity. And an Activity may contain zero or more fragments. The main important feature of a fragment is we can reuse them anyplace. Here I will explain the basics of a fragment; that is, how we can add a fragment into our activity from the XML layout file. Please follow the steps.
 
First, we need to create our Activity and layout. For that first, I will create the layout and set the background color as a variant of the red color. And I put a Textview in that layout and give the text "I am Activity".
 
Now we need to create the Fragment and its layout. For that I create a layout file named fragment_layout. I set the background as a variant color of green and add a text view and put a Textview on that layout and give text as "I am Fragment". Please see the fragment_layout below,
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3. android:orientation="vertical" android:layout_width="match_parent"    
  4. android:background="#009900"    
  5. android:layout_height="match_parent">    
  6.     <TextView    
  7. android:layout_width="wrap_content"    
  8. android:layout_margin="40dp"    
  9. android:layout_height="wrap_content"    
  10. android:layout_gravity="center_horizontal"    
  11. android:textAppearance="?android:attr/textAppearanceLarge"    
  12. android:text="I am Fragment"    
  13. android:id="@+id/textView2" />    
  14. </LinearLayout>   
    Now create the fragment java class named FragmentA that extends from android.app.Fragment and in the onCreateView() of that class attach the layout which we have created in the first step. Please see the code below,
    1. public class FragmentA extends Fragment    
    2. {    
    3.     @Nullable    
    4.     @Override    
    5.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    
    6.     {    
    7.         return inflater.inflate(R.layout.fragment_layout, container, false);    
    8.     }    
    9. }   
    Now we need to attach this fragment to our Activity because we need to have the Activity and with this Activity only we can add the fragment. For adding the fragment in the layout please go to the following step. On the main_activity layout file click on the Design view and on the left side please go to the bottom and there will be an option named <fragment> click on that and one box will appear. From that please select FragmentA which we have created. Please see the screenshot,
     
     
    Now the main_acivity layout file will look like the following,
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    3. android:orientation="vertical" android:layout_width="match_parent"    
    4. android:background="#ff1a1a"    
    5. android:layout_height="match_parent">    
    6.     
    7.     <TextView    
    8. android:layout_width="wrap_content"    
    9. android:layout_margin="40dp"    
    10. android:layout_height="wrap_content"    
    11. android:layout_gravity="center_horizontal"    
    12. android:textAppearance="?android:attr/textAppearanceLarge"    
    13. android:text="I am Activity"    
    14. android:id="@+id/textView" />    
    15.     <fragment    
    16. android:layout_width="match_parent"    
    17. android:layout_height="match_parent"    
    18. android:name="study.fragment.negociosit.com.fragementstudyworks.FragmentA"    
    19. android:id="@+id/fragment"    
    20. android:layout_gravity="center_horizontal" />    
    21.     
    22. </LinearLayout>  
      Now the fragment is added in your layout and in the MainActivity Java class load your XML layout in the onCreate() and run the App. Please see below for the output screenshot,
       
       
      Read more articles on Android


      Similar Articles