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,
- <string-array name="titles">
- <item>Activity</item>
- <item>Services</item>
- <item>Broad Cast Receiver</item>
- <item>Content Providers</item>
- </string-array>
- <string-array name="description">
- <item>This is the Activity Decription</item>
- <item>This is the Service Decription</item>
- <item>This is the Broad Cast Receiver Decription</item>
- <item>This is the Content Provider Decription</item>
- </string-array>
FragmentA for showing the Listview,
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);
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/listView"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
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,
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,
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,
- public void setCommunicator(Communicator comm)
- {
- this.communicator = comm;
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:background="#ff00dd"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_margin="20dp"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:text="Medium Text"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
- public void changeText(int index)
- {
- String[] arary = getResources().getStringArray(R.array.description);
- textView.setText(arary[index]);
- }



Vignesh ManiPosted Apr 6, 2016, 12:44 PM
nice
Rahul Kumar SaxenaPosted Apr 5, 2016, 1:11 PM
Good Work