Back to project page BaseAndroid.
The source code is released under:
MIT License
If you think the Android project BaseAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.cmovil.baseandroid.view; /* w ww . j ava 2 s .c o m*/ 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.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.FrameLayout; import com.cmovil.baseandroid.R; /** * Main class to handle Support Library Navigation Drawer in all application views * Created by Ing. Arturo Ayala on 19/09/13. */ public class BaseDrawerActivity extends BaseActionBarActivity { protected DrawerLayout mDrawerLayout; private FrameLayout mDrawer; private ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawer_activity); mDrawerTitle = getTitle(); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawer = (FrameLayout) findViewById(R.id.left_drawer); mDrawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */) { public void onDrawerClosed(View view) { getBaseActionBar().setTitle(getmTitle()); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getBaseActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); getBaseActionBar().setDisplayHomeAsUpEnabled(true); getBaseActionBar().setHomeButtonEnabled(true); } protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle your other action bar items... return super.onOptionsItemSelected(item); } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view //boolean drawerOpen = mDrawerLayout.isDrawerOpen(GravityCompat.END); //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } /** * It is necessary to call this method from the class that will use the navigation drawer, so that the * left drawer is substituted by a fragment representing the custom app menu * @param drawer * Fragment that represents the drawer menu */ protected void setLeftDrawer(Fragment drawer){ FragmentTransaction t = this.getSupportFragmentManager().beginTransaction(); t.replace(R.id.left_drawer, drawer); t.commit(); } public FrameLayout getmDrawer() { return mDrawer; } }