package com.florianmski.tracktoid.ui.activities; import android.app.ActionBar; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.florianmski.tracktoid.R; import com.florianmski.tracktoid.services.sync.SyncAdapter; import com.florianmski.tracktoid.ui.fragments.DrawerTraktFragment; public class HomeActivity extends TranslucentActivity { private DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerToggle; private CharSequence drawerTitle = "Menu"; private CharSequence title; @Override protected int getContentViewId() { return R.layout.activity_home; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the drawer fragment if(savedInstanceState == null) { getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_drawer, DrawerTraktFragment.newInstance()) .commit(); } drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // set a custom shadow that overlays the main content when the drawer opens drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // enable ActionBar home button to behave as action to toggle nav drawer getSupportActionBar().setIcon(null); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon drawerToggle = new ActionBarDrawerToggle( this, drawerLayout, (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar), R.string.drawer_open, R.string.drawer_close ) { public void onDrawerClosed(View view) { invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; drawerLayout.setDrawerListener(drawerToggle); // trigger a sync SyncAdapter.requestImmediateSync(); } @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view if(drawerLayout != null) { boolean drawerOpen = drawerLayout.isDrawerOpen(Gravity.START); for(int i = 0; i < menu.size(); i++) menu.getItem(i).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. if (drawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { default: return super.onOptionsItemSelected(item); } } @Override public void setTitle(CharSequence title) { this.title = title; getSupportActionBar().setTitle(this.title); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls drawerToggle.onConfigurationChanged(newConfig); } public static void launch(Activity a) { launchActivity(a, HomeActivity.class); } }