Java tutorial
/* * Copyright 2015. Emin Yahyayev * * 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 igor70334.mymovies.ui.activity; import android.app.SearchManager; import android.content.Context; import android.support.annotation.CallSuper; import android.support.annotation.Nullable; import android.support.v4.view.ViewCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import butterknife.BindView; import butterknife.ButterKnife; import igor70334.mymovies.R; /** * Base class for all activities. * Binds views * * @see ButterKnife */ public abstract class BaseActivity extends AppCompatActivity { private static final String LOG_TAG = BaseActivity.class.getSimpleName(); @Nullable @BindView(R.id.toolbar) Toolbar mToolbar; @CallSuper @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); ButterKnife.bind(this); setupToolbar(); } @Nullable public final Toolbar getToolbar() { return mToolbar; } private void setupToolbar() { if (mToolbar == null) { Log.w(LOG_TAG, "Didn't find a toolbar"); return; } ViewCompat.setElevation(mToolbar, getResources().getDimension(R.dimen.toolbar_elevation)); setSupportActionBar(mToolbar); ActionBar actionBar = getSupportActionBar(); if (actionBar == null) return; actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setHomeButtonEnabled(false); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView; MenuItem item = menu.findItem(R.id.search); searchView = (SearchView) item.getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setQueryHint(getResources().getString(R.string.search_hint)); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } }