Advertisement
yoesuv

MainActivity

Oct 3rd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package yoesuv.drawerlayout;
  2.  
  3. import android.content.res.Configuration;
  4. import android.support.v4.widget.DrawerLayout;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.os.Bundle;
  7. import android.support.v7.app.ActionBarDrawerToggle;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12.  
  13. public class MainActivity extends ActionBarActivity {
  14.  
  15.     private DrawerLayout mDrawerLayout;
  16.     private ActionBarDrawerToggle actionBarDrawerToggle;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         Toolbar toolbar = (Toolbar) findViewById(R.id.customToolbar);
  24.         setSupportActionBar(toolbar);
  25.  
  26.         getSupportActionBar().setDisplayShowHomeEnabled(true);
  27.         getSupportActionBar().setHomeButtonEnabled(true);
  28.  
  29.         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
  30.         actionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_opened,R.string.drawer_closed){
  31.             @Override
  32.             public void onDrawerOpened(View drawerView) {
  33.                 super.onDrawerOpened(drawerView);
  34.                 if(getSupportActionBar()!=null) {
  35.                     getSupportActionBar().setTitle(R.string.drawer_opened);
  36.                 }
  37.             }
  38.  
  39.             @Override
  40.             public void onDrawerClosed(View drawerView) {
  41.                 super.onDrawerClosed(drawerView);
  42.                 if(getSupportActionBar()!=null){
  43.                     getSupportActionBar().setTitle(R.string.drawer_closed);
  44.                 }
  45.             }
  46.         };
  47.         mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
  48.     }
  49.  
  50.     @Override
  51.     protected void onPostCreate(Bundle savedInstanceState) {
  52.         super.onPostCreate(savedInstanceState);
  53.         actionBarDrawerToggle.syncState();
  54.     }
  55.  
  56.     @Override
  57.     public void onConfigurationChanged(Configuration newConfig) {
  58.         super.onConfigurationChanged(newConfig);
  59.         actionBarDrawerToggle.onConfigurationChanged(newConfig);
  60.     }
  61.  
  62.     @Override
  63.     public boolean onCreateOptionsMenu(Menu menu) {
  64.         // Inflate the menu; this adds items to the action bar if it is present.
  65.         getMenuInflater().inflate(R.menu.menu_main, menu);
  66.         return true;
  67.     }
  68.  
  69.     @Override
  70.     public boolean onOptionsItemSelected(MenuItem item) {
  71.         // Handle action bar item clicks here. The action bar will
  72.         // automatically handle clicks on the Home/Up button, so long
  73.         // as you specify a parent activity in AndroidManifest.xml.
  74.  
  75.         if(actionBarDrawerToggle.onOptionsItemSelected(item)){
  76.             return true;
  77.         }
  78.  
  79.         int id = item.getItemId();
  80.  
  81.         //noinspection SimplifiableIfStatement
  82.         if (id == R.id.action_settings) {
  83.             return true;
  84.         }
  85.  
  86.         return super.onOptionsItemSelected(item);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement