Stepping In Fragment With Landscape View In Android

Create a view for landscape and portrait in Android with fragments

 
Fragments are the most important feature in  Android Applications. In my previous articles, I have described how to create fragments. Now here I will explain how to add a view in both Portrait and Landscape mode.
 
Here I am creating a ListView in the first activity and on clicking on each item in the ListView it will show the Details in the Second Activity. In Landscape view, the details will be shown on the same page as the right side of the current Activity. In Portrait mode, it will navigate to the next Activity. We know that in the Tab there will be lots of space; to avoid this we can use fragments and show the details on the right side of the page. Please see the screenshot below, this will give you an exact idea about this.
 

Portrait mode view

 
 
 

Landscape

 
 
This is the task I am going to do now.
 
For this I have to string array, one for listing and one for the details, please see the string array,
  1. <string-array name="titles">    
  2.     <item>Activity</item>    
  3.     <item>Services</item>    
  4.     <item>Broad Cast Receiver</item>    
  5.     <item>Content Providers</item>    
  6.     
  7. </string-array>    
  8. <string-array name="description">    
  9.     <item>This is the Activity Decription</item>    
  10.     <item>This is the Service Decription</item>    
  11.     <item>This is the Broad Cast Receiver Decription</item>    
  12.     <item>This is the Content Provider Decription</item>    
  13. </string-array>  
    First create the Fragments for listview and details. That is FragmentA and FragmentB
     
    FragmentA for showing the Listview,
    1. <?xml version="1.0" encoding="utf-8"?>    
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    3. android:orientation="vertical" android:layout_width="match_parent"    
    4. android:layout_height="match_parent">    
    5.     <ListView    
    6. android:layout_width="match_parent"    
    7. android:layout_height="wrap_content"    
    8. android:id="@+id/listView"    
    9. android:layout_gravity="center_horizontal" />    
    10. </LinearLayout>   
      Now we will load the XML file to the fragmentA class file in the onCreateView(). and we will create an interface for communication between the Fragments. The interface is named Communicator and this interface has a method named respond(int index);
       
      With this interface, we are passing the clicked index value to the next fragment. In the FragmentA class there we need to create a method for setting the  communicator,
      1. public void setCommunicator(Communicator comm)    
      2. {    
      3.     this.communicator = comm;    
      4. }   
        Now create the FragmentB layout and java class. The layout file contains only a TextView which will be used to display the details. Please see the layout file below,
        1. <?xml version="1.0" encoding="utf-8"?>    
        2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        3. android:orientation="vertical" android:layout_width="match_parent"    
        4. android:background="#ff00dd"    
        5. android:layout_height="match_parent">    
        6.     <TextView    
        7. android:layout_width="wrap_content"    
        8. android:layout_margin="20dp"    
        9. android:layout_height="wrap_content"    
        10. android:textAppearance="?android:attr/textAppearanceMedium"    
        11. android:text="Medium Text"    
        12. android:id="@+id/textView"    
        13. android:layout_gravity="center_horizontal" />    
        14. </LinearLayout>  
          In the Java file, there will be a method named changeText(int index); this method will give the selected index from FragmentA to FragmentB with the help of communicator interface, please see the code below,
          1. public void changeText(int index)    
          2. {    
          3.     String[] arary = getResources().getStringArray(R.array.description);    
          4.     textView.setText(arary[index]);    
          5. }   
          Now create the second Activity and its layout. The SecondActivity will be called only in the Portrait view.
           
          Please see the layout file for second_activity,
          1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          2. android:orientation="vertical" android:layout_width="match_parent"    
          3. android:layout_height="match_parent">    
          4.     <fragment    
          5. android:layout_width="match_parent"    
          6. android:layout_height="wrap_content"    
          7. android:name="study.fragment.negociosit.com.fragementstudyworks.FragmentB"    
          8. android:id="@+id/fragmentb"    
          9. android:layout_gravity="center_horizontal" />    
          10. </LinearLayout>  
          In the Java class (This will be used only in the Portrait view) In the onCreate() call the method changeText(int),
          1. protected void onCreate(Bundle savedInstanceState)    
          2. {    
          3.     super.onCreate(savedInstanceState);    
          4.     setContentView(R.layout.second_activity);    
          5.     int index = getIntent().getIntExtra("index"0);    
          6.     FragmentB fragmentB = (FragmentB) getFragmentManager().findFragmentById(R.id.fragmentb);    
          7.     fragmentB.changeText(index);    
          8. }   
          Now create the main_activity for the portrait view,
          1. <?xml version="1.0" encoding="utf-8"?>    
          2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          3. android:orientation="vertical" android:layout_width="match_parent"    
          4. android:background="#ff1a1a"    
          5. android:layout_height="match_parent">    
          6.     
          7.     <fragment    
          8. android:layout_width="wrap_content"    
          9. android:layout_height="wrap_content"    
          10. android:name="study.fragment.negociosit.com.fragementstudyworks.FragmentA"    
          11. android:id="@+id/fragmenta"    
          12. android:layout_gravity="center_horizontal" />    
          13.     
          14. </LinearLayout>   
            Now comes the most important part of this application, which is creating the main layout for the landscape mode. For creating a layout file for Landscape mode please follow the steps.
             
            Right on Layout, New, then Layout resource file 
             
             
             
            Please see the code for activity_main for the landscape view,
            1. <?xml version="1.0" encoding="utf-8"?>    
            2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
            3. android:orientation="horizontal" android:layout_width="match_parent"    
            4. android:layout_height="match_parent">    
            5.     <fragment    
            6. android:layout_width="0dp"    
            7. android:layout_weight="1"    
            8. android:layout_height="wrap_content"    
            9. android:name="study.fragment.negociosit.com.fragementstudyworks.FragmentA"    
            10. android:id="@+id/fragmenta" />    
            11.     <fragment    
            12. android:layout_width="0dp"    
            13. android:layout_weight="1"    
            14. android:layout_height="wrap_content"    
            15. android:name="study.fragment.negociosit.com.fragementstudyworks.FragmentB"    
            16. android:id="@+id/fragmentb"    
            17. android:layout_gravity="center_horizontal" />    
            18. </LinearLayout>   
              This will contain both the fragments added in the layout with horizontal alignment with width 0dp and weight 1 for both so that it will be equal with and please see the code for MainActivity,
              1. public class MainActivity extends Activity implements Communicator    
              2. {    
              3.     private FragmentA fragmentA = null;    
              4.     private FragmentB fragmentB = null;    
              5.     private FragmentManager manager = null;    
              6.     @Override    
              7.     protected void onCreate(Bundle savedInstanceState)     
              8.     {    
              9.         super.onCreate(savedInstanceState);    
              10.         setContentView(R.layout.main_activity);    
              11.         manager = getFragmentManager();    
              12.         fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmenta);    
              13.         fragmentA.setCommunicator(this);    
              14.     }    
              15.     @Override    
              16.     public void resopond(int index)    
              17.     {    
              18.         fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentb);    
              19.         if (fragmentB != null && fragmentB.isVisible())    
              20.         {    
              21.             fragmentB.changeText(index);    
              22.         } else    
              23.         {    
              24.             Intent intent = new Intent(MainActivity.this, SecondActivity.class);    
              25.             intent.putExtra("index", index);    
              26.             startActivity(intent);    
              27.         }    
              28.     }    
              29. }   
                On clicking on the listitem in the FragmentA then the respond method will be called and it will check whether the device is in Portrait or Landscape and then it will do the necessary action according to the view. That is, in portrait mode it will call the next activity and pass the index with putExtra, it its in Landscape mode then the changeText() method of FragmentB will be called.
                 
                Read more articles on Android: