Advertisement
markella92

Untitled

Dec 1st, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.60 KB | None | 0 0
  1. package info.androidhive.navigationdrawer.activity;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.support.design.widget.CoordinatorLayout;
  7. import android.support.design.widget.FloatingActionButton;
  8. import android.support.design.widget.NavigationView;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v4.app.Fragment;
  11. import android.support.v4.app.FragmentTransaction;
  12. import android.support.v4.view.GravityCompat;
  13. import android.support.v4.widget.DrawerLayout;
  14. import android.support.v7.app.ActionBarDrawerToggle;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.support.v7.widget.Toolbar;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.widget.ImageView;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23.  
  24. import com.bumptech.glide.Glide;
  25. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  26.  
  27. import info.androidhive.navigationdrawer.R;
  28. import info.androidhive.navigationdrawer.fragment.HomeFragment;
  29. import info.androidhive.navigationdrawer.fragment.MoviesFragment;
  30. import info.androidhive.navigationdrawer.fragment.NotificationsFragment;
  31. import info.androidhive.navigationdrawer.fragment.PhotosFragment;
  32. import info.androidhive.navigationdrawer.fragment.SettingsFragment;
  33. import info.androidhive.navigationdrawer.other.CircleTransform;
  34.  
  35. public class MainActivity extends AppCompatActivity {
  36.  
  37. private NavigationView navigationView;
  38. private DrawerLayout drawer;
  39. private CoordinatorLayout coordinatorLayout;
  40. private View navHeader;
  41. private ImageView imgNavHeaderBg, imgProfile;
  42. private TextView txtName, txtWebsite;
  43. private Toolbar toolbar;
  44. private FloatingActionButton fab;
  45.  
  46. // urls to load navigation header background image
  47. // and profile image
  48. private static final String urlNavHeaderBg = "http://api.androidhive.info/images/nav-menu-header-bg.jpg";
  49. private static final String urlProfileImg = "https://lh3.googleusercontent.com/eCtE_G34M9ygdkmOpYvCag1vBARCmZwnVS6rS5t4JLzJ6QgQSBquM0nuTsCpLhYbKljoyS-txg";
  50.  
  51. // index to identify current nav menu item
  52. public static int navItemIndex = 0;
  53.  
  54. // tags used to attach the fragments
  55. private static final String TAG_HOME = "home";
  56. private static final String TAG_PHOTOS = "photos";
  57. private static final String TAG_MOVIES = "movies";
  58. private static final String TAG_NOTIFICATIONS = "notifications";
  59. private static final String TAG_SETTINGS = "settings";
  60. public static String CURRENT_TAG = TAG_HOME;
  61.  
  62. // toolbar titles respected to selected nav menu item
  63. private String[] activityTitles;
  64.  
  65. // flag to load home fragment when user presses back key
  66. private boolean shouldLoadHomeFragOnBackPress = true;
  67. private Handler mHandler;
  68.  
  69.  
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.activity_main);
  74. toolbar = (Toolbar) findViewById(R.id.toolbar);
  75. setSupportActionBar(toolbar);
  76. coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);
  77.  
  78. mHandler = new Handler();
  79.  
  80. drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  81.  
  82. navigationView = (NavigationView) findViewById(R.id.nav_view);
  83. fab = (FloatingActionButton) findViewById(R.id.fab);
  84.  
  85. // Navigation view header
  86. navHeader = navigationView.getHeaderView(0);
  87. txtName = (TextView) navHeader.findViewById(R.id.name);
  88. txtWebsite = (TextView) navHeader.findViewById(R.id.website);
  89. imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);
  90. imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile);
  91.  
  92. // load toolbar titles from string resources
  93. activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);
  94.  
  95. fab.setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View view) {
  98. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  99. .setAction("Action", null).show();
  100. }
  101. });
  102.  
  103. // load nav menu header data
  104. loadNavHeader();
  105.  
  106. // initializing navigation menu
  107. setUpNavigationView();
  108.  
  109. if (savedInstanceState == null) {
  110. navItemIndex = 0;
  111. CURRENT_TAG = TAG_HOME;
  112. loadHomeFragment();
  113. }
  114. }
  115.  
  116. /***
  117. * Load navigation menu header information
  118. * like background image, profile image
  119. * name, website, notifications action view (dot)
  120. */
  121. private void loadNavHeader() {
  122. // name, website
  123. txtName.setText("Ravi Tamada");
  124. txtWebsite.setText("www.androidhive.info");
  125.  
  126. // loading header background image
  127. Glide.with(this).load(urlNavHeaderBg)
  128. .crossFade()
  129. .diskCacheStrategy(DiskCacheStrategy.ALL)
  130. .into(imgNavHeaderBg);
  131.  
  132. // Loading profile image
  133. Glide.with(this).load(urlProfileImg)
  134. .crossFade()
  135. .thumbnail(0.5f)
  136. .bitmapTransform(new CircleTransform(this))
  137. .diskCacheStrategy(DiskCacheStrategy.ALL)
  138. .into(imgProfile);
  139.  
  140. // showing dot next to notifications label
  141. navigationView.getMenu().getItem(3).setActionView(R.layout.menu_dot);
  142. }
  143.  
  144. /***
  145. * Returns respected fragment that user
  146. * selected from navigation menu
  147. */
  148. private void loadHomeFragment() {
  149. // selecting appropriate nav menu item
  150. selectNavMenu();
  151.  
  152. // set toolbar title
  153. setToolbarTitle();
  154.  
  155. // if user select the current navigation menu again, don't do anything
  156. // just close the navigation drawer
  157. if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
  158. drawer.closeDrawers();
  159.  
  160. // show or hide the fab button
  161. toggleFab();
  162. return;
  163. }
  164.  
  165. // Sometimes, when fragment has huge data, screen seems hanging
  166. // when switching between navigation menus
  167. // So using runnable, the fragment is loaded with cross fade effect
  168. // This effect can be seen in GMail app
  169. Runnable mPendingRunnable = new Runnable() {
  170. @Override
  171. public void run() {
  172. // update the main content by replacing fragments
  173. Fragment fragment = getHomeFragment();
  174. FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  175. fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
  176. android.R.anim.fade_out);
  177. fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
  178. fragmentTransaction.commitAllowingStateLoss();
  179. }
  180. };
  181.  
  182. // If mPendingRunnable is not null, then add to the message queue
  183. if (mPendingRunnable != null) {
  184. mHandler.post(mPendingRunnable);
  185. }
  186.  
  187. // show or hide the fab button
  188. toggleFab();
  189.  
  190. //Closing drawer on item click
  191. drawer.closeDrawers();
  192.  
  193. // refresh toolbar menu
  194. invalidateOptionsMenu();
  195. }
  196.  
  197. private Fragment getHomeFragment() {
  198. switch (navItemIndex) {
  199. case 0:
  200. // home
  201. HomeFragment homeFragment = new HomeFragment();
  202. return homeFragment;
  203. case 1:
  204. // photos
  205. PhotosFragment photosFragment = new PhotosFragment();
  206. return photosFragment;
  207. case 2:
  208. // movies fragment
  209. MoviesFragment moviesFragment = new MoviesFragment();
  210. return moviesFragment;
  211. case 3:
  212. // notifications fragment
  213. NotificationsFragment notificationsFragment = new NotificationsFragment();
  214. return notificationsFragment;
  215.  
  216. case 4:
  217. // settings fragment
  218. SettingsFragment settingsFragment = new SettingsFragment();
  219. return settingsFragment;
  220. default:
  221. return new HomeFragment();
  222. }
  223. }
  224.  
  225. private void setToolbarTitle() {
  226. getSupportActionBar().setTitle(activityTitles[navItemIndex]);
  227. }
  228.  
  229. private void selectNavMenu() {
  230. navigationView.getMenu().getItem(navItemIndex).setChecked(true);
  231. }
  232.  
  233. private void setUpNavigationView() {
  234. //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
  235. navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  236.  
  237. // This method will trigger on item Click of navigation menu
  238. @Override
  239. public boolean onNavigationItemSelected(MenuItem menuItem) {
  240.  
  241. //Check to see which item was being clicked and perform appropriate action
  242. switch (menuItem.getItemId()) {
  243. //Replacing the main content with ContentFragment Which is our Inbox View;
  244. case R.id.home:
  245. navItemIndex = 0;
  246. CURRENT_TAG = TAG_HOME;
  247. break;
  248. case R.id.nav_photos:
  249. navItemIndex = 1;
  250. CURRENT_TAG = TAG_PHOTOS;
  251. break;
  252. case R.id.nav_movies:
  253. navItemIndex = 2;
  254. CURRENT_TAG = TAG_MOVIES;
  255. break;
  256. case R.id.nav_notifications:
  257. navItemIndex = 3;
  258. CURRENT_TAG = TAG_NOTIFICATIONS;
  259. break;
  260. case R.id.nav_settings:
  261. navItemIndex = 4;
  262. CURRENT_TAG = TAG_SETTINGS;
  263. break;
  264. case R.id.nav_about_us:
  265. // launch new intent instead of loading fragment
  266. startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
  267. drawer.closeDrawers();
  268. return true;
  269. case R.id.nav_privacy_policy:
  270. // launch new intent instead of loading fragment
  271. startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
  272. drawer.closeDrawers();
  273. return true;
  274. default:
  275. navItemIndex = 0;
  276. }
  277.  
  278. //Checking if the item is in checked state or not, if not make it in checked state
  279. if (menuItem.isChecked()) {
  280. menuItem.setChecked(false);
  281. } else {
  282. menuItem.setChecked(true);
  283. }
  284. menuItem.setChecked(true);
  285.  
  286. loadHomeFragment();
  287.  
  288. return true;
  289. }
  290. });
  291.  
  292.  
  293. ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
  294.  
  295. @Override
  296. public void onDrawerClosed(View drawerView) {
  297. // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
  298. super.onDrawerClosed(drawerView);
  299. }
  300.  
  301. @Override
  302. public void onDrawerOpened(View drawerView) {
  303. // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
  304. super.onDrawerOpened(drawerView);
  305. }
  306. };
  307.  
  308. //Setting the actionbarToggle to drawer layout
  309. drawer.setDrawerListener(actionBarDrawerToggle);
  310.  
  311. //calling sync state is necessary or else your hamburger icon wont show up
  312. actionBarDrawerToggle.syncState();
  313. }
  314.  
  315. @Override
  316. public void onBackPressed() {
  317. if (drawer.isDrawerOpen(GravityCompat.START)) {
  318. drawer.closeDrawers();
  319. return;
  320. }
  321.  
  322. // This code loads home fragment when back key is pressed
  323. // when user is in other fragment than home
  324. if (shouldLoadHomeFragOnBackPress) {
  325. // checking if user is on other navigation menu
  326. // rather than home
  327. if (navItemIndex != 0) {
  328. navItemIndex = 0;
  329. CURRENT_TAG = TAG_HOME;
  330. loadHomeFragment();
  331. return;
  332. }
  333. }
  334.  
  335. super.onBackPressed();
  336. }
  337.  
  338. @Override
  339. public boolean onCreateOptionsMenu(Menu menu) {
  340. // Inflate the menu; this adds items to the action bar if it is present.
  341.  
  342. // show menu only when home fragment is selected
  343. if (navItemIndex == 0) {
  344. getMenuInflater().inflate(R.menu.main, menu);
  345. }
  346.  
  347. // when fragment is notifications, load the menu created for notifications
  348. if (navItemIndex == 3) {
  349. getMenuInflater().inflate(R.menu.notifications, menu);
  350. }
  351. return true;
  352. }
  353.  
  354. @Override
  355. public boolean onOptionsItemSelected(MenuItem item) {
  356. // Handle action bar item clicks here. The action bar will
  357. // automatically handle clicks on the Home/Up button, so long
  358. // as you specify a parent activity in AndroidManifest.xml.
  359. int id = item.getItemId();
  360.  
  361. //noinspection SimplifiableIfStatement
  362. if (id == R.id.action_logout) {
  363. Toast.makeText(getApplicationContext(), "Logout user!", Toast.LENGTH_LONG).show();
  364. return true;
  365. }
  366.  
  367. // user is in notifications fragment
  368. // and selected 'Mark all as Read'
  369. if (id == R.id.action_mark_all_read) {
  370. Toast.makeText(getApplicationContext(), "All notifications marked as read!", Toast.LENGTH_LONG).show();
  371. }
  372.  
  373. // user is in notifications fragment
  374. // and selected 'Clear All'
  375. if (id == R.id.action_clear_notifications) {
  376. Toast.makeText(getApplicationContext(), "Clear all notifications!", Toast.LENGTH_LONG).show();
  377. }
  378.  
  379. return super.onOptionsItemSelected(item);
  380. }
  381.  
  382. // show or hide the fab
  383. private void toggleFab() {
  384. if (navItemIndex == 0)
  385. fab.show();
  386. else
  387. fab.hide();
  388. }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement