Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package yoesuv.drawerlayout;
- import android.content.res.Configuration;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- public class MainActivity extends ActionBarActivity {
- private DrawerLayout mDrawerLayout;
- private ActionBarDrawerToggle actionBarDrawerToggle;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.customToolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- getSupportActionBar().setHomeButtonEnabled(true);
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
- actionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_opened,R.string.drawer_closed){
- @Override
- public void onDrawerOpened(View drawerView) {
- super.onDrawerOpened(drawerView);
- if(getSupportActionBar()!=null) {
- getSupportActionBar().setTitle(R.string.drawer_opened);
- }
- }
- @Override
- public void onDrawerClosed(View drawerView) {
- super.onDrawerClosed(drawerView);
- if(getSupportActionBar()!=null){
- getSupportActionBar().setTitle(R.string.drawer_closed);
- }
- }
- };
- mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- actionBarDrawerToggle.syncState();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- actionBarDrawerToggle.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);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- if(actionBarDrawerToggle.onOptionsItemSelected(item)){
- return true;
- }
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement