[Solved] How to open a fragment on click of a button?


In your activity_layout.xml you should add a linearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/fragment_container"
           android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >

 </LinearLayout>

Then the rest is up to your code

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){    
    Fragment newCase=new NewCase();
    FragmentTransaction transaction=getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container,newCase); // give your fragment container id in first parameter
    transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
    transaction.commit();

}
});

Notice the Layout id in the xml and containe id same

solved How to open a fragment on click of a button?