Java tutorial
/******************************************************************************* * Copyright (c) 2012 Andrew Hanes * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ package edu.csh.coursebrowser; import android.app.ActionBar; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; public class AboutActivity extends FragmentActivity implements ActionBar.TabListener { protected static boolean lookedAtLicense = false; /** * The serialization (saved instance state) Bundle key representing the * current tab position. */ private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_about); // Set up the action bar to show tabs. 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("About").setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("License").setTabListener(this)); this.setTitle("About"); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { // Restore the previously serialized current tab position. if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); } } @Override public void onSaveInstanceState(Bundle outState) { // Serialize the current tab position. outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_about, menu); return true; } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, show the tab contents in the // container view. 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 onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @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 { /** * The fragment argument representing the section number for this * fragment. */ public static final String ARG_SECTION_NUMBER = "section_number"; public DummySectionFragment() { } @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { // Create a new TextView and set its text to the fragment's section // number argument value. int num = getArguments().getInt(ARG_SECTION_NUMBER); switch (num) { case 1: ScrollView l = new ScrollView(container.getContext()); LinearLayout a = new LinearLayout(container.getContext()); a.setPadding(10, 10, 10, 10); TextView t = new TextView(container.getContext()); t.setText("Course Browser\n\nAuthor: Andrew Hanes " + "<ahanes AT csh.rit.edu>\n\nProject is located " + "at http://www.github" + ".com/AndrewHanes/CourseBrowser\n\n" + "" + "Uses iconic icon set by P.J. Onori, " + "licensed under CC BY-SA 3.0\n" + "\thttp://somerandomdude.com/work/iconic/\n" + "\thttp://creativecommons.org/licenses/by-sa/3" + ".0/us/\n\nUses APIs provided by CSH ScheduleMaker" + " by Ben Russell (benrr101 at csh.rit.edu)" + ".edu\n\nWARNING: Usage of this application may " + "result in data charges. These charges are YOUR " + "responsibility!\n\nSee the " + '"' + "License" + '"' + " tab for license information\n\nHit the " + "back button on your device to start the " + "application."); a.addView(t); l.addView(a); return l; case 2: // License AboutActivity.lookedAtLicense = true; ScrollView s = new ScrollView(container.getContext()); TextView tv = new TextView(container.getContext()); tv.setPadding(10, 10, 10, 10); String str = "Copyright (c) 2012 Andrew " + "Hanes\n\nPermission" + " " + "is hereby granted, free of charge, " + "to any person obtaining a copy of this software " + "and associated documentation files (the " + '"' + "Software" + '"' + "), to deal in the Software " + "without restriction, " + "including without limitation the rights to use, " + "copy, modify, merge, publish, distribute, " + "sublicense, and/or sell copies of the Software, " + "and to permit persons to whom the Software is " + "furnished to do so, subject to the following " + "conditions:\n\nThe above copyright notice and " + "this" + " permission notice shall be included in all " + "copies or substantial portions of the Software" + ".\n\nTHE SOFTWARE IS PROVIDED " + '"' + "AS IS" + '"' + ", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED," + " INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, " + "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " + "SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, " + "DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, " + "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE" + " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE " + "SOFTWARE."; tv.setText(str); s.addView(tv); return s; default: return null; } } } }