How to call fragment into the Activity

We will learn how to call fragment into the activity step by step:

First we will create fragment layout and fragment java class.

fragment_one.xml


<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

     <Textview
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
             />
    </LinearLayout>

FragmentOne.java

import android.os.Bundle;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
Public class FragmentOne extends Fragment
{
@Override
public void onCreate ( Bundle savedInstanceState ) {
   super.onCreate(savedInstanceState);
   }
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState ) {
 View convertView=inflater.inflate(R.layout.fragment_one, container, false);
 
        return convertView;
}
}

Now you will call the fragment into the MainActivity class

main_activity.xml

<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

     <Button
            android id="id/show_button"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
             />
    </LinearLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
  
public class MainActivity extends AppCompatActivity {  
private Button clickbutton;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main_activity);  
      clickbutton=(Button)findViewById(R.id.show_button);
      clickbutton.setOnClickListner(fragmentListner);
    }  
//Now here we will call fragment on the button click
private View.OnClickListner  fragmentListner=new View.OnClickListner
{
FragmentManager fragmentmanager=getSupportFragmentManager();
FragmentOne fragmentone=new FragmentOne();
FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.replace(android.R.id.content, fragmentone).addToBackStack("homepage")
                .commit();

}  

Note:

  • FragmentManager:It is used to manage the fragments and transactions between the fragments.
  • FragmentTransaction:It provides the method in which we can add,remove and replace the fragment in the android.It also gives the interface for interacting with the fragments.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *