Guest User

Untitled

a guest
Dec 6th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setTitle("first activity");
  5. setContentView(R.layout.activity_main);
  6.  
  7. toolbar = findViewById(R.id.toolbar);
  8. setSupportActionBar(toolbar);
  9.  
  10. drawer = findViewById(R.id.drawer_layout);
  11. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  12. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  13. drawer.addDrawerListener(toggle);
  14. toggle.syncState();
  15.  
  16. navigationView = findViewById(R.id.nav_view);
  17. navigationView.setNavigationItemSelectedListener(this);
  18. Menu menu = navigationView.getMenu();
  19. v_camera = menu.findItem(R.id.nav_camera);
  20. v_contacts = menu.findItem(R.id.nav_contacts);
  21. v_parties = menu.findItem(R.id.nav_parties);
  22. v_gallery = menu.findItem(R.id.nav_gallery);
  23. v_logout = menu.findItem(R.id.nav_logout);
  24. v_manage = menu.findItem(R.id.nav_manage);
  25. v_share = menu.findItem(R.id.nav_share);
  26. v_send = menu.findItem(R.id.nav_send);
  27. vf = findViewById(R.id.viewflipper);
  28. vf.setDisplayedChild(vf.indexOfChild(findViewById(R.id.page1)));
  29. changeNavMenuItems(false);
  30.  
  31. page1 = findViewById(R.id.page1);
  32. page2 = findViewById(R.id.page2);
  33.  
  34. SharedPreferences.OnSharedPreferenceChangeListener sharedPref_listener =
  35. new SharedPreferences.OnSharedPreferenceChangeListener() {
  36. @Override
  37. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  38. Log.e("SharedChanged",
  39. "key= " + key + " " + sharedPreferences.getString(key, null));
  40. if (sharedPreferences.getString(key, null) == null)
  41. changeNavMenuItems(false);
  42. else
  43. changeNavMenuItems(true);
  44. }
  45. };
  46. G.sharedPref.registerOnSharedPreferenceChangeListener(sharedPref_listener);
  47. rq = Volley.newRequestQueue(MainActivity.this);
  48. onLayoutChangeListener = new View.OnLayoutChangeListener() {
  49. @Override
  50. public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
  51. Log.e("check view", v.toString());
  52. if (vf.getCurrentView() == page1) {
  53.  
  54. Button signIn_btn = findViewById(R.id.signInBtn);
  55. signIn_btn.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. EditText userName = findViewById(R.id.username);
  59. EditText passWord = findViewById(R.id.password);
  60. str_username = userName.getText().toString();
  61. str_password = passWord.getText().toString();
  62.  
  63. StringRequest JsonReq = new StringRequest(Request.Method.POST,
  64. "https://www.ghasedakia.com/core/api/cc_login",
  65. new Response.Listener<String>() {
  66. @Override
  67. public void onResponse(String response) {
  68. Log.e("response", response);
  69. String session_id = null;
  70. try {
  71. JSONObject jsonObj = new JSONObject(response);
  72. session_id = jsonObj.getString("session_id");
  73. } catch (JSONException e) {
  74. e.printStackTrace();
  75. }
  76.  
  77. if (session_id != null) { ///Authorize successful
  78. Log.e("LOG", session_id);
  79. G.sharedPref.edit().putString("session_id", session_id).apply();
  80.  
  81. vf.setDisplayedChild(vf.indexOfChild(findViewById(R.id.page2)));
  82.  
  83. } else { //// not authorized
  84. Log.e("LOG", "User Not found");
  85. }
  86. }
  87. },
  88. new Response.ErrorListener() {
  89. @Override
  90. public void onErrorResponse(VolleyError error) {
  91. Log.e("error", error.toString());
  92. }
  93. }) {
  94. Map<String, String> params = new HashMap<String, String>();
  95.  
  96. @Override
  97. public Map<String, String> getParams() {
  98.  
  99. params.put("password", str_password);
  100. params.put("username", str_username);
  101.  
  102. return params;
  103. }
  104. };
  105. JsonReq.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  106. rq.add(JsonReq);
  107. }
  108. });
  109. } else if (vf.getCurrentView() == page2) {
  110. Log.e("layout", "changed to page2");
  111. getContactsFromServer(G.sharedPref.getString("session_id", null));
  112. }
  113. }
  114. };
  115. vf.addOnLayoutChangeListener(onLayoutChangeListener);
  116. }
  117.  
  118. <android.support.v4.widget.DrawerLayout
  119. xmlns:android="http://schemas.android.com/apk/res/android"
  120. xmlns:app="http://schemas.android.com/apk/res-auto"
  121. xmlns:tools="http://schemas.android.com/tools"
  122. android:id="@+id/drawer_layout"
  123. android:layout_width="match_parent"
  124. android:layout_height="match_parent"
  125. android:fitsSystemWindows="true"
  126. tools:openDrawer="start">
  127.  
  128. <include
  129. layout="@layout/app_bar_main"
  130. android:layout_width="match_parent"
  131. android:layout_height="match_parent" />
  132.  
  133. <android.support.design.widget.NavigationView
  134. android:id="@+id/nav_view"
  135. android:layout_width="wrap_content"
  136. android:layout_height="match_parent"
  137. android:layout_gravity="start"
  138. android:fitsSystemWindows="true"
  139. app:headerLayout="@layout/nav_header_main"
  140. app:menu="@menu/activity_main_drawer" />
  141.  
  142. <?xml version="1.0" encoding="utf-8"?>
  143. <android.support.design.widget.CoordinatorLayout
  144. xmlns:android="http://schemas.android.com/apk/res/android"
  145. xmlns:app="http://schemas.android.com/apk/res-auto"
  146. xmlns:tools="http://schemas.android.com/tools"
  147. android:layout_width="match_parent"
  148. android:layout_height="match_parent"
  149. tools:context="com.ghasedakia.MainActivity">
  150.  
  151. <android.support.design.widget.AppBarLayout
  152. android:layout_width="match_parent"
  153. android:layout_height="wrap_content"
  154. android:theme="@style/AppTheme.AppBarOverlay">
  155.  
  156. <android.support.v7.widget.Toolbar
  157. android:id="@+id/toolbar"
  158. android:layout_width="match_parent"
  159. android:layout_height="?attr/actionBarSize"
  160. app:popupTheme="@style/AppTheme.PopupOverlay" />
  161.  
  162. </android.support.design.widget.AppBarLayout>
  163. <ViewFlipper
  164. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  165. android:layout_width="match_parent"
  166. android:layout_height="match_parent"
  167. android:id="@+id/viewflipper">
  168. <include
  169. android:id="@+id/page1" layout="@layout/content_main" />
  170. <include
  171. android:id="@+id/page2" layout="@layout/contacts_page" />
  172. </ViewFlipper>
  173.  
  174. <?xml version="1.0" encoding="utf-8"?>
  175. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  176. xmlns:app="http://schemas.android.com/apk/res-auto"
  177. android:layout_width="match_parent"
  178. android:layout_height="@dimen/nav_header_height"
  179. android:background="@drawable/bg"
  180. android:gravity="bottom"
  181. android:orientation="vertical"
  182. android:paddingBottom="@dimen/activity_vertical_margin"
  183. android:paddingLeft="@dimen/activity_horizontal_margin"
  184. android:paddingRight="@dimen/activity_horizontal_margin"
  185. android:paddingTop="@dimen/activity_vertical_margin"
  186. android:theme="@style/ThemeOverlay.AppCompat.Dark">
  187.  
  188. <ImageView
  189. android:id="@+id/nav_image"
  190. android:layout_width="wrap_content"
  191. android:layout_height="wrap_content"
  192. android:paddingTop="@dimen/nav_header_vertical_spacing"
  193. app:srcCompat="@mipmap/ic_launcher_round" />
  194.  
  195. <TextView
  196. android:id="@+id/nav_username"
  197. android:layout_width="match_parent"
  198. android:layout_height="wrap_content"
  199. android:paddingTop="@dimen/nav_header_vertical_spacing"
  200. android:text="Android Studio"
  201. android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  202.  
  203. <?xml version="1.0" encoding="utf-8"?>
  204. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  205. xmlns:tools="http://schemas.android.com/tools"
  206. tools:showIn="navigation_view">
  207.  
  208. <group android:checkableBehavior="single">
  209. <item
  210. android:id="@+id/nav_contacts"
  211. android:icon="@drawable/ic_person_black"
  212. android:title="Contacts" />
  213. <item
  214. android:id="@+id/nav_parties"
  215. android:icon="@drawable/ic_cake_black"
  216. android:title="Parties" />
  217. <item
  218. android:id="@+id/nav_camera"
  219. android:icon="@drawable/ic_menu_camera"
  220. android:title="Import" />
  221. <item
  222. android:id="@+id/nav_gallery"
  223. android:icon="@drawable/ic_menu_gallery"
  224. android:title="Gallery" />
  225. <item
  226. android:id="@+id/nav_logout"
  227. android:icon="@drawable/ic_menu_logout"
  228. android:title="Log out" />
  229. <item
  230. android:id="@+id/nav_manage"
  231. android:icon="@drawable/ic_menu_manage"
  232. android:title="Tools" />
  233. </group>
  234.  
  235. <item android:title="Communicate">
  236. <menu>
  237. <item
  238. android:id="@+id/nav_share"
  239. android:icon="@drawable/ic_menu_share"
  240. android:title="Share" />
  241. <item
  242. android:id="@+id/nav_send"
  243. android:icon="@drawable/ic_menu_send"
  244. android:title="Send" />
  245. </menu>
  246. </item>
Add Comment
Please, Sign In to add comment