Java tutorial
/* Copyright (C) 2013, 2014 EP Studios, Inc. www.epstudiossoftware.com This file is part of EP Coding. EP Coding is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. EP Coding is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with EP Coding. If not, see <http://www.gnu.org/licenses/>. Note also: CPT copyright 2012 American Medical Association. All rights reserved. CPT is a registered trademark of the American Medical Association. A limited number of CPT codes are used in this program under the Fair Use doctrine of US Copyright Law. See README.md for more information. */ package org.epstudios.epcoding; import android.app.Activity; import android.app.SearchManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; /** * An activity representing a list of Procedures. This activity has different * presentations for handset and tablet-size devices. On handsets, the activity * presents a list of items, which when touched, lead to a * {@link ProcedureDetailActivity} representing item details. On tablets, the * activity presents the list of items and item details side-by-side using two * vertical panes. * <p> * The activity makes heavy use of fragments. The list of items is a * {@link ProcedureListFragment} and the item details (if present) is a * {@link ProcedureDetailFragment}. * <p> * This activity also implements the required * {@link ProcedureListFragment.Callbacks} interface to listen for item * selections. */ public class ProcedureListActivity extends ActionBarActivity implements ProcedureListFragment.Callbacks { /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. */ private boolean mTwoPane; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_procedure_list); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (findViewById(R.id.procedure_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. ((ProcedureListFragment) getSupportFragmentManager().findFragmentById(R.id.procedure_list)) .setActivateOnItemClick(true); } } /** * Callback method from {@link ProcedureListFragment.Callbacks} indicating * that the item with the given ID was selected. */ @Override public void onItemSelected(String id) { 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(ProcedureDetailFragment.ARG_ITEM_ID, id); ProcedureDetailFragment fragment = new ProcedureDetailFragment(); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction().replace(R.id.procedure_detail_container, fragment) .commit(); } else { // In single-pane mode, simply start the detail activity // for the selected item ID. Intent detailIntent = new Intent(this, ProcedureDetailActivity.class); detailIntent.putExtra(ProcedureDetailFragment.ARG_ITEM_ID, id); startActivity(detailIntent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.shortmenu, menu); // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); Log.d("EPCODING", getComponentName().toString()); searchView.setIconifiedByDefault(false); // Do not iconify the widget; // expand it by default return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { ProcedureDetailFragment fragment = (ProcedureDetailFragment) getSupportFragmentManager() .findFragmentById(R.id.procedure_detail_container); switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; case R.id.help: startActivity(new Intent(this, Help.class)); return true; case R.id.saveCodeSelection: if (fragment != null) fragment.saveCoding(); else saveCodeSelectionErrorMessage(); return true; case R.id.settings: startActivity(new Intent(this, Prefs.class)); return true; case R.id.wizard: startActivity(new Intent(this, ScreenSlideActivity.class)); return true; } return super.onOptionsItemSelected(item); } private void saveCodeSelectionErrorMessage() { Activity activity = this; Toast toast = Toast.makeText(this, activity.getString(R.string.save_code_selection_error_message), Toast.LENGTH_SHORT); toast.show(); } }