The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #11 - Adding Simple Content

Now that we have seen how to work with assets, we can start putting them to use, by defining some “help” and “about” HTML files and displaying them in their respective activities.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Step #1: Adding Some Content

Your project may already have an assets/ folder. If not, create one. In Android Studio, right-click over the main sourceset directory, choose New > Directory from the context menu, fill in the name assets in the dialog, and click OK. This should give you an app/ module that looks like:

EmPubLite Project, Showing assets/ in main/ of app/
Figure 288: EmPubLite Project, Showing assets/ in main/ of app/

In assets/, create a misc/ sub-folder, by right-clicking over the assets/ folder and choosing to add a new directory named misc (e.g., New > Directory from the Android Studio context menu), giving you something like:

EmPubLite Project, Showing assets/misc/ in main/ of app/
Figure 289: EmPubLite Project, Showing assets/misc/ in main/ of app/

In assets/misc/, create two files, about.html and help.html. In Android Studio, right-click over the assets/misc/ folder in the project explorer, choose New > File from the context menu, fill in the desired filename in the dialog, and click OK.

The actual HTML content of these two files does not matter, so long as you can tell them apart when looking at them. If you prefer, you can download sample about.html and help.html files from the application’s GitHub repository, via the links.

Step #2: Using SimpleContentFragment

Now, open up SimpleContentActivity and replace the stub implementation that we have now with the following Java:

package com.commonsware.empublite;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class SimpleContentActivity extends Activity {
  public static final String EXTRA_FILE = "file";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getFragmentManager().findFragmentById(android.R.id.content)==null) {
      String file=getIntent().getStringExtra(EXTRA_FILE);
      Fragment f=SimpleContentFragment.newInstance(file);

      getFragmentManager().beginTransaction()
        .add(android.R.id.content, f).commit();
    }
  }
}

If you prefer, you can view this file’s contents in your Web browser via this GitHub link.

In onCreate(), we follow the standard recipe for defining our fragment if (and only if) we were started new, rather than restarted after a configuration change, by seeing if the fragment already exists. If we do need to add the fragment, we retrieve a string extra from the Intent used to launch us (identified as EXTRA_FILE), create an instance of SimpleContentFragment using that value from the extra, and execute a FragmentTransaction to add the SimpleContentFragment to our UI.

Step #3: Launching Our Activities, For Real This Time

The preview of this section is in the process of being translated from its native Klingon.

Step #4: Getting a Bit More Material

The preview of this section is presently indisposed.

Step #5: Seeing the Results

The preview of this section will not appear here for a while, due to a time machine mishap.

In Our Next Episode…

The preview of this section was fed to a gremlin, after midnight.