Java tutorial
// Copyright (c) 2014-2015 Akop Karapetyan // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package org.akop.crosswords.activity; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import org.akop.crosswords.R; import org.akop.crosswords.Storage; import org.akop.crosswords.fragment.FoldersFragment; import org.akop.crosswords.fragment.SelectorFragment; public class MainActivity extends BaseActivity implements FoldersFragment.OnFolderSelectedListener { private static final String FRAGMENT_TAG_FOLDERS = "folders"; private static final String FRAGMENT_TAG_PUZZLES = "puzzles"; private DrawerLayout mDrawerLayout; private Toolbar mToolbar; private DrawerListener mDrawerToggle; private class DrawerListener extends ActionBarDrawerToggle { public DrawerListener() { super(MainActivity.this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); Resources.Theme theme = getTheme(); TypedValue typedValue = new TypedValue(); theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true); mDrawerToggle = new DrawerListener(); mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.setStatusBarBackgroundColor(typedValue.data); if (savedInstanceState == null) { SelectorFragment selectorFragment = new SelectorFragment(); selectorFragment.setArguments(SelectorFragment.createArgs(Storage.FOLDER_INBOX)); FoldersFragment foldersFragment = new FoldersFragment(); foldersFragment.setArguments(FoldersFragment.createArgs(Storage.FOLDER_INBOX)); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().add(R.id.container, selectorFragment, FRAGMENT_TAG_PUZZLES) .add(R.id.folder_container, foldersFragment, FRAGMENT_TAG_FOLDERS).commit(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public void onBackPressed() { if (mDrawerLayout.isDrawerOpen(Gravity.START)) { mDrawerLayout.closeDrawers(); return; } super.onBackPressed(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_subscribe: SubscriptionActivity.launch(this); return true; } return super.onOptionsItemSelected(item); } @Override protected int getContentViewResource() { return R.layout.activity_main; } @Override public void onFolderSelected(long id, String title) { setTitle(title); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG_PUZZLES); if (fragment instanceof FoldersFragment.OnFolderSelectedListener) { FoldersFragment.OnFolderSelectedListener listener = (FoldersFragment.OnFolderSelectedListener) fragment; listener.onFolderSelected(id, title); } if (mDrawerLayout.isDrawerOpen(Gravity.START)) { mDrawerLayout.closeDrawer(Gravity.START); } } }