Java tutorial
/* * Copyright (C) 2014 Pranay Airan. * * 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.dexterapps.codelessdomore; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.ActivityCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class BaseActivity extends ActionBarActivity { //protected ShareActionProvider mShareActionProvider; protected DrawerLayout mDrawerLayout; protected ListView mDrawerList; private CharSequence mTitle; private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void populateNavigationDrawer(String title) { mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.drawer); mTitle = title; // 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 ArrayAdapter<String>(this, R.layout.drawer_list_item, NavigationList.navigationOptionsArray)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon 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) { getSupportActionBar().setTitle(mTitle); ActivityCompat.invalidateOptionsMenu(BaseActivity.this); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle("Movie Ratings"); ActivityCompat.invalidateOptionsMenu(BaseActivity.this); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { selectItem(position); } } /** Swaps fragments in the main content view */ protected void selectItem(int position) { Intent i = new Intent(BaseActivity.this, NavigationList.navigationOptions.get(NavigationList.navigationOptionsArray.get(position))); //If home is clicked clear the top stack if (position == 0) i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); //overridePendingTransition(R.anim.fade_in, R.anim.fade_out); mDrawerLayout.closeDrawer(mDrawerList); } public void highlightitem(int position) { // Highlight the selected item, update the title, and close the drawer mDrawerList.setItemChecked(position, true); setTitle(NavigationList.navigationOptionsArray.get(position)); } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(title); } protected Intent getDefaultIntent() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); return intent; } @Override public boolean onOptionsItemSelected(MenuItem item) { // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle presses on the action bar items switch (item.getItemId()) { // case R.id.action_search: // // return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } }