Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package com.example.bablu.zoo;
  2.  
  3. import android.content.res.Configuration;
  4. import android.os.Bundle;
  5. import android.os.PersistableBundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v4.widget.DrawerLayout;
  9. import android.support.v7.app.ActionBarDrawerToggle;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.support.v7.widget.Toolbar;
  12. import android.view.View;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. private DrawerLayout mDrawerLayout;
  19. private ActionBarDrawerToggle mActionBarDrawerToggle;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  25. setSupportActionBar(toolbar);
  26.  
  27. // For navigation bar
  28. if (getSupportActionBar() != null) {
  29. getSupportActionBar().setDisplayShowHomeEnabled(true);
  30. }
  31. getSupportActionBar().setHomeButtonEnabled(true);
  32.  
  33. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  34. mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed){
  35. @Override
  36. public void onDrawerOpened(View drawerView) {
  37. super.onDrawerOpened(drawerView);
  38. if(getSupportActionBar() != null)
  39. getSupportActionBar().setTitle(R.string.drawer_opened);
  40. mActionBarDrawerToggle.syncState();
  41. }
  42.  
  43. @Override
  44. public void onDrawerClosed(View drawerView) {
  45. super.onDrawerClosed(drawerView);
  46. if(getSupportActionBar() != null)
  47. getSupportActionBar().setTitle(R.string.drawer_closed);
  48. mActionBarDrawerToggle.syncState();
  49.  
  50. }
  51. };
  52.  
  53. mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
  54. }
  55.  
  56. @Override
  57. public void onPostCreate(Bundle savedInstanceState) {
  58. super.onPostCreate(savedInstanceState);
  59. mActionBarDrawerToggle.syncState();
  60. //Call syncState() from your activity's onPostCreate to set the state of the indicator
  61. // based on whether the drawerlayout is in open or closed state once the activity
  62. // has been restored with onRestoreInstanceState.
  63. }
  64.  
  65. @Override
  66. public void onConfigurationChanged(Configuration newConfig) {
  67. super.onConfigurationChanged(newConfig);
  68. mActionBarDrawerToggle.onConfigurationChanged(newConfig);
  69. //When the device is rotated the method 'onConfigurationChanged(newConfig' knows when the drawer is open or close.
  70. }
  71.  
  72. @Override
  73. public boolean onCreateOptionsMenu(Menu menu) {
  74. // Inflate the menu; this adds items to the action bar if it is present.
  75. getMenuInflater().inflate(R.menu.menu_main, menu);
  76. return true;
  77. }
  78.  
  79. @Override
  80. public boolean onOptionsItemSelected(MenuItem item) {
  81. // Handle action bar item clicks here. The action bar will
  82. // automatically handle clicks on the Home/Up button, so long
  83. // as you specify a parent activity in AndroidManifest.xml.
  84. if(mActionBarDrawerToggle.onOptionsItemSelected(item))
  85. return true;
  86.  
  87. int id = item.getItemId();
  88.  
  89. //noinspection SimplifiableIfStatement
  90. if (id == R.id.action_settings) {
  91. return true;
  92. }
  93.  
  94. return super.onOptionsItemSelected(item);
  95. }
  96. }
  97.  
  98. <?xml version="1.0" encoding="utf-8"?>
  99. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  100. xmlns:app="http://schemas.android.com/apk/res-auto"
  101. xmlns:tools="http://schemas.android.com/tools"
  102. android:layout_width="match_parent"
  103. android:layout_height="match_parent"
  104. android:orientation="vertical"
  105. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  106. tools:context="com.example.bablu.zoo.MainActivity"
  107. tools:showIn="@layout/activity_main">
  108.  
  109. <android.support.v4.widget.DrawerLayout
  110. android:id="@+id/drawer_layout"
  111. android:layout_width="match_parent"
  112. android:layout_height="match_parent">
  113.  
  114. <TextView
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:text="Hello world"/>
  118.  
  119. <ListView android:id="@+id/left_drawer"
  120. android:layout_width="240dp"
  121. android:layout_height="match_parent"
  122. android:layout_gravity="start"
  123. android:choiceMode="singleChoice"
  124. android:divider="@android:color/transparent"
  125. android:dividerHeight="0dp"
  126. android:background="#111"/>
  127.  
  128.  
  129. </android.support.v4.widget.DrawerLayout>
  130. </LinearLayout>
  131.  
  132. <?xml version="1.0" encoding="utf-8"?>
  133. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  134. xmlns:app="http://schemas.android.com/apk/res-auto"
  135. xmlns:tools="http://schemas.android.com/tools"
  136. android:layout_width="match_parent"
  137. android:layout_height="match_parent"
  138. android:fitsSystemWindows="true"
  139. tools:context="com.example.bablu.zoo.MainActivity">
  140.  
  141. <android.support.design.widget.AppBarLayout
  142. android:layout_width="match_parent"
  143. android:layout_height="wrap_content"
  144. android:theme="@style/AppTheme.AppBarOverlay">
  145.  
  146. <android.support.v7.widget.Toolbar
  147. android:id="@+id/toolbar"
  148. android:layout_width="match_parent"
  149. android:layout_height="?attr/actionBarSize"
  150. android:background="?attr/colorPrimary"
  151. app:popupTheme="@style/AppTheme.PopupOverlay" />
  152.  
  153. </android.support.design.widget.AppBarLayout>
  154.  
  155. <include layout="@layout/content_main" />
  156.  
  157. </android.support.design.widget.CoordinatorLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement