Android Open Source - NavigationDrawer Main Activity






From Project

Back to project page NavigationDrawer.

License

The source code is released under:

Apache License

If you think the Android project NavigationDrawer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Mukesh Y authors//from   w  ww. ja va  2 s.c  o  m
 *
 * 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.
 */
package com.android.navigationdrawer;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.ActionProvider;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.SubMenu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.android.Adapter.MenuDrawerListAdapter;
import com.android.Config.Config;

/**
 * @author Mukesh Y
 */
public class MainActivity extends SherlockActivity {

  String TAG = "MainActivity";
  private DrawerLayout mDrawerLayout;
  private ListView mDrawerList;
  private ActionBarDrawerToggle mDrawerToggle;
  private String[] menuItemTitles;
  WebView webView;
  ProgressDialog mProgress;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    menuItemTitles = getResources().getStringArray(
        R.array.drawer_menu_itmes_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    webView = (WebView) findViewById(R.id.webview);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
        GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new MenuDrawerListAdapter(this, menuItemTitles,
        Config.drawerMenuItemsIconIds));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ActionBar ab = getSupportActionBar();
    ab.setBackgroundDrawable(getApplicationContext().getResources()
        .getDrawable(R.drawable.bg_titlebar_tile));
    ab.setLogo(getApplicationInfo().loadIcon(getPackageManager()));
    ab.setDisplayShowTitleEnabled(true);
    ab.setTitle("MainActivity");

    ab.setHomeButtonEnabled(true);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, R.string.drawer_open,
        R.string.drawer_close);
    loadDrawer(mDrawerToggle, mDrawerLayout);

    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.androiddevelopersolutions.com");
    mProgress = ProgressDialog.show(MainActivity.this, "Loading", "Please wait for a moment...");
  }

  public class myWebClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
      super.onPageStarted(view, url, favicon);
       
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      view.loadUrl(url);
      return true;

    }
    
    // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
  }

  // To handle "Back" key press event for WebView to go back to previous
  // screen.
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
      webView.goBack();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    if (mDrawerToggle.onOptionsItemSelected(getMenuItem(item))) {
      return true;
    }

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    if (drawerOpen) {
      Log.d(TAG, "" + drawerOpen);
      mDrawerLayout.closeDrawers();
    }
    switch (item.getItemId()) {

    }
    return true;

  }

  protected android.view.MenuItem getMenuItem(final MenuItem item) {
    return new android.view.MenuItem() {
      @Override
      public int getItemId() {
        return item.getItemId();
      }

      public boolean isEnabled() {
        return true;
      }

      @Override
      public boolean collapseActionView() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean expandActionView() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public ActionProvider getActionProvider() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public View getActionView() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public char getAlphabeticShortcut() {
        // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public int getGroupId() {
        // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public Drawable getIcon() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public ContextMenuInfo getMenuInfo() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public char getNumericShortcut() {
        // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public int getOrder() {
        // TODO Auto-generated method stub
        return 0;
      }

      @Override
      public SubMenu getSubMenu() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public CharSequence getTitle() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public CharSequence getTitleCondensed() {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public boolean hasSubMenu() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean isActionViewExpanded() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean isCheckable() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean isChecked() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean isVisible() {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public android.view.MenuItem setActionProvider(
          ActionProvider actionProvider) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setActionView(View view) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setActionView(int resId) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setAlphabeticShortcut(char alphaChar) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setCheckable(boolean checkable) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setChecked(boolean checked) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setEnabled(boolean enabled) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setIcon(Drawable icon) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setIcon(int iconRes) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setIntent(Intent intent) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setNumericShortcut(char numericChar) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setOnActionExpandListener(
          OnActionExpandListener listener) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setOnMenuItemClickListener(
          OnMenuItemClickListener menuItemClickListener) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setShortcut(char numericChar,
          char alphaChar) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public void setShowAsAction(int actionEnum) {
        // TODO Auto-generated method stub

      }

      @Override
      public android.view.MenuItem setShowAsActionFlags(int actionEnum) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setTitle(CharSequence title) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setTitle(int title) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setTitleCondensed(CharSequence title) {
        // TODO Auto-generated method stub
        return null;
      }

      @Override
      public android.view.MenuItem setVisible(boolean visible) {
        // TODO Auto-generated method stub
        return null;
      }
    };
  }

    /* The click listener for ListView in the navigation drawer */
    public class DrawerItemClickListener implements
        ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {
  
        switch (position) {
        case 0: {
          Intent list = new Intent(getApplicationContext(),
              ListActivity.class);
          startActivity(list);
          finish();
          break;
        }
        case 1: {
          Intent main = new Intent(getApplicationContext(),
              MainActivity.class);
          startActivity(main);
          finish();
          break;
        }
        case 2: {
          Intent list = new Intent(getApplicationContext(),
              ListActivity.class);
          startActivity(list);
          finish();
          break;
        }
        
        default:
          break;
        }
      }
    }

  /**
   * When using the ActionBarDrawerToggle, you must call it during
   * onPostCreate() and onConfigurationChanged()...
   */

  @Override
  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);
  }

  public void loadDrawer(ActionBarDrawerToggle mDrawerToggle2,
      DrawerLayout mDrawerLayout2) {
    mDrawerToggle = mDrawerToggle2;
    mDrawerLayout = mDrawerLayout2;
    mDrawerLayout.setDrawerListener(mDrawerToggle);
  }

}




Java Source Code List

