Back to project page ProjectStudio.
The source code is released under:
Apache License
If you think the Android project ProjectStudio 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.example.uniutilproject; /*from ww w .ja va 2s. com*/ import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; 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.widget.DrawerLayout; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import adapters.NavDrawerListAdapter; import fragments.CoursesFragment; import fragments.ExamsFragment; import fragments.HomeFragment; import fragments.ProfessorsFragment; import fragments.TasksFragment; import model.NavDrawerItem; public class UniUtil_MainActivity extends FragmentActivity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; // nav drawer title private CharSequence mDrawerTitle; // used to store app title private CharSequence mTitle; // slide menu itemscolorString private String[] navMenuTitles; private TypedArray navMenuIcons; private ArrayList<NavDrawerItem> navDrawerItems; private NavDrawerListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //inflate the view setContentView(R.layout.activity_uni_util__main); //CHANGE THE COLOR OF THE ACTIONBAR TITLE int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); //get the title id TextView yourTextView = (TextView)findViewById(titleId); //assign it to a textview yourTextView.setTextColor(getResources().getColor(R.color.card_white)); //change the color of the textview mTitle = mDrawerTitle = getTitle(); // load slide menu items navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // nav drawer icons from resources navMenuIcons = getResources().obtainTypedArray(R.array.drawer_icons); // mDrawerLayout = new DrawerLayout(this); // mDrawerLayout.setId(0x1000); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.list_slider_menu); // add a footer to the listview /* View footerView = ((LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate( R.layout.drawerlist_footer_view, null, false); mDrawerList.addFooterView(footerView); */ navDrawerItems = new ArrayList<NavDrawerItem>(); // adding nav drawer items to to drawer for (int i = 0; i <= 4; i++) { navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons .getResourceId(i, -1))); } // Recycle the typed array navMenuIcons.recycle(); // set the nav drawer list adapter adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); // Bind the listview to its adapter mDrawerList.setAdapter(adapter); // enable action bar icon to behave as a toggle button getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, // nav menu toggle button R.string.app_name, // nav drawer open - description for // accessibility R.string.app_name // nav drawer close - description for // accessibility ) { @Override public void onDrawerClosed(View drawerView) { //getActionBar().setTitle(mTitle); // calls onPrepareOptionsMenu() to hide action bar icons //invalidateOptionsMenu(); setActionBarArrowDependingOnFragmentsBackStack(); } @Override public void onDrawerOpened(View drawerView) { // getActionBar().setTitle(mDrawerTitle); // calling onPrepareOptionsMenu() to hide action bar icons // invalidateOptionsMenu(); mDrawerToggle.setDrawerIndicatorEnabled(true); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { setActionBarArrowDependingOnFragmentsBackStack(); } }); if (savedInstanceState == null) { // on first time display view of first nav item displayView(0); } mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); //Add fragments to be called at runtime //AddProfFragment pf = new AddProfFragment(); } //METHOD SETS ICON ON ACTIONBAR TO "BACK ICON" TO PREVENT CHILD FRAGMENTS FROM //OVERLAYING PARENT FRAGMENTS private void setActionBarArrowDependingOnFragmentsBackStack() { int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount(); boolean shouldEnableDrawerIndicator = backStackEntryCount == 0; mDrawerToggle.setDrawerIndicatorEnabled(shouldEnableDrawerIndicator); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.uni_util_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // toggle nav drawer on selecting action bar app icon/title /* if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action bar actions click switch (item.getItemId()) { case R.id.action_settings: return true; default: return super.onOptionsItemSelected(item); }*/ if (mDrawerToggle.isDrawerIndicatorEnabled() && mDrawerToggle.onOptionsItemSelected(item)) { return true; } else if (item.getItemId() == android.R.id.home && getSupportFragmentManager().popBackStackImmediate()) { return true; } else { return super.onOptionsItemSelected(item); } } /** * Called when invalidateOptionsMenu is triggered */ @Override public boolean onPrepareOptionsMenu(Menu menu) { // if nav drawer is opened, hide the action items boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_settings).setVisible(!drawerOpen); //disable custom view from child fragment, set actionbar to its original state getActionBar().setDisplayShowHomeEnabled(true); getActionBar().setCustomView(null); getActionBar().setDisplayShowTitleEnabled(true); return super.onPrepareOptionsMenu(menu); } @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); // 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 toggle mDrawerToggle.onConfigurationChanged(newConfig); } /** * Display the fragment view for the selected nav drawer list item */ private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new HomeFragment(); break; case 1: //Was previously StudyPlanFragment //Has now been changed to tasksFragment on 21/01/14 fragment = new TasksFragment(); break; /* Temporarily removed from app case 2: fragment = new MatesFragment(); break; */ case 2: fragment = new ProfessorsFragment(); break; case 3: fragment = new CoursesFragment(); break; case 4: fragment = new ExamsFragment(); break; default: break; } if (fragment != null) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.frame_container, fragment, "fragTag"); ft.commit(); // update the selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); if (navMenuTitles[position].equals("Home")) { setTitle("Uni Util"); } if (position != 0) { setTitle(navMenuTitles[position]); } // mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e("MainActivity", "Error in creating fragment"); } } /** * Sliding menu item click listener */ private class SlideMenuClickListener implements ListView.OnItemClickListener { private final android.os.Handler mDrawerHandler = new android.os.Handler(); @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { //CREATE A HANDLER TO HANDLE SMOOTH DRAWER OPENING AND CLOSING mDrawerHandler.removeCallbacksAndMessages(null); mDrawerHandler.postDelayed(new Runnable() { @Override public void run() { displayView(position); } }, 250); // The millisecond delay is arbitrary and was arrived at through trial and error // display view for selected nav drawer item mDrawerLayout.closeDrawer(mDrawerList); } } }