Android Dynamically Change Fragment

We have explained in previous tutorial how to add resource layout so that application is self-aware for smart device orientation change. Next step is to do the same thing but not with resource folder but dynamically. So android dynamically change fragment, i.e. android dynamically adding fragment is on. Let start with new empty project. Then add two fragments, one call landscape and another portrait. Landscape fragment look might be something like this:

Landscape-Fragment-Look-Example

Really important change in XML code is to remove tool:context line of code:

tools:context=”cirvirlab.androiddynamicallychangefragmentexample.Landscape”

For emphasizing importance of removing this line of code see figure bellow,

Delete-tools-context-line-in-Landscape-fragment-XML

Then add one more fragment and call it Portrait (guess why).
Portrait fragment might look like this:

Portrait-Fragment-Look-Example

Also, delete tool:context line of code in Portrait fragment XML code.

Delete-tools-context-line-in-Portrait-fragment-XML

Our final goal is to have zero functionality application that react on orientation change of the mobile device dynamically:

Adding-Android-Dynamically-Adding-Fragment

Then go to content_main.xml and add two fragments statically.

adding-fragment-in-this-example

Also delete android:name in content_main.xml code:

Delete-android-fragment-name-in-XML

Just keep layout_width, layout_height, and id. You can copy and paste this code to add XML code for fragment Portrait.

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:tools=”http://schemas.android.com/tools”

xmlns:app=”http://schemas.android.com/apk/res-auto”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

app:layout_behavior=”@string/appbar_scrolling_view_behavior”

tools:showIn=”@layout/activity_main”

tools:context=”.MainActivity”>

<fragment

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:id=”@+id/fragmentLandscape” />

<fragment

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:id=”@+id/fragmentPortrait” />

</RelativeLayout>

But after this stage, when you start to run application, it will crash.

Screenshot-2016-04-12-13-33-23

This can be quite frustrating. But solution is simple, just go to MainActivity.java and comment or remove following line of the code:

//      setContentView(R.layout.activity_main);

Like this

Remove-or-comment-setContentView

Now application will not crash. We came to crucial point to write the code that will replace one fragment by another when orientation change. So let’s go.
In onCreate method of MainActivity.java we will use:

  • fragmentManager object of FragmentManager class
  • transactionManager object of Transactionmanagament class
  • configuration object of Configuration class

Like this:

  • FragmentManager fragmentManager = getFragmentManager();
  • FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  • Configuration configuration = getResources().getConfiguration();

Then just check the orientation.

Key moment in here is to use replace method in fragmentTransaction object rather then add. Also note that code editor will not offer code for arguments of replace method that might be confusing. However, type

Landscape landscape = new Landscape();
fragmentTransaction.replace(android.R.id.content,landscape);

Do similar for portrait orientation. At the end of the code just commit. Code is given in figure bellow:

Android-Dynamically-Change-Fragment

You can download Android Dynamically Change Fragment example

External links:

Android Dynamically Change Fragment on Developer
Android Dynamically Change Fragment on Stackoverflow

Posted in android Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*