Google Search

How To Add Fragment at Runtime In Activity

-->Hello Friends Today i post one important topic is Fragment. In this Post you Can easily Learn How To Add Fragment At runtime  in our Activity.

-->Fragment is one of the most important topic  in android.whenever i used Fragment So no need to Handle our Activity OnBackPress.

-->in android used Fragment when multiple View or Activity Can Available our Project.


FirstFragment.xmlfile  


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#da6f66">

 <TextView  android:gravity="center" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true" 
 android:layout_centerVertical="true" 
 android:text="@string/text1" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" 
 android:textSize="20dp" />

</RelativeLayout>

SecondFragment.xmlfile  


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#371f4d">

    <TextView 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" 
 android:text="@string/text2" 
 android:textAllCaps="false" 
 android:gravity="center" 
 android:textColor="@color/text_btn" 
 android:textSize="20dp" />

</RelativeLayout>

Main.XmlFile 

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 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:layout_height="match_parent"
     android:background="#79efb0" 
 tools:context="com.college.runtimeaddfragmentdemo.MainActivity">

    <LinearLayout 
 android:id="@+id/ll_first" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:weightSum="2">

        <Button 
 android:id="@+id/btn_frag1" 
 android:layout_width="0dp" 
 android:layout_height="wrap_content" 
 android:layout_margin="5dp" 
 android:layout_weight="1" 
 android:background="@color/back_btn" 
 android:text="@string/btn1" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" />

        <Button 
 android:id="@+id/btn_frag_two" 
 android:layout_width="0dp" 
 android:layout_height="wrap_content" 
 android:layout_margin="5dp" 
 android:layout_weight="1" 
 android:background="@color/back_btn" 
 android:text="@string/btn2" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" />
    </LinearLayout>

    <FrameLayout 
 android:id="@+id/ff_layout" 
 android:layout_width="wrap_content" 
 android:layout_height="200dp" 
 android:layout_below="@+id/ll_first" 
 android:orientation="horizontal">

        <FrameLayout 
 android:id="@+id/ll_container_first" 
 android:layout_width="match_parent" 
 android:layout_height="250dp" 
 android:orientation="horizontal"/>


    </FrameLayout>

    <FrameLayout 
 android:id="@+id/ll_container_second" 
 android:layout_width="match_parent" 
 android:layout_height="200dp" 
 android:layout_below="@id/ff_layout">
 </FrameLayout>
</RelativeLayout>
 
String.xmlfile 


<resources>
    <string name="app_name">RuntimeAddFragmentDemo</string>
    <string name="btn1">Load First fragment</string>
    <string name="btn2">Load Second fragment</string>
    <string name="text1">First Fragment</string>
    <string name="text2">Second Fragment</string>
</resources>


color.xmlfile


<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="text_btn">#FFFFFF</color>
    <color name="back_btn">#c261ec</color>
    <color name="text_textview">#a42395</color>
</resources>

First_Fragment.java

package com.college.runtimeaddfragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by admin on 04-05-2017. */
public class First_Fragment extends Fragment {
    @Nullable    @Override    public View onCreateView
(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState)
 {
        View v = inflater.inflate(R.layout.fragmet_first, null);
        return v;
    }
}

Second_Fragment.java
package com.college.runtimeaddfragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by admin on 04-05-2017. */
public class Second_Fragment extends Fragment {
    @Nullable    @Override    public View onCreateView
(LayoutInflater inflater, @Nullable ViewGroup container,
 Bundle savedInstanceState) 
{
        View v = inflater.inflate(R.layout.fragment_second, null);
        return v;
    }
}
 MainActivity.java

package com.college.runtimeaddfragmentdemo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button first_frag, second_frag;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first_frag = (Button) findViewById(R.id.btn_frag1);
        second_frag = (Button) findViewById(R.id.btn_frag_two);

        first_frag.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction
                 = fragmentManager.beginTransaction();
                First_Fragment first = new First_Fragment();
                fragmentTransaction.add(R.id.ll_container_first, first, "FIRST");
                fragmentTransaction.commit();

            }
        });
        second_frag.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction
                 = fragmentManager.beginTransaction();
                Second_Fragment second = new Second_Fragment();
                fragmentTransaction.add(R.id.ll_container_first, second, "SECOND");
                fragmentTransaction.commit();

            }
        });
    }
}

output
 
Android Fragment

Add Android fragment