===============
MainActivity.java
===============
Fragment1 fragment1 = new Fragment1();
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.main_layout, fragment1)
.commit();
===============
Fragment1.java
===============
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false );
}
}
===============
Fragment1.xml
===============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#CBA"
android:layout_height="match_parent">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "This is fragment 1"
android:textColor = "#000000"
android:textSize = "25sp" />
</LinearLayout>