Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bablu.zoo;
- import android.content.res.Configuration;
- import android.os.Bundle;
- import android.os.PersistableBundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- public class MainActivity extends AppCompatActivity {
- private DrawerLayout mDrawerLayout;
- private ActionBarDrawerToggle mActionBarDrawerToggle;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- // For navigation bar
- if (getSupportActionBar() != null) {
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- }
- getSupportActionBar().setHomeButtonEnabled(true);
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- mActionBarDrawerToggle = 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);
- mActionBarDrawerToggle.syncState();
- }
- @Override
- public void onDrawerClosed(View drawerView) {
- super.onDrawerClosed(drawerView);
- if(getSupportActionBar() != null)
- getSupportActionBar().setTitle(R.string.drawer_closed);
- mActionBarDrawerToggle.syncState();
- }
- };
- mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
- }
- @Override
- public void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- mActionBarDrawerToggle.syncState();
- //Call syncState() from your activity's onPostCreate to set the state of the indicator
- // based on whether the drawerlayout is in open or closed state once the activity
- // has been restored with onRestoreInstanceState.
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- mActionBarDrawerToggle.onConfigurationChanged(newConfig);
- //When the device is rotated the method 'onConfigurationChanged(newConfig' knows when the drawer is open or close.
- }
- @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(mActionBarDrawerToggle.onOptionsItemSelected(item))
- return true;
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.example.bablu.zoo.MainActivity"
- tools:showIn="@layout/activity_main">
- <android.support.v4.widget.DrawerLayout
- android:id="@+id/drawer_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello world"/>
- <ListView android:id="@+id/left_drawer"
- android:layout_width="240dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:choiceMode="singleChoice"
- android:divider="@android:color/transparent"
- android:dividerHeight="0dp"
- android:background="#111"/>
- </android.support.v4.widget.DrawerLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:context="com.example.bablu.zoo.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- </android.support.design.widget.CoordinatorLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement