Switching between Fragment view

I am giving an example to switch between two layouts in a fragments:

First declare a layout with two fragments:(it depends on how many fragments you want in your layout)

fragment_layout_example.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

     <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/Fragment2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.SecondFragment" >

        <!-- Preview: layout=@layout/details -->
    </fragment>
</LinearLayout>

Above layout will display two fragments Fragment1 and Fragment2. For Fragment1 I had declared the container as content of the container is going to changed at runtime. So not declared the Fragment class here. for more on this check

http://developer.android.com/training/basics/fragments/fragment-ui.html

Then create a class FragmentExampleActivity which extends Activity. In case you are using Fragment in Backward compatibility mode then extend FragmentActivity

  public class FragmentExampleActivity extends FragmentActivity{

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_layout_example);

     // Check that the activity is using the layout version with
    // the fragment_container FrameLayout
    if (findViewById(R.id.fragment_container) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of Fragment1
        Fragment1 firstFragment = new Fragment1();

        // In case this activity was started with special instructions from an Intent,
        // pass the Intent's extras to the fragment as arguments
        firstFragment.setArguments(getIntent().getExtras());

        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
  }

 }

To create a layout for two fragments create two classes which extends Fragment

public class Fagment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //set the layout you want to display in First Fragment
    View view = inflater.inflate(R.layout.fragment1,
            container, false);
    return view;

}

}

Same way create Fragment class for second Fragment and set the layout

Now If you want to switch your fragment layout in Fragment1 to another layout on click of a button then create another class say Fragment3.java and set the layout you want to switch and write the below code inside the Fragment1.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment3=(Button)getView().findViewById(R.id.Button1);
    showFragment3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();

            Fragment3 fragment3 = new Fragment3();
            fragmentTransaction.replace(R.id.Fragment1, fragment3);
//provide the fragment ID of your first fragment which you have given in
//fragment_layout_example.xml file in place of first argument
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

}

Now to come back again on the first Fragment you can click on back button. But if you want to come back on click of button then write the below code in Fragment3.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button showFragment1 = (Button) getView().findViewById(
            R.id.Button2);
    showFragment1 .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            getFragmentManager().popBackStack();
        }
    });

}

Thanks! Hope it will help you...


You should use a FrameLayout for that, that way you don't have to specify the fragment class in the XML and that way it is not limited to one class.

<FrameLayout 
    android:id="@+id/contentFragment"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" />

and than you can set the fragment in the code like this

Fragment fragment = new YourFragment();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();