Android layouts (Navigation Drawer Activity, Settings Activity,MasterDetail Activity)

This blog post contains basic tutorial on how to create a simple layout in Android using templates (useful for newbies to Android development). I have Used Android Studio beta (0.8.0 version) because I have had some problems with sdk tools 23 in Eclipse. But at the end templates and files generated are more or less the same.

First I created a project with Navigation Drawer Activity template. With this template I created navigation on the left like it is for example in Google Play. I named activity MainActivity
This template creates following basic layout:
After that I renamed sections into Home, Setting and About. This is done in strings.xml file:

<string name="title_section1">Home</string>
<string name="title_section2">Settings</string>
<string name="title_section3">About</string>

Next I wanted to hide Example action and Settings button in upper right corner. This is done by commenting out following line of code in MainActivity.java method onCreateOptionsMenu:

getMenuInflater().inflate(R.menu.main, menu);

and in file NavigationDrawerFragment.java method onCreateOptionsMenu:

inflater.inflate(R.menu.global, menu);

Later this can be modified to Search or whatever is needed.

After basic layout it is time to create SettingsActivity using template
It is important to choose Parent Activity (Hierarchical Parent) since it creates back button to MainActivity. We need to call this activity from Navigation Drawer in MainActivity.java.

 @Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    switch (position) {
        case 0:
            //Home
            break;
        case 1:
            //Settings
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            break;
        case 2:
            //About
            break;
    }
}

When you click on Settings it should show you settings activity:
You can make your own settings fields and headers by changing files in /res/xml/ folder.
Last activity to include is Master/Detail Flow activity.
I did not need to specify any parent activity because MainActivity will use ItemListFragment to display list. The best example how to do this is to view ItemListActivity.java file. MainActivity implements ItemListFragment.Callbacks and implement onItemSelected method. Then I copied part of code from onCreate in ItemListActivity and paste it to onCreate in MainActivity.

if (findViewById(R.id.item_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((ItemListFragment) getFragmentManager()
                .findFragmentById(R.id.item_list))
                .setActivateOnItemClick(true);
}

The same as before I copied onItemSelected.

if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getFragmentManager().beginTransaction()
                .replace(R.id.item_detail_container, fragment)
                .commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, ItemDetailActivity.class);
        detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
    }

Last thing is to load fragment on Home click from Navigation Drawer.

    @Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    switch (position) {
        case 0:
            //Home
            fragmentManager.beginTransaction()
                    .replace(R.id.container, new ItemListFragment())
                    .commit();
            break;
        case 1:
            //Settings
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            break;
        case 2:
            //About
            fragmentManager.beginTransaction()
                    .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                    .commit();
            break;
    }
}

Screenshot shows the output of Master/Detail Flow.

I created a simple layout with three activities (MainActivity, Settings Activity and Master/Detail Activity) because I had problems binding together different activities. Hopefuly it helps you with the idea on how to use templates and customize them for your needs.

For more detail we put the sample code (examples) to Bitbucket:
Android examples git repository
NOTE: Checkout layouts branch!