android_learning:headfirst_android_development_notes:chapter_10
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
android_learning:headfirst_android_development_notes:chapter_10 [2016/03/16 03:38] – [Foreword] mithat | android_learning:headfirst_android_development_notes:chapter_10 [2016/03/16 04:49] (current) – [Foreword] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Chapter 10 ====== | ====== Chapter 10 ====== | ||
- | |||
- | <WRAP center round info 60%> | ||
- | Under development. | ||
- | </ | ||
===== Foreword ===== | ===== Foreword ===== | ||
- | I think Android' | + | I think Android' |
- | If you get frustrated in this chapter, I feel your pain. It's not you, it's Android. If there' | + | If you get frustrated in this chapter, I feel your pain. It's not you, it's Android. If there' |
===== p. 398: Creating the app ===== | ===== p. 398: Creating the app ===== | ||
Line 31: | Line 27: | ||
===== p 401: Project dependencies ===== | ===== p 401: Project dependencies ===== | ||
- | In the preceding chapter, you were instructed to remove the project' | + | In the preceding chapter, you were instructed to remove the project' |
{{ https:// | {{ https:// | ||
- | <WRAP center round important 90%> | + | So, leave '' |
- | Leave '' | + | |
- | </ | + | |
- | + | ||
- | A dependence on '' | + | |
===== p 401: Code blocks ===== | ===== p 401: Code blocks ===== | ||
Line 75: | Line 67: | ||
</ | </ | ||
- | Notice the required empty public constructor not included in the book's code. | + | Notice the required empty public constructor not included in the book's code. The [[http:// |
- | <WRAP center round info 90%> | + | > All subclasses of Fragment must include a public no-argument constructor. |
- | The [[http:// | + | |
- | </ | + | |
+ | The book doesn' | ||
And here's the layout: | And here's the layout: | ||
Line 232: | Line 223: | ||
===== p. 417: Anonymous classes ===== | ===== p. 417: Anonymous classes ===== | ||
- | In case you're not familiar with the construct of the '' | + | In case you're not familiar with how the '' |
===== p. 422: Code block ===== | ===== p. 422: Code block ===== | ||
Line 420: | Line 411: | ||
</ | </ | ||
+ | ===== pp. 429-434 ===== | ||
+ | I think the length of the ActivityMain class definition is an indication of how poorly designed the Android features introduced in this and the previous chapter are. But you be the judge. | ||
+ | Note when you add code that references the FragmentManager class to your code, you'll be offered two different class options to import. Use '' | ||
+ | |||
+ | <file java ActivityMain.java> | ||
+ | package com.hfad.bitsandpizzas; | ||
+ | |||
+ | import android.app.Activity; | ||
+ | import android.app.Fragment; | ||
+ | import android.app.FragmentManager; | ||
+ | import android.app.FragmentTransaction; | ||
+ | import android.content.Intent; | ||
+ | import android.content.res.Configuration; | ||
+ | import android.os.Bundle; | ||
+ | import android.support.v4.widget.DrawerLayout; | ||
+ | import android.support.v7.app.ActionBarDrawerToggle; | ||
+ | import android.view.Menu; | ||
+ | import android.view.MenuItem; | ||
+ | import android.view.View; | ||
+ | import android.widget.AdapterView; | ||
+ | import android.widget.ArrayAdapter; | ||
+ | import android.widget.ListView; | ||
+ | import android.widget.ShareActionProvider; | ||
+ | import android.widget.Toast; | ||
+ | |||
+ | public class MainActivity extends Activity { | ||
+ | |||
+ | private ShareActionProvider shareActionProvider; | ||
+ | private String[] titles; | ||
+ | private ListView drawerList; | ||
+ | private DrawerLayout drawerLayout; | ||
+ | private ActionBarDrawerToggle drawerToggle; | ||
+ | private int currentPosition = 0; | ||
+ | |||
+ | private class DrawerItemClickListener implements ListView.OnItemClickListener { | ||
+ | |||
+ | @Override | ||
+ | public void onItemClick(AdapterView<?> | ||
+ | selectItem(position); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | |||
+ | // populate local list of categories | ||
+ | titles = getResources().getStringArray(R.array.titles); | ||
+ | drawerList = (ListView) findViewById(R.id.drawer); | ||
+ | drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | ||
+ | |||
+ | // Populate the list view | ||
+ | drawerList.setAdapter(new ArrayAdapter< | ||
+ | android.R.layout.simple_list_item_activated_1, | ||
+ | drawerList.setOnItemClickListener(new DrawerItemClickListener()); | ||
+ | |||
+ | // Display the correct fragment | ||
+ | if (savedInstanceState != null) { | ||
+ | currentPosition = savedInstanceState.getInt(" | ||
+ | setActionBarTitle(currentPosition); | ||
+ | } else { // if MainActivity newly created ... | ||
+ | selectItem(0); | ||
+ | } | ||
+ | |||
+ | // Create the ActionBarToggle ... | ||
+ | drawerToggle = new ActionBarDrawerToggle(this, | ||
+ | R.string.open_drawer, | ||
+ | |||
+ | @Override | ||
+ | public void onDrawerClosed(View drawerView) { | ||
+ | super.onDrawerClosed(drawerView); | ||
+ | invalidateOptionsMenu(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onDrawerOpened(View drawerView) { | ||
+ | super.onDrawerOpened(drawerView); | ||
+ | invalidateOptionsMenu(); | ||
+ | } | ||
+ | }; | ||
+ | // ... and set the ActionBarToggle as the drawer' | ||
+ | drawerLayout.setDrawerListener(drawerToggle); | ||
+ | |||
+ | getActionBar().setDisplayHomeAsUpEnabled(true); | ||
+ | getActionBar().setHomeButtonEnabled(true); | ||
+ | |||
+ | // Add a listener for changes in the backstack to the fragment manager, | ||
+ | // use that to set titles appropriately and check drawer items. | ||
+ | getFragmentManager().addOnBackStackChangedListener( | ||
+ | new FragmentManager.OnBackStackChangedListener() { | ||
+ | |||
+ | @Override | ||
+ | public void onBackStackChanged() { | ||
+ | FragmentManager fragMan = getFragmentManager(); | ||
+ | Fragment fragment = fragMan.findFragmentByTag(" | ||
+ | |||
+ | if (fragment instanceof TopFragment) { | ||
+ | currentPosition = 0; | ||
+ | } | ||
+ | if (fragment instanceof PizzaFragment) { | ||
+ | currentPosition = 1; | ||
+ | } | ||
+ | if (fragment instanceof PastaFragment) { | ||
+ | currentPosition = 2; | ||
+ | } | ||
+ | if (fragment instanceof StoresFragment) { | ||
+ | currentPosition = 3; | ||
+ | } | ||
+ | setActionBarTitle(currentPosition); | ||
+ | drawerList.setItemChecked(currentPosition, | ||
+ | } | ||
+ | |||
+ | } | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onSaveInstanceState(Bundle outState) { | ||
+ | super.onSaveInstanceState(outState); | ||
+ | outState.putInt(" | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onPostCreate(Bundle savedInstanceState) { | ||
+ | super.onPostCreate(savedInstanceState); | ||
+ | // sync toggle state (after onRestoreInstanceState has occurred). | ||
+ | drawerToggle.syncState(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onConfigurationChanged(Configuration newConfig) { | ||
+ | super.onConfigurationChanged(newConfig); | ||
+ | drawerToggle.onConfigurationChanged(newConfig); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onPrepareOptionsMenu(Menu menu) { | ||
+ | // If the drawer is open, hide action items related to the content view. | ||
+ | boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList); | ||
+ | menu.findItem(R.id.action_share).setVisible(!drawerOpen); | ||
+ | return super.onPrepareOptionsMenu(menu); | ||
+ | } | ||
+ | |||
+ | private void selectItem(int position) { | ||
+ | currentPosition = position; | ||
+ | // update the main content by replacing fragments | ||
+ | Fragment fragment; | ||
+ | switch (position) { | ||
+ | case 1: | ||
+ | fragment = new PizzaFragment(); | ||
+ | break; | ||
+ | case 2: | ||
+ | fragment = new PastaFragment(); | ||
+ | break; | ||
+ | case 3: | ||
+ | fragment = new StoresFragment(); | ||
+ | break; | ||
+ | default: | ||
+ | fragment = new TopFragment(); | ||
+ | } | ||
+ | FragmentTransaction ft = getFragmentManager().beginTransaction(); | ||
+ | ft.replace(R.id.content_frame, | ||
+ | .addToBackStack(null) | ||
+ | .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) | ||
+ | .commit(); | ||
+ | |||
+ | //Set the action bar title | ||
+ | setActionBarTitle(position); | ||
+ | |||
+ | //Close drawer | ||
+ | drawerLayout.closeDrawer(drawerList); | ||
+ | } | ||
+ | |||
+ | private void setActionBarTitle(int position) { | ||
+ | String title; | ||
+ | |||
+ | if (position == 0) { | ||
+ | title = getResources().getString(R.string.app_name); | ||
+ | } else { | ||
+ | title = titles[position]; | ||
+ | } | ||
+ | getActionBar().setTitle(title); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onCreateOptionsMenu(Menu menu) { | ||
+ | // Add menu_main items to the action bar (if present): | ||
+ | getMenuInflater().inflate(R.menu.menu_main, | ||
+ | MenuItem menuItem = menu.findItem(R.id.action_share); | ||
+ | shareActionProvider = (ShareActionProvider) menuItem.getActionProvider(); | ||
+ | setIntent(" | ||
+ | return super.onCreateOptionsMenu(menu); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean onOptionsItemSelected(MenuItem item) { | ||
+ | // If a drawer option has been clicked ... | ||
+ | if (drawerToggle.onOptionsItemSelected(item)) { | ||
+ | // do nothing and return. | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | // ... otherwise ... | ||
+ | switch (item.getItemId()) { | ||
+ | case R.id.action_create_order: | ||
+ | //Code to run when the Create Order item is clicked | ||
+ | Intent intent = new Intent(this, | ||
+ | startActivity(intent); | ||
+ | return true; | ||
+ | case R.id.action_settings: | ||
+ | //Code to run when the settings item is clicked | ||
+ | Toast.makeText(MainActivity.this, | ||
+ | return true; | ||
+ | default: | ||
+ | return super.onOptionsItemSelected(item); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void setIntent(String text) { | ||
+ | Intent intent = new Intent(Intent.ACTION_SEND); | ||
+ | intent.setType(" | ||
+ | intent.putExtra(Intent.EXTRA_TEXT, | ||
+ | shareActionProvider.setShareIntent(intent); | ||
+ | } | ||
+ | } | ||
+ | </ |
android_learning/headfirst_android_development_notes/chapter_10.1458099497.txt.gz · Last modified: 2016/03/16 03:38 by mithat