Changes

Jump to: navigation, search
no edit summary
<brtable border="0" cellspacing="0" width="100%"><brtr>A clear understanding <td width="20%">[[Implementing an Android Navigation Drawer|Previous]]<td align="center">[[Android Studio Development Essentials - Java Edition|Table of how to use fragments (as outlined in the preceding chapters) is essential in order to be able to make use Contents]]<td width="20%" align="right">[[An Overview of the MasterAndroid Intents|Next]]</Detail Flow template. This chapter will apply these skills to explain the concept td><tr><td width="20%">Implementing an Android Navigation Drawer<td align="center"><td width="20%" align="right">An Overview of the MasterAndroid Intents</Detail user interface design before exploring in detail the elements that make up the Mastertd></Detail Flow template included with Android Studio. Finally, an example application will be created that demonstrates the steps involved in modifying the template to meet the specific needs of the application developer.table><hr>
== The Android Master/Detail Flow ==
A master<htmlet>androidstudio32</detail flow is an interface design concept whereby a list of items (referred to as the master list) is displayed to the user. On selecting an item from the list, additional information relating to that item is then presented to the user within a details panel. An email application might, for example, consist of a master list of received messages consisting of the address of the sender and the subject of the message. Upon selection of a message from the master list, the body of the email message would appear within the detail panel.htmlet>
On tablet sized Android device displays, the master list appears in a narrow vertical panel along the left hand edge of the screen. The remainder of the display is devoted to the detail panel in an arrangement referred to as two-pane mode. Figure 24-1, for example, shows the master/detail two-pane arrangement with three master items listed and the content of item three listed in the detail panel:
This chapter will explain the concept of the Master/Detail user interface design before exploring, in detail, the elements that make up the Master/Detail Flow template included with Android Studio. An example application will then be created that demonstrates the steps involved in modifying the template to meet the specific needs of the application developer.
[[Image:android_studio_large_screen_master_detail.png|== The Master/Detail flow on a large Android screen]] Figure 24-1Flow ==
A master/detail flow is an interface design concept whereby a list of items (referred to as the master list) is displayed to the user. On selecting an item from the list, additional information relating to that item is then presented to the user within a detail pane. An email application might, for example, consist of a master list of received messages consisting of the address of the sender and the subject of the message. Upon selection of a message from the master list, the body of the email message would appear within the detail pane.
On smaller, phone tablet sized Android devicesdevice displays in landscape orientation, the master list takes up appears in a narrow vertical panel along the left-hand edge of the entire screen and . The remainder of the detail panel appears on a separate screen which appears when a selection display is made from the master list. In this mode, devoted to the detail screen includes pane in an action bar entry arrangement referred to return to the master listas two-pane mode. Figure 2451-21, for example, illustrates both shows the master /detail, two-pane arrangement with master items listed and detail screens for the same three content of item list on a 4” phone screenone displayed in the detail pane:
[[Image:android_studio_small_screen_master_detail.png|Master/Detail flow on a small Android screen]]
Figure 24-2[[Image:as3.0_master_detail_tablet.png]]
== Creating a Master/Detail Flow Activity ==
In the next section of this chapterOn smaller, phone sized Android devices, the different elements that comprise master list takes up the entire screen and the Master/Detail Flow template will be covered in some detail. This is best achieved by creating pane appears on a project using separate screen which appears when a selection is made from the Master/Detail Flow template master list. In this mode, the detail screen includes an action bar entry to return to use while working through the informationmaster list. This project will subsequently be used as Figure 51-2 for example, illustrates both the basis master and detail screens for the tutorial at the end of the chapter.same item list on a 4” phone screen:
Begin by launching Android Studio and creating a new project with both the Application and Module names set to MasterDetailFlow. Specify a suitable package name for the project (or use the default com.example.masterdetailflow.masterdetailflow package name as a placeholder if you do not have a company URL).
Select API 8[[Image: Android 2as3.2 (Froyo) as the minimum SDK and API 19: Android 4.4 (KitKat) for both the target SDK and compilation options. Make sure that the Create activity option is selected before clicking on the Next button to proceed0_master_detail_portrait_both.png]]
When the activity configuration screen of the New Project dialog appears, select the Master/Detail Flow option as illustrated in Figure 24-3 before clicking on Next once again:
== Creating a Master/Detail Flow Activity ==
[[Image:android_studio_select_master_detail_flow_activity.png|Selecting In the next section of this chapter, the different elements that comprise the Master/Detail Flow template will be covered in Android Studio]] Figure 24-3some detail. This is best achieved by creating a project using the Master/Detail Flow template to use while working through the information. This project will subsequently be used as the basis for the tutorial at the end of the chapter.
Create a new project in Android Studio, entering MasterDetailFlow into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
The next On the form factors screen (Figure 24-4) provides , enable the opportunity to configure Phone and Tablet option and set the objects that will be displayed within the master/detail activityminimum SDK setting to API 26: Android 8.0 (Oreo). In After the tutorial later in this chapterproject has been created, the master list will contain a number of web site names which, when selected, will load the chosen web site into a web view within minSdkVersion setting in the detail panelbuild. With these requirements gradle (module: app) file located under Gradle Scripts in mind, set the Object Kind field to “Website” and the Object Kind Plural setting Project tool window may be changed to “Websites”target older Android versions if required.
When the activity configuration screen of the New Project dialog appears, select the Master/Detail Flow option as illustrated in Figure 51-3 before clicking on Next once again:
[[Image:android_studio_configure_master_detail_kinds.png|Configuring Master/Detail Flow object kind names in Android Studio]]
Figure 24-4
[[Image:as3.0_master_detail_template.png]]
Finally, click finish to create the new Master/Detail Flow based application project.
== The Anatomy of next screen (Figure 51-4) provides the Masteropportunity to configure the objects that will be displayed within the master/Detail Flow Template ==detail activity. In the tutorial later in this chapter, the master list will contain a number of web site names which, when selected, will load the chosen web site into a web view within the detail pane. With these requirements in mind, set the Object Kind field to “Website”, and the Object Kind Plural and Title settings to “Websites”.
Once a new project has been created using the Master/Detail Flow template, a number of Java and XML layout resource files will have been created automatically. It is important to gain an understanding of these different files in order to be able to adapt the template to specific requirements. A review of the project within the Android Studio Project tool window will reveal the following files, where <kind_name> is replaced by the Object Kind name that was specified when the project was created (this being “Website” in the case of the MasterDetailFlow example project):
* '''<kind_name>ListActivity[[Image:as3.java''' – This is the main activity of the project, the purpose of which is to display the master list to the user (the layout for which is stored in the activity_<kind_name>_list.xml XML fragment resource file). If the class detects that the display is large enough to support two-pane mode, an instance of the <kind_name>_list fragment from the activity_<kind_name>_twopane.xml file is created and displayed, otherwise, the instance contained in the activity_<kind_name>_list.xml file is used. This class also implements and sets up the onItemSelected() callback method that will be called when the user makes a selection from the master list. It is the responsibility of this method to create and display an instance of the detail panel. In the case of two-pane mode, the detail panel is handled by creating an instance of the <kind_name>DetailFragment class and adding it to the current activity. On smaller device displays, this instead involves launching a second activity in the form of the <kind_name>DetailActivity class (which will then, in turn, create an instance of the <kind_name>DetailFragment class)0_master_detail_object_kind.png]]
* '''activity_<kind_name>_twopane.xml''' – Contains both the <kind_name>_list master list fragment and <kind_name>_detail_container FrameLayout declarations for the detail pane to be used when the application is running on a device display large enough to support two-pane mode.
* '''activity_<kind_name>_list.xml''' – The XML layout resource file containing Finally, click Finish to create the <kind_name>_list fragment for the master list to be used on displays too small to support two-pane modenew Master/Detail Flow based application project.
* '''<kind_name>ListFragment.java''' – The Java class that accompanies the activity_<kind_name>_list.xml fragment resource file. This class contains a number == The Anatomy of methods that serve to identify and highlight the user’s current master list selection. Master/Detail Flow Template ==
* '''<kind_name>DetailActivityOnce a new project has been created using the Master/Detail Flow template, a number of Java and XML layout resource files will have been created automatically.java''' – The Java class representing the activity for It is important to gain an understanding of these different files in order to be able to adapt the detail panel for use on devices too small template to handle two-pane modespecific requirements. This class creates an instance A review of the <kind_name>FragmentDetail class and displays project within the Android Studio Project tool window will reveal the following files, where <kind_nameitem>_detail_container FrameLayout container declared is replaced by the Object Kind name that was specified when the project was created (this being “Website” in the activity_<kind_name>_detail.xml file. case of the MasterDetailFlow example project):
* '''activity_<kind_nameitem>_detail_list.xml''' – The XML resource top level layout that accompanies file for the master list, this file is loaded by the <kind_nameitem>ActivityDetail.java ListActivity class file used on small screen devices. By default, this This layout contains a FrameLayout instance to which will be added user interface elements declared by toolbar, a floating action button and includes the <kind_nameitem>FragmentDetail class_list.xml file.
* '''<kind_nameitem>DetailFragmentListActivity.javajava''' – The Java activity class that accompanies the fragment_<kind_name>_detail.xml XML resource file. The code in this method loads the data associated with responsible for displaying and managing the master list and displays the content of (declared in the fragment_activity_<kind_nameitem>_detail_list.xml file ) and for both displaying and responding to the userselection of items within that list.
* '''fragment_<kind_nameitem>_detail_list.xml''' – The <kind_name>_detail user interface for layout file used to display the master list of items in single-pane mode where the master list and detail pane appear on different screens. This file consists of a RecyclerView object configured to use the LinearLayoutManager. The RecyclerView element declares that each item in the master list is to be displayed using the layout declared within the onCreateView() method of the <kind_nameitem>DetailFragment class_list_content. By default, this contains a single TextView object instance and is used in both two-pane and small screen modesxml file.
* '''DummyContent<item>_list.javaxml (w900dp)''' – A class The layout file intended to provide sample data for the templatemaster list in the two-pane mode used on tablets in landscape (where the master list and detail pane appear side by side). This class can either be modified file contains a horizontal LinearLayout parent within which resides a RecyclerView to meet application needs, or replaced entirely. By defaultdisplay the master list, and a FrameLayout to contain the content provided by this class simply consists of a number the detail pane. As with the single-pane variant of string itemsthis file, the RecyclerView element declares that each item in the list be displayed using the layout contained within the <item>_list_content.xml file.
Two additional files are also of interest purely * '''<item>_content_list.xml''' – This file contains the layout to be used for each item in the sake master list. By default, this consists of understanding how the two TextView objects embedded in a horizontal LinearLayout but may be changed to meet specific application is able to identify whether to use two-pane mode or not. These files are res/values-large/refs.xml and res/values-sw600dp/refs.xmlneeds.
As will be outlined in greater * '''activity_<item>_detail.xml''' – The top level layout file used for the detail pane when running in the chapter entitled Handling Different Android Devices single-pane mode. This layout contains an app bar, collapsing toolbar, scrolling view and Displays, each application project has multiple sets of resources that target different display sizesa floating action button. At runtime, this layout file is loaded and displayed by the Android system automatically uses the resource set that most closely matches the physical display size of the device<item>DetailActivity class. The values-large and values-sw600dp resources are, of course, used in devices with larger displays. The ref.xml files in these folders simply declare an alias that causes the two-pane mode layouts to be used:
* '''<preitem>DetailActivity.java''' – This class displays the layout defined in the activity_<item name="activity_item_list" type="layout">@layout/activity_item_twopane_detail.xml file. The class also initializes and displays the fragment containing the detail content defined in the </item>_detail.xml and </preitem>DetailFragment.java files.
== Modifying * '''<item>_detail.xml''' – The layout file that accompanies the Master/Detail Flow Template ==<item>DetailFragment class and contains the layout for the content area of the detail pane. By default, this contains a single TextView object, but may be changed to meet your specific application needs. In single-pane mode, this fragment is loaded into the layout defined by the activity_<item>_detail.xml file. In two-pane mode, this layout is loaded into the FrameLayout area of the <item>_list.xml (w900dp) file so that it appears adjacent to the master list.
Whilst * '''<item>DetailFragment.java''' – The fragment class file responsible for displaying the <item>_detail.xml layout and populating it with the content to be displayed in the detail pane. This fragment is initialized and displayed within the <item>DetailActivity.java file to provide the content displayed within the activity_<item>_detail.xml layout for single-pane mode and the <item>_list.xml (w900dp) layout for two-pane mode. * '''DummyContent.java''' – A class file intended to provide sample data for the template. This class can either be modified to meet application needs, or replaced entirely. By default, the content provided by this class simply consists of a number of string items. == Modifying the Master/Detail Flow Template == While the structure of the Master/Detail Flow template can appear confusing at first, the concepts will become clearer as the default template is modified in the remainder of this chapter. As will become evident, much of the functionality provided by the template can remain unchanged for many master/detail implementation requirements.
In the rest of this chapter, the MasterDetailFlow project will be modified such that the master list displays a list of web site names and the detail pane altered to contain a WebView object instead of the current TextView. When a web site is selected by the user, the corresponding web page will subsequently load and display in the detail pane.
== Changing Changing the Content Model ==
The content for the example as it currently stands is defined by the DummyContent class file. Begin, therefore, by selecting the DummyContent.java java file (located in the Project tool window in the src app -> main java -> java com.ebookfrenzy.masterdetailflow -> <package name> dummy folder) and reviewing the code. At the bottom of the file is a declaration for a class named DummyItem which is currently able to store two String objects representing a content string and an ID. The updated project, on the other hand, will need each item object to contain an ID string, a string for the web site name, and a string for the corresponding URL of the web site. To add these features, modify the DummyItem class so that it reads as follows:
<pre>
public public static class DummyItem { public        public String id; public        public String website_name; public        public String website_url;  public        public DummyItem(String id, String website_name, String              String website_url)        { this              this.id = id; this              this.website_name = website_name; this              this.website_url = website_url;        }                @Override public        public String toString() { return               return website_name;        }
}
</pre>
<htmlet>androidstudio32</htmlet>Note that the encapsulating DummyContent class currently contains a for loop that adds three 25 items in the form by making multiple calls to methods named createDummyItem() and makeDetails(). Much of strings that read “Item 1”, “Item 2” this code will no longer be required and “Item 3”should be deleted from the class as follows:
<pre>
public public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();  private static { // Add 3 sample items. void addItem(new DummyItem("1", "Item 1"item));{ addItem    ITEMS.add(new DummyItem("2", "Item 2")item); addItem    ITEM_MAP.put(new DummyItem("3"item.id, "Item 3")item); }
</pre>
<pre>
public public static final Map<String, DummyItem> ITEM_MAP = new           new HashMap<String, DummyItem>(); 
static {
       // Add 3 sample items. addItem        addItem(new DummyItem("1", "eBookFrenzy",                 "httphttps://www.ebookfrenzy.com")); addItem        addItem(new DummyItem("2", "Amazon",                 "httphttps://www.amazon.com")); addItem        addItem(new DummyItem("3", "WikipediaNew York Times",                 "httphttps://www.wikipedianytimes.orgcom"));}
</pre>
The code now takes advantage of the modified DummyItem class to store an ID, web site name and URL for each item.
== Changing Changing the Detail Pane ==
The detail information shown to the user when an item is selected from the master list is currently displayed via the layout contained in the fragment_website_detailwebsite_detail.xml file. By default , this contains a single view in the form of a TextView. Since the TextView class is not capable of displaying a web page, this needs to be changed to a WebView object for the purposes of this tutorial. To achieve this, navigate to the src -> main app -> res -> layout -> fragment_website_detailwebsite_detail.xml file in the Project tool window and double -click on it to load it into the Designer Layout Editor tool. Switch to Design Text mode before right-clicking on the white background of the display canvas (which represents the TextView) and selecting delete the Delete menu option current XML content from the resulting menu to remove the TextView from the layoutfile. Locate Replace this content with the WebView component from the Widgets section of the Designer tool palette and drag and drop it onto the layout canvas. With the newly added WebView instance selected in the design layout, use either the Properties panel or the Designer toolbar buttons to change both the layout:height and layout:width properties to match_parent so that the view fills the available space as illustrated in Figure 24-5following XML:
<pre>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/website_detail"
    tools:context= ".WebsiteDetailFragment">
</WebView>
</pre>
[[Image:android_studio_detail_webview.png|A WebView added Switch to a Design mode and verify that the layout now matches that shown in Android Studio Designer]] Figure 2451-5:
Double click on the WebView instance in the layout and, in the resulting panel, change the ID of the component to @+id/website_detail[[Image:as3.0_master_detail_webview.png]]
== Modifying the WebsiteDetailFragment Class ==
At this point the user interface detail panel has been modified but the corresponding Java class is still designed for working with a TextView object instead of a WebView. Load the source code for this class by double clicking on == Modifying the WebsiteDetailFragment.java file in the Project tool window. Within the source file locate the onCreateView() method which should read as outlined in the following listing:Class ==
</pre>package comAt this point the user interface detail pane has been modified but the corresponding Java class is still designed for working with a TextView object instead of a WebView.ebookfrenzyLoad the source code for this class by double-clicking on the WebsiteDetailFragment.masterdetailflowjava file in the Project tool window.masterdetailflow;
import androidIn order to load the web page URL corresponding to the currently selected item only a few lines of code need to be changed.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;Once this change has been made, the code should read as follows:
import <pre>package com.exampleebookfrenzy.masterdetailflow.masterdetailflow.dummy.DummyContentpublic class WebsiteDetailFragment extends Fragment {
.
.
import android.webkit.WebViewClient;import android.webkit.WebView;import android.webkit.WebResourceRequest;..public class WebSiteDetailFragment extends Fragment {...    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         if (getArguments().containsKey(ARG_ITEM_ID)) {            // Load the dummy content specified by the fragment            // arguments. In a real-world scenario, use a Loader            // to load content from a content provider.            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));             Activity activity = this.getActivity();            CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);            if (appBarLayout != null) {                appBarLayout.setTitle(mItem.website_name);            }        }    }     @Overridepublic     public View onCreateView(LayoutInflater inflater, ViewGroup               ViewGroup container, Bundle savedInstanceState) {  View         View rootView = inflater.inflate(R             R.layout.fragment_website_detail, container, false);          // Show the dummy content as text in a TextView. if         if (mItem != null) {             ((TextViewWebView) rootView.findViewById(R.id.website_detail))                     .setTextloadUrl(mItem.contentwebsite_url);             WebView webView = (WebView)                  rootView.findViewById(R.id.website_detail);            webView.setWebViewClient(new WebViewClient(){                @Override                public boolean shouldOverrideUrlLoading(                    WebView view, WebResourceRequest request) {                    return super.shouldOverrideUrlLoading(                                 view, request);                }            }); return             webView.getSettings().setJavaScriptEnabled(true);            webView.loadUrl(mItem.website_url);        }         return rootView;    }
}
</pre>
In order The above changes modify the onCreate() method to load display the web page URL corresponding to site name on the currently selected item only one line of code needs to be changed. Once this change has been made, the method should read as follows (note also the addition of the import directive for the android.webkit.WebView library)app bar:
<pre>
package comappBarLayout.ebookfrenzysetTitle(mItem.masterdetailflow.masterdetailflowwebsite_name);</pre>
import androidThe onCreateView() method is then modified to find the view with the ID of website_detail (this was formally the TextView but is now a WebView) and extract the URL of the web site from the selected item.osAn instance of the WebViewClient class is created and assigned the shouldOverrideUrlLoading() callback method.Bundle;import androidThis method is implemented so as to force the system to use the WebView instance to load the page instead of the Chrome browser.Finally, JavaScript supportis enabled on the webView instance and the web page loaded.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.webkit.WebView;
import com.example.masterdetailflow.masterdetailflow.dummy.DummyContent;== Modifying the WebsiteListActivity Class ==
public class WebsiteDetailFragment extends Fragment A minor change also needs to be made to the WebsiteListActivity.java file to make sure that the web site names appear in the master list. Edit this file, locate the onBindViewHolder() method and modify the setText() method call to reference the web site name as follows: <pre>public void onBindViewHolder(final ViewHolder holder, int position) {    holder.mItem = mValues.get(position);    holder.mIdView.setText(mValues.get(position).id);    holder.mContentView.setText(mValues.get(position).website_name);
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
View rootView =
inflater.inflate(R.layout.fragment_website_detail,
container, false);
 
// Show the dummy content as text in a TextView.
if (mItem != null) {
((WebView) rootView.findViewById(R.id.website_detail))
.loadUrl(mItem.website_url);
}
return rootView;
}
}
</pre>
All that this change does is find the view with the ID of website_detail (this was formally the TextView but is now a WebView), extracts the URL of the web site from the selected item and instructs the WebView object to load that page.== Adding Manifest Permissions ==
== Adding Manifest Permissions == The final step is to add internet permission to the application via the manifest file. This will enable the WebView object to access the internet and download web pages. Navigate to , and load the AndroidManifest.xml file in the Project tool window (app -> manifests), and double -click on it to load it into the editor. Once loaded, add the appropriate permission line to the file:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package    package="com.example.masterdetailflow.masterdetailflow" >              <uses-permission android:name="android.permission.INTERNET" />      <application android        android:allowBackup="true" android        android:icon="@drawablemipmap/ic_launcher" android        android:label="@string/app_name" android        android:theme="@style/AppTheme" >.
.
.
</manifest>
</pre>
== Running Running the Application == Compile and run the application on a suitably configured emulator or an attached Android device. Depending on the size of the display, the application will appear either in small screen or two-pane mode. Regardless, the master list should appear primed with the names of the three web sites defined in the content model. Selecting an item should cause the corresponding web site to appear in the detail pane as illustrated in two-pane mode in Figure 51-6:  [[Image:as3.0_master_detail_running.png]]
Compile and run the application on a suitably configured emulator or an attached Android device. Depending on the size of the display, the application will appear either in small screen or two-pane mode. Regardless, the master list should appear primed with the names of the three web sites defined in the content model. Selecting an item should cause the corresponding web site to appear in the detail panel as illustrated in two-pane mode in Figure 24-6:
[[Image:android_studio_master_detail_example_running.png|The completed Android Studio based Master Detail Flow example app running]]== Summary ==
Figure 24-6A master/detail user interface consists of a master list of items which, when selected, displays additional information about that selection within a detail pane. The Master/Detail Flow is a template provided with Android Studio that allows a master/detail arrangement to be created quickly and with relative ease. As demonstrated in this chapter, with minor modifications to the default template files, a wide range of master/detail based functionality can be implemented with minimal coding and design effort.
<htmlet>androidstudio32</htmlet>
== Summary ==
A master/detail user interface consists <hr><table border="0" cellspacing="0" width="100%"><tr><td width="20%">[[Implementing an Android Navigation Drawer|Previous]]<td align="center">[[Android Studio Development Essentials - Java Edition|Table of a master list Contents]]<td width="20%" align="right">[[An Overview of items which, when selected, display additional information about that selection within a detail panel. The Master/Detail Flow is a template provided with Android Studio that allows a masterIntents|Next]]</detail arrangement to be created quickly and with relative ease. As demonstrated in this chapter, with minor modifications to the default template files, a wide range td><tr><td width="20%">Implementing an Android Navigation Drawer<td align="center"><td width="20%" align="right">An Overview of masterAndroid Intents</td></detail based functionality can be implemented with minimal coding and design effort.table>

Navigation menu