Java tutorial
/* * Copyright (C) 2015 Maros Cavojsky, (mpage.sk) * * 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 sk.mpage.androidsample.searchviewtoolbar; import android.accounts.Account; import android.accounts.AccountManager; import android.app.SearchManager; import android.content.Context; import android.content.res.Configuration; 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.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; 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 java.util.List; import sk.mpage.androidsample.searchviewtoolbar.drawer.CustomAdapter; import sk.mpage.androidsample.searchviewtoolbar.drawer.MenuItemObject; import sk.mpage.androidsample.searchviewtoolbar.fragments.GeneralFragment; public class MainActivity extends AppCompatActivity { public static final String[] titles = { "Profile", "News", "Friends", "About" }; int[] titleIcons = { R.drawable.ic_account, R.drawable.ic_bullhorn, R.drawable.ic_account_multiple, R.drawable.ic_email }; public static final int NEWS_FRAGMENT = 1; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private CharSequence mTitle; private CharSequence mDrawerTitle; private ActionBarDrawerToggle mDrawerToggle; private Toolbar topToolBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); topToolBar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(topToolBar); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); LayoutInflater inflater = getLayoutInflater(); View listHeaderView = inflater.inflate(R.layout.header_list, null, false); mDrawerList.addHeaderView(listHeaderView); List<MenuItemObject> listViewItems = new ArrayList<MenuItemObject>(); for (int i = 1; i < titles.length; i++) { listViewItems.add(new MenuItemObject(titles[i], titleIcons[i])); } mDrawerList.setAdapter(new CustomAdapter(this, listViewItems)); mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { /** * Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); getSupportActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** * Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getSupportActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.setDrawerIndicatorEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // make Toast when click selectItemFragment(position); } }); if (savedInstanceState == null) { selectItemFragment(NEWS_FRAGMENT); } } private void selectItemFragment(int position) { Fragment fragment = null; Bundle args = new Bundle(); switch (position) { default: fragment = new GeneralFragment(); break; } fragment.setArguments(args); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit(); mDrawerList.setItemChecked(position, true); setTitle(titles[position]); mDrawerLayout.closeDrawer(mDrawerList); } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } @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); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } }