Java tutorial
package pwr.itapps.meetme.activity; /* * Copyright 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.List; import pwr.itapps.meetme.R; import pwr.itapps.meetme.fragments.EventListFragment; import pwr.itapps.meetme.fragments.FriendsFragment; import pwr.itapps.meetme.fragments.MapsFragment; import pwr.itapps.meetme.fragments.SettingsFragment; import android.app.SearchManager; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class WallActivity extends FragmentActivity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mMenuOptions; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); setProgressBarIndeterminateVisibility(false); mTitle = mDrawerTitle = getTitle(); mMenuOptions = getResources().getStringArray(R.array.menu_options); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // set a custom shadow that overlays the main content when the drawer // opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mMenuOptions)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); getActionBar().setTitle(null); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { selectItem(0); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content // view boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); menu.findItem(R.id.action_createEvent).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. Intent intent; if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch (item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; case R.id.services_download: Fragment f = getActiveFragment(); if (f instanceof FriendsFragment) { ((FriendsFragment) f).downloadContacs(); } else if (f instanceof EventListFragment) { ((EventListFragment) f).runRefresh(); } return true; case R.id.action_createEvent: // create intent to perform web search for this planet intent = new Intent(getApplicationContext(), CreateEventActivity.class); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } } public Fragment getActiveFragment() { FragmentManager fragmentManager = this.getSupportFragmentManager(); List<Fragment> fragments = fragmentManager.getFragments(); for (Fragment fragment : fragments) { if (fragment.isVisible()) return fragment; } return null; } /* The click listner for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { Fragment fragment = null; FragmentManager fragmentManager = getSupportFragmentManager(); switch (position) { case 0: fragment = new EventListFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; case 1: // TODO map fragment FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction(); android.support.v4.app.Fragment mapFragment = new MapsFragment(); mTransaction.replace(R.id.content_frame, mapFragment); mTransaction.commit(); // try { // MapsInitializer.initialize(this); // } catch (GooglePlayServicesNotAvailableException e) { // e.printStackTrace(); // } break; case 2: fragment = new EventListFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; case 3: fragment = new EventListFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; case 4: fragment = new EventListFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; case 5: fragment = new FriendsFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; case 6: fragment = new SettingsFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); break; default: fragment = new EventListFragment(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); } // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); setTitle(mMenuOptions[position]); mDrawerLayout.closeDrawer(mDrawerList); } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(mTitle); } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } /** * Fragment that appears in the "content_frame", shows a planet */ // public static class PlanetFragment extends Fragment { // public static final String ARG_PLANET_NUMBER = "planet_number"; // // public PlanetFragment() { // // Empty constructor required for fragment subclasses // } // // @Override // public View onCreateView(LayoutInflater inflater, ViewGroup container, // Bundle savedInstanceState) { // View rootView = inflater.inflate(R.layout.fragment_planet, // container, false); // int i = getArguments().getInt(ARG_PLANET_NUMBER); // String planet = getResources().getStringArray(R.array.menu_options)[i]; // // int imageId = getResources().getIdentifier( // planet.toLowerCase(Locale.getDefault()), "drawable", // getActivity().getPackageName()); // ((ImageView) rootView.findViewById(R.id.image)) // .setImageResource(imageId); // getActivity().setTitle(planet); // return rootView; // } // } }