android.support.v4.app.Watson.java
com.actionbarsherlock.ActionBarSherlock.java
com.actionbarsherlock.app.ActionBar.java
com.actionbarsherlock.app.SherlockActivity.java
com.actionbarsherlock.app.SherlockDialogFragment.java
com.actionbarsherlock.app.SherlockExpandableListActivity.java
com.actionbarsherlock.app.SherlockFragmentActivity.java
com.actionbarsherlock.app.SherlockFragment.java
com.actionbarsherlock.app.SherlockListActivity.java
com.actionbarsherlock.app.SherlockListFragment.java
com.actionbarsherlock.app.SherlockPreferenceActivity.java
com.actionbarsherlock.internal.ActionBarSherlockCompat.java
com.actionbarsherlock.internal.ActionBarSherlockNative.java
com.actionbarsherlock.internal.ResourcesCompat.java
com.actionbarsherlock.internal.app.ActionBarImpl.java
com.actionbarsherlock.internal.app.ActionBarWrapper.java
com.actionbarsherlock.internal.nineoldandroids.animation.AnimatorListenerAdapter.java
com.actionbarsherlock.internal.nineoldandroids.animation.AnimatorSet.java
com.actionbarsherlock.internal.nineoldandroids.animation.Animator.java
com.actionbarsherlock.internal.nineoldandroids.animation.FloatEvaluator.java
com.actionbarsherlock.internal.nineoldandroids.animation.FloatKeyframeSet.java
com.actionbarsherlock.internal.nineoldandroids.animation.IntEvaluator.java
com.actionbarsherlock.internal.nineoldandroids.animation.IntKeyframeSet.java
com.actionbarsherlock.internal.nineoldandroids.animation.KeyframeSet.java
com.actionbarsherlock.internal.nineoldandroids.animation.Keyframe.java
com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator.java
com.actionbarsherlock.internal.nineoldandroids.animation.PropertyValuesHolder.java
com.actionbarsherlock.internal.nineoldandroids.animation.TypeEvaluator.java
com.actionbarsherlock.internal.nineoldandroids.animation.ValueAnimator.java
com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.java
com.actionbarsherlock.internal.nineoldandroids.view.animation.AnimatorProxy.java
com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.java
com.actionbarsherlock.internal.nineoldandroids.widget.NineHorizontalScrollView.java
com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.java
com.actionbarsherlock.internal.view.ActionProviderWrapper.java
com.actionbarsherlock.internal.view.StandaloneActionMode.java
com.actionbarsherlock.internal.view.View_HasStateListenerSupport.java
com.actionbarsherlock.internal.view.View_OnAttachStateChangeListener.java
com.actionbarsherlock.internal.view.menu.ActionMenuItemView.java
com.actionbarsherlock.internal.view.menu.ActionMenuItem.java
com.actionbarsherlock.internal.view.menu.ActionMenuPresenter.java
com.actionbarsherlock.internal.view.menu.ActionMenuView.java
com.actionbarsherlock.internal.view.menu.ActionMenu.java
com.actionbarsherlock.internal.view.menu.BaseMenuPresenter.java
com.actionbarsherlock.internal.view.menu.ListMenuItemView.java
com.actionbarsherlock.internal.view.menu.MenuBuilder.java
com.actionbarsherlock.internal.view.menu.MenuItemImpl.java
com.actionbarsherlock.internal.view.menu.MenuItemWrapper.java
com.actionbarsherlock.internal.view.menu.MenuPopupHelper.java
com.actionbarsherlock.internal.view.menu.MenuPresenter.java
com.actionbarsherlock.internal.view.menu.MenuView.java
com.actionbarsherlock.internal.view.menu.MenuWrapper.java
com.actionbarsherlock.internal.view.menu.SubMenuBuilder.java
com.actionbarsherlock.internal.view.menu.SubMenuWrapper.java
com.actionbarsherlock.internal.widget.AbsActionBarView.java
com.actionbarsherlock.internal.widget.ActionBarContainer.java
com.actionbarsherlock.internal.widget.ActionBarContextView.java
com.actionbarsherlock.internal.widget.ActionBarView.java
com.actionbarsherlock.internal.widget.CapitalizingButton.java
com.actionbarsherlock.internal.widget.CapitalizingTextView.java
com.actionbarsherlock.internal.widget.CollapsibleActionViewWrapper.java
com.actionbarsherlock.internal.widget.FakeDialogPhoneWindow.java
com.actionbarsherlock.internal.widget.IcsAbsSpinner.java
com.actionbarsherlock.internal.widget.IcsAdapterView.java
com.actionbarsherlock.internal.widget.IcsColorDrawable.java
com.actionbarsherlock.internal.widget.IcsLinearLayout.java
com.actionbarsherlock.internal.widget.IcsListPopupWindow.java
com.actionbarsherlock.internal.widget.IcsProgressBar.java
com.actionbarsherlock.internal.widget.IcsSpinner.java
com.actionbarsherlock.internal.widget.IcsView.java
com.actionbarsherlock.internal.widget.ScrollingTabContainerView.java
com.actionbarsherlock.view.ActionMode.java
com.actionbarsherlock.view.ActionProvider.java
com.actionbarsherlock.view.CollapsibleActionView.java
com.actionbarsherlock.view.MenuInflater.java
com.actionbarsherlock.view.MenuItem.java
com.actionbarsherlock.view.Menu.java
com.actionbarsherlock.view.SubMenu.java
com.actionbarsherlock.view.Window.java
com.actionbarsherlock.widget.ActivityChooserModel.java
com.actionbarsherlock.widget.ActivityChooserView.java
com.actionbarsherlock.widget.SearchView.java
com.actionbarsherlock.widget.ShareActionProvider.java
com.actionbarsherlock.widget.SuggestionsAdapter.java
com.android.Adapter.MenuDrawerListAdapter.java
com.android.Config.Config.java
com.android.navigationdrawer.ListActivity.java
com.android.navigationdrawer.MainActivity.java