CS4521:   Mobile and Topics in Web Programming



Solution

Exercise 4.3 - Dyanmic Fragments

You will be modifying the pervious exercise to now have switch dynamically between two Fragments in you active Activity.

 

 

Portrait Mode

portrail mode
Landscape Mode

landscape mode

(TO SWITCH orientation in emulator hit Ctrl + F11)

 

1) Open Eclipse project named Fragments from pervious exercise

 

In the res/layout folder,comment out the <fragment> tags in the main.xml file

2) in src folder open the FragmentsActivity.java file and add the bold code. This code will test the orientation (portrait or landscape) of the device and based on this information replace the currect Fragment in this Activity (the only for this application) associated with the id android.R.id.content.

package grewe.fragments;
-
import android.app.Activity;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Display;
import android.view.WindowManager;

//NOTE toggle between Horizontal and Vertical mode in emulator
// by hitting Cntrl-F11

public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     //add so that dynamically add fragments1 and 2
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

     //get the current display info
     WindowManager wm = getWindowManager();
     Display d = wm.getDefaultDisplay();
     if(d.getWidth() > d.getHeight())
          {     //landscape mode display fragment 1
                Fragment1 fragment1 = new Fragment1();
               //adroid.R.id.content refers to the content
               //view of the activity
               fragmentTransaction.replace(android.R.id.content, fragment1);
          }
     else
         {    //portrait --display fragment 2
               Fragment2 fragment2 = new Fragment2();
               fragmentTransaction.replace(android.R.id.content, fragment2);
         }

     fragmentTransaction.commit();

     setContentView(R.layout.main);
}
}

4) Run the application

© Lynne Grewe