diff options
author | Tor Norbye <tnorbye@google.com> | 2012-05-25 17:05:55 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-06-04 16:57:34 -0700 |
commit | 56afaff9c34a60c0e850324938d101a0509f8a38 (patch) | |
tree | 8a945bf07d4cb162d2c1b0c1d13989ccaa55da41 /templates/activities/BlankActivity/root | |
parent | 6de0364094c5780cf39f4805ac507374c382da2f (diff) | |
download | sdk-56afaff9c34a60c0e850324938d101a0509f8a38.zip sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.gz sdk-56afaff9c34a60c0e850324938d101a0509f8a38.tar.bz2 |
Allow project templates to be bundled separately
This changeset moves the various FreeMarker templates out of the ADT
plugin and over into the tools install area. The code to instantiate
templates is simplified a bit now that it no longer needs to both
handle files on disk and files read out of the .jar.
There's a new first page to the various template wizards which is
shown if the tools are not up to date (specifically, if the
$SDK/tools/templates/ directory does not exist). This page explains
that the tools have to be updated, and the Next button is disabled
until they are up to date.
This CL also contains some other tweaks suggested by Roman to the
activity-to-layout name mapping and misc code changes.
Change-Id: I3bc65f54a6b79bbeedfb917a9d34ec0d312f3526
Diffstat (limited to 'templates/activities/BlankActivity/root')
15 files changed, 442 insertions, 0 deletions
diff --git a/templates/activities/BlankActivity/root/AndroidManifest.xml.ftl b/templates/activities/BlankActivity/root/AndroidManifest.xml.ftl new file mode 100644 index 0000000..ffcce79 --- /dev/null +++ b/templates/activities/BlankActivity/root/AndroidManifest.xml.ftl @@ -0,0 +1,15 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" > + + <application> + + <activity android:name=".${activityClass}" + android:label="@string/activity_name"> + <intent-filter android:label="@string/activity_name"> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + + </application> + +</manifest> diff --git a/templates/activities/BlankActivity/root/res/drawable-hdpi/ic_action_search.png b/templates/activities/BlankActivity/root/res/drawable-hdpi/ic_action_search.png Binary files differnew file mode 100644 index 0000000..67de12d --- /dev/null +++ b/templates/activities/BlankActivity/root/res/drawable-hdpi/ic_action_search.png diff --git a/templates/activities/BlankActivity/root/res/drawable-mdpi/ic_action_search.png b/templates/activities/BlankActivity/root/res/drawable-mdpi/ic_action_search.png Binary files differnew file mode 100644 index 0000000..134d549 --- /dev/null +++ b/templates/activities/BlankActivity/root/res/drawable-mdpi/ic_action_search.png diff --git a/templates/activities/BlankActivity/root/res/drawable-xhdpi/ic_action_search.png b/templates/activities/BlankActivity/root/res/drawable-xhdpi/ic_action_search.png Binary files differnew file mode 100644 index 0000000..d699c6b --- /dev/null +++ b/templates/activities/BlankActivity/root/res/drawable-xhdpi/ic_action_search.png diff --git a/templates/activities/BlankActivity/root/res/layout/activity_fragment_container.xml b/templates/activities/BlankActivity/root/res/layout/activity_fragment_container.xml new file mode 100644 index 0000000..3128b5f --- /dev/null +++ b/templates/activities/BlankActivity/root/res/layout/activity_fragment_container.xml @@ -0,0 +1,6 @@ +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/container" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".${activityClass}" /> diff --git a/templates/activities/BlankActivity/root/res/layout/activity_pager.xml.ftl b/templates/activities/BlankActivity/root/res/layout/activity_pager.xml.ftl new file mode 100644 index 0000000..c8f1604 --- /dev/null +++ b/templates/activities/BlankActivity/root/res/layout/activity_pager.xml.ftl @@ -0,0 +1,22 @@ +<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/pager" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".${activityClass}"<#if navType != "pager_strip"> /><#else>> + + <!-- + This title strip will display the currently visible page title, as well as the page + titles for adjacent pages. + --> + <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="top" + android:background="#33b5e5" + android:textColor="#fff" + android:paddingTop="4dp" + android:paddingBottom="4dp" /> + +</android.support.v4.view.ViewPager> +</#if> diff --git a/templates/activities/BlankActivity/root/res/layout/activity_simple.xml b/templates/activities/BlankActivity/root/res/layout/activity_simple.xml new file mode 100644 index 0000000..aa34ee3 --- /dev/null +++ b/templates/activities/BlankActivity/root/res/layout/activity_simple.xml @@ -0,0 +1,8 @@ +<TextView 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:gravity="center" + android:text="@string/hello_world" + android:padding="@dimen/padding_medium" + tools:context=".${activityClass}" /> diff --git a/templates/activities/BlankActivity/root/res/menu/main.xml b/templates/activities/BlankActivity/root/res/menu/main.xml new file mode 100644 index 0000000..cfc10fd --- /dev/null +++ b/templates/activities/BlankActivity/root/res/menu/main.xml @@ -0,0 +1,6 @@ +<menu xmlns:android="http://schemas.android.com/apk/res/android"> + <item android:id="@+id/menu_settings" + android:title="@string/menu_settings" + android:orderInCategory="100" + android:showAsAction="never" /> +</menu> diff --git a/templates/activities/BlankActivity/root/res/values-large/dimens.xml b/templates/activities/BlankActivity/root/res/values-large/dimens.xml new file mode 100644 index 0000000..d8cd7c2 --- /dev/null +++ b/templates/activities/BlankActivity/root/res/values-large/dimens.xml @@ -0,0 +1,5 @@ +<resources> + <dimen name="padding_small">8dp</dimen> + <dimen name="padding_medium">16dp</dimen> + <dimen name="padding_large">16dp</dimen> +</resources> diff --git a/templates/activities/BlankActivity/root/res/values/dimens.xml b/templates/activities/BlankActivity/root/res/values/dimens.xml new file mode 100644 index 0000000..d95a70f --- /dev/null +++ b/templates/activities/BlankActivity/root/res/values/dimens.xml @@ -0,0 +1,5 @@ +<resources> + <dimen name="padding_small">8dp</dimen> + <dimen name="padding_medium">8dp</dimen> + <dimen name="padding_large">16dp</dimen> +</resources> diff --git a/templates/activities/BlankActivity/root/res/values/strings.xml.ftl b/templates/activities/BlankActivity/root/res/values/strings.xml.ftl new file mode 100644 index 0000000..753649d --- /dev/null +++ b/templates/activities/BlankActivity/root/res/values/strings.xml.ftl @@ -0,0 +1,13 @@ +<resources> + <string name="activity_name">${appTitle}</string> + + <string name="menu_settings">Settings</string> + + <string name="hello_world">Hello world!</string> + + <#if navType != "none"> + <string name="title_section1">Section 1</string> + <string name="title_section2">Section 2</string> + <string name="title_section3">Section 3</string> + </#if> +</resources> diff --git a/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl new file mode 100644 index 0000000..98c1a8f --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/DropdownActivity.java.ftl @@ -0,0 +1,98 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity implements ActionBar.OnNavigationListener { + + private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setDisplayShowTitleEnabled(false); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + + // Set up the dropdown list navigation in the action bar. + actionBar.setListNavigationCallbacks( + // Specify a SpinnerAdapter to populate the dropdown list. + new ArrayAdapter( + actionBar.getThemedContext(), + android.R.layout.simple_list_item_1, + android.R.id.text1, + new String[]{ + getString(R.string.title_section1), + getString(R.string.title_section2), + getString(R.string.title_section3), + }), + this); + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { + getActionBar().setSelectedNavigationItem( + savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, + getActionBar().getSelectedNavigationIndex()); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + + @Override + public boolean onNavigationItemSelected(int position, long id) { + // When the given tab is selected, show the tab contents in the container + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); + fragment.setArguments(args); + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, fragment) + .commit(); + return true; + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl new file mode 100644 index 0000000..1ebc0fa --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/SimpleActivity.java.ftl @@ -0,0 +1,19 @@ +package ${packageName}; + +import android.os.Bundle; +import android.app.Activity; +import android.view.Menu; + +public class ${activityClass} extends Activity { + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl new file mode 100644 index 0000000..ab11a7f --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/TabsActivity.java.ftl @@ -0,0 +1,94 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity implements ActionBar.TabListener { + + private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + + // For each of the sections in the app, add a tab to the action bar. + actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this)); + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { + getActionBar().setSelectedNavigationItem( + savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, + getActionBar().getSelectedNavigationIndex()); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + + @Override + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + @Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + // When the given tab is selected, show the tab contents in the container + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); + fragment.setArguments(args); + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, fragment) + .commit(); + } + + @Override + public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} diff --git a/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl new file mode 100644 index 0000000..eb47519 --- /dev/null +++ b/templates/activities/BlankActivity/root/src/app_package/TabsAndPagerActivity.java.ftl @@ -0,0 +1,151 @@ +package ${packageName}; + +import android.app.ActionBar; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class ${activityClass} extends FragmentActivity<#if navType?contains("tabs")> implements ActionBar.TabListener</#if> { + + /** + * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the + * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will + * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best + * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. + */ + SectionsPagerAdapter mSectionsPagerAdapter; + + /** + * The {@link ViewPager} that will host the section contents. + */ + ViewPager mViewPager; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.${layoutName}); + + // Create the adapter that will return a fragment for each of the three primary sections + // of the app. + mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + + <#if navType?contains("tabs")> + // Set up the action bar. + final ActionBar actionBar = getActionBar(); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + </#if> + + // Set up the ViewPager with the sections adapter. + mViewPager = (ViewPager) findViewById(R.id.pager); + mViewPager.setAdapter(mSectionsPagerAdapter); + + <#if navType?contains("tabs")> + // When swiping between different sections, select the corresponding tab. + // We can also use ActionBar.Tab#select() to do this if we have a reference to the + // Tab. + mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageSelected(int position) { + actionBar.setSelectedNavigationItem(position); + } + }); + + // For each of the sections in the app, add a tab to the action bar. + for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { + // Create a tab with text corresponding to the page title defined by the adapter. + // Also specify this Activity object, which implements the TabListener interface, as the + // listener for when this tab is selected. + actionBar.addTab( + actionBar.newTab() + .setText(mSectionsPagerAdapter.getPageTitle(i)) + .setTabListener(this)); + } + </#if> + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.${menuName}, menu); + return true; + } + <#if navType?contains("tabs")> + @Override + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + + @Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + // When the given tab is selected, switch to the corresponding page in the ViewPager. + mViewPager.setCurrentItem(tab.getPosition()); + } + + @Override + public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + } + </#if> + + /** + * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary + * sections of the app. + */ + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int i) { + Fragment fragment = new DummySectionFragment(); + Bundle args = new Bundle(); + args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); + fragment.setArguments(args); + return fragment; + } + + @Override + public int getCount() { + return 3; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: return getString(R.string.title_section1).toUpperCase(); + case 1: return getString(R.string.title_section2).toUpperCase(); + case 2: return getString(R.string.title_section3).toUpperCase(); + } + return null; + } + } + + /** + * A dummy fragment representing a section of the app, but that simply displays dummy text. + */ + public static class DummySectionFragment extends Fragment { + public DummySectionFragment() { + } + + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + TextView textView = new TextView(getActivity()); + textView.setGravity(Gravity.CENTER); + Bundle args = getArguments(); + textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); + return textView; + } + } +} |