Advertisement
minafaw3

ResultActivity

Oct 5th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. package com.imaadv.rist_management;
  2.  
  3. import android.os.PersistableBundle;
  4. import android.support.v4.view.GravityCompat;
  5. import android.support.v4.widget.DrawerLayout;
  6. import android.support.v7.app.ActionBarActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.Gravity;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12.  
  13. import java.util.ArrayList;
  14.  
  15. import android.support.v7.app.ActionBarDrawerToggle;
  16. import android.util.Log;
  17. import android.widget.AdapterView;
  18. import android.widget.ListView;
  19. import android.widget.Toast;
  20.  
  21. import com.imaadv.rist_management.adapter.NavDrawerListAdapter;
  22.  
  23. import com.imaadv.rist_management.fragments.AboutusFragment;
  24. import com.imaadv.rist_management.fragments.Home_fragment;
  25. import com.imaadv.rist_management.fragments.ReferenceFragment;
  26.  
  27. import com.imaadv.rist_management.model.NavDrawerItem;
  28.  
  29.  
  30. public class ResultActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
  31.  
  32.  
  33. MyApplication myApplication;
  34. private DrawerLayout mDrawerLayout;
  35. private ListView mDrawerList;
  36. private ActionBarDrawerToggle mDrawerToggle;
  37.  
  38. // nav drawer title
  39. private CharSequence mDrawerTitle;
  40.  
  41. // used to store app title
  42. private CharSequence mTitle;
  43.  
  44. // slide menu items
  45. private String[] navMenuTitles;
  46.  
  47. private ArrayList<NavDrawerItem> navDrawerItems;
  48. private NavDrawerListAdapter adapter;
  49.  
  50. Toolbar toolbar;
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.activity_result);
  56. toolbar = (Toolbar) findViewById(R.id.toolbar);
  57.  
  58. myApplication = (MyApplication) getApplication();
  59.  
  60. setSupportActionBar(toolbar);
  61. getSupportActionBar().setHomeButtonEnabled(true);
  62. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  63.  
  64. // load slide menu items
  65. navMenuTitles = getResources().getStringArray(R.array.nav_items);
  66.  
  67.  
  68. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  69. mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  70.  
  71. navDrawerItems = new ArrayList<NavDrawerItem>();
  72.  
  73. // adding nav drawer items to array
  74.  
  75. navDrawerItems.add(new NavDrawerItem(navMenuTitles[0]));
  76. navDrawerItems.add(new NavDrawerItem(navMenuTitles[1]));
  77. navDrawerItems.add(new NavDrawerItem(navMenuTitles[2]));
  78. navDrawerItems.add(new NavDrawerItem(navMenuTitles[3]));
  79. toolbar.setOnClickListener(new View.OnClickListener() {
  80. @Override
  81. public void onClick(View view) {
  82. if (mDrawerLayout.isDrawerOpen(GravityCompat.START)){
  83. mDrawerLayout.closeDrawers();
  84. }else{
  85. mDrawerLayout.openDrawer(GravityCompat.START);
  86. }
  87. }
  88. });
  89.  
  90.  
  91.  
  92. mDrawerList.setOnItemClickListener(this);
  93.  
  94. // setting the nav drawer list adapter
  95. adapter = new NavDrawerListAdapter(this, navDrawerItems);
  96. mDrawerList.setAdapter(adapter);
  97.  
  98.  
  99. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
  100. toolbar, //nav menu toggle icon
  101. R.string.app_name, // nav drawer open - description for accessibility
  102. R.string.app_name // nav drawer close - description for accessibility
  103. ) {
  104. @Override
  105. public void onDrawerClosed(View drawerView) {
  106. super.onDrawerClosed(drawerView);
  107. }
  108.  
  109. @Override
  110. public void onDrawerOpened(View drawerView) {
  111. super.onDrawerOpened(drawerView);
  112. }
  113. };
  114. mDrawerLayout.setDrawerListener(mDrawerToggle);
  115. mDrawerToggle.setDrawerIndicatorEnabled(true);
  116.  
  117.  
  118. if (savedInstanceState == null) {
  119. // on first time display view for first nav item
  120. displayView(0);
  121. }
  122. }
  123.  
  124. @Override
  125. public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
  126. super.onPostCreate(savedInstanceState, persistentState);
  127. mDrawerToggle.syncState();
  128. }
  129.  
  130. @Override
  131. public boolean onOptionsItemSelected(MenuItem item) {
  132. // toggle nav drawer on selecting action bar app icon/title
  133.  
  134. // Handle action bar actions click
  135. switch (item.getItemId()) {
  136. case R.id.action_settings:
  137. return true;
  138.  
  139.  
  140.  
  141. default:
  142. return super.onOptionsItemSelected(item);
  143. }
  144. }
  145.  
  146.  
  147. /**
  148. * Diplaying fragment view for selected nav drawer list item
  149. */
  150. private void displayView(int position) {
  151. // update the main content by replacing fragments
  152. android.support.v4.app.Fragment fragment = null;
  153. switch (position) {
  154. case 0:
  155. fragment = new Home_fragment();
  156. break;
  157. case 1:
  158. fragment = new AboutusFragment();
  159. break;
  160. case 2:
  161. fragment = new ReferenceFragment();
  162. break;
  163.  
  164. case 3:
  165. myApplication.rest_test(getBaseContext());
  166. break;
  167.  
  168. default:
  169. break;
  170. }
  171.  
  172. if (fragment != null) {
  173. android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
  174. fragmentManager.beginTransaction()
  175. .replace(R.id.frame_container, fragment).commit();
  176.  
  177. // update selected item and title, then close the drawer
  178. mDrawerList.setItemChecked(position, true);
  179. mDrawerList.setSelection(position);
  180. setTitle(navMenuTitles[position]);
  181. mDrawerLayout.closeDrawer(mDrawerList);
  182. } else {
  183. // error in creating fragment
  184. Log.e("MainActivity", "Error in creating fragment");
  185. }
  186. }
  187.  
  188.  
  189. /**
  190. * When using the ActionBarDrawerToggle, you must call it during
  191. * onPostCreate() and onConfigurationChanged()...
  192. */
  193.  
  194. @Override
  195. protected void onPostCreate(Bundle savedInstanceState) {
  196. super.onPostCreate(savedInstanceState);
  197. // Sync the toggle state after onRestoreInstanceState has occurred.
  198. mDrawerToggle.syncState();
  199. }
  200.  
  201.  
  202. @Override
  203. public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
  204. displayView(position);
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement