• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package ${packageName};
2
3<#if minApiLevel < 14>import android.annotation.TargetApi;</#if>
4import android.app.ActionBar;
5import android.os.Bundle;
6<#if minApiLevel < 14>import android.content.Context;
7import android.os.Build;</#if>
8import android.support.v4.app.Fragment;
9import android.support.v4.app.FragmentActivity;
10import android.support.v4.app.NavUtils;
11import android.view.Gravity;
12import android.view.LayoutInflater;
13import android.view.Menu;
14import android.view.MenuItem;
15import android.view.View;
16import android.view.ViewGroup;
17import android.widget.ArrayAdapter;
18import android.widget.TextView;
19
20public class ${activityClass} extends FragmentActivity implements ActionBar.OnNavigationListener {
21
22    /**
23     * The serialization (saved instance state) Bundle key representing the
24     * current dropdown position.
25     */
26    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
27
28    @Override
29    protected void onCreate(Bundle savedInstanceState) {
30        super.onCreate(savedInstanceState);
31        setContentView(R.layout.${layoutName});
32
33        // Set up the action bar to show a dropdown list.
34        final ActionBar actionBar = getActionBar();
35        actionBar.setDisplayShowTitleEnabled(false);
36        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
37        <#if parentActivityClass != "">
38        // Show the Up button in the action bar.
39        actionBar.setDisplayHomeAsUpEnabled(true);
40        </#if>
41
42        // Set up the dropdown list navigation in the action bar.
43        actionBar.setListNavigationCallbacks(
44                // Specify a SpinnerAdapter to populate the dropdown list.
45                new ArrayAdapter<String>(
46                        <#if minApiLevel gte 14>actionBar.getThemedContext()<#else>getActionBarThemedContextCompat()</#if>,
47                        android.R.layout.simple_list_item_1,
48                        android.R.id.text1,
49                        new String[] {
50                                getString(R.string.title_section1),
51                                getString(R.string.title_section2),
52                                getString(R.string.title_section3),
53                        }),
54                this);
55    }
56
57    <#if minApiLevel < 14>
58    /**
59     * Backward-compatible version of {@link ActionBar#getThemedContext()} that
60     * simply returns the {@link android.app.Activity} if
61     * <code>getThemedContext</code> is unavailable.
62     */
63    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
64    private Context getActionBarThemedContextCompat() {
65        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
66            return getActionBar().getThemedContext();
67        } else {
68            return this;
69        }
70    }
71    </#if>
72
73    @Override
74    public void onRestoreInstanceState(Bundle savedInstanceState) {
75        // Restore the previously serialized current dropdown position.
76        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
77            getActionBar().setSelectedNavigationItem(
78                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
79        }
80    }
81
82    @Override
83    public void onSaveInstanceState(Bundle outState) {
84        // Serialize the current dropdown position.
85        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
86                getActionBar().getSelectedNavigationIndex());
87    }
88
89    <#include "include_onCreateOptionsMenu.java.ftl">
90    <#include "include_onOptionsItemSelected.java.ftl">
91
92    @Override
93    public boolean onNavigationItemSelected(int position, long id) {
94        // When the given dropdown item is selected, show its contents in the
95        // container view.
96        Fragment fragment = new DummySectionFragment();
97        Bundle args = new Bundle();
98        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
99        fragment.setArguments(args);
100        getSupportFragmentManager().beginTransaction()
101                .replace(R.id.container, fragment)
102                .commit();
103        return true;
104    }
105
106    <#include "include_DummySectionFragment.java.ftl">
107
108}
109