Android Add a Fragment to an Activity at Runtime

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="iterator.ir.a029.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:id="@+id/textView"
        android:text="@string/fragment_demo"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="30dp"
        android:id="@+id/relativeLayoutFragment">

    </RelativeLayout>

    <Button
        android:text="First"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="36dp"
        android:layout_marginStart="36dp"
        android:layout_marginBottom="23dp"
        android:id="@+id/buttonFirstFragment" />

    <Button
        android:text="Second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonFirstFragment"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="39dp"
        android:layout_marginEnd="39dp"
        android:id="@+id/buttonSecondFragment" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonFirst;
    Button buttonSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final FragmentManager fragmentManager=getSupportFragmentManager();

        final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

        FirstFragment firstFragment=new FirstFragment();

        fragmentTransaction.add(R.id.relativeLayoutFragment,firstFragment);

        fragmentTransaction.commit();

        buttonFirst= (Button) findViewById(R.id.buttonFirstFragment);
        buttonSecond= (Button) findViewById(R.id.buttonSecondFragment);

        buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentManager fragmentManager=getSupportFragmentManager();

                final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

                FirstFragment firstFragment=new FirstFragment();

                fragmentTransaction.replace(R.id.relativeLayoutFragment,firstFragment);

                fragmentTransaction.commit();
            }
        });

        buttonSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentManager fragmentManager=getSupportFragmentManager();

                final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

                SecondFragment secondFragment=new SecondFragment();

                fragmentTransaction.replace(R.id.relativeLayoutFragment,secondFragment);

                fragmentTransaction.commit();
            }
        });
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/029