Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. tools:context=".LoginFragment"
  7. xmlns:android="http://schemas.android.com/apk/res/android">
  8. <RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent">
  11.  
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:textAppearance="?android:attr/textAppearanceLarge"
  16. android:text="Login."
  17. android:id="@+id/login_title_text"
  18. android:layout_alignParentTop="true"
  19. android:textSize="40sp"
  20. android:layout_marginTop="100dp"
  21. android:fontFamily="sans-serif-light"
  22. android:textColor="@color/colorAlt"
  23. android:layout_centerHorizontal="true" />
  24.  
  25. <!-- E-mail section -->
  26. <EditText
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:inputType="textEmailAddress"
  30. android:hint="E-mail address"
  31. android:layout_marginLeft="50dp"
  32. android:layout_marginTop="10dp"
  33. android:layout_marginRight="20dp"
  34. android:ems="10"
  35. android:id="@+id/login_emailField"
  36. android:layout_above="@+id/login_passwordField" />
  37. <ImageView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:src="@drawable/register_email_icon"
  41. android:layout_alignBottom="@id/login_emailField"
  42. android:layout_marginLeft="15dp"
  43. android:layout_marginBottom="10dp"/>
  44.  
  45.  
  46. <!-- Password section -->
  47. <ImageView
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:src="@drawable/register_password_icon"
  51. android:layout_alignBottom="@id/login_passwordField"
  52. android:layout_marginLeft="15dp"
  53. android:layout_marginBottom="10dp"/>
  54.  
  55. <EditText
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:inputType="textPassword"
  59. android:layout_marginTop="10dp"
  60. android:hint="Password"
  61. android:id="@+id/login_passwordField"
  62. android:layout_centerVertical="true"
  63. android:layout_marginLeft="50dp"
  64. android:layout_marginRight="20dp"
  65. android:layout_marginBottom="25dp" />
  66.  
  67. <android.support.v7.widget.CardView
  68. xmlns:android="http://schemas.android.com/apk/res/android"
  69. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  70. android:layout_width="match_parent"
  71. android:layout_height="wrap_content"
  72. android:id="@+id/login_login_button"
  73. android:layout_gravity="center_vertical"
  74. android:layout_below="@+id/login_passwordField"
  75. android:layout_marginBottom="29dp"
  76. android:layout_marginLeft="20dp"
  77. android:layout_marginRight="20dp"
  78. card_view:cardBackgroundColor="#E91E63"
  79. card_view:cardCornerRadius="2dp"
  80. card_view:cardElevation="6dp">
  81.  
  82. <TextView
  83. android:id="@+id/material_text"
  84. android:layout_width="wrap_content"
  85. android:layout_height="wrap_content"
  86. android:paddingTop="13dp"
  87. android:paddingBottom="13dp"
  88. android:layout_gravity="center_horizontal"
  89. android:textColor="#FFF"
  90. android:textSize="19sp"
  91. android:fontFamily="sans-serif-medium"
  92. android:text="Login" />
  93. </android.support.v7.widget.CardView>
  94.  
  95.  
  96.  
  97. <TextView
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:textAppearance="?android:attr/textAppearanceMedium"
  101. android:text="Forgotten your username or password?"
  102. android:layout_below="@id/login_login_button"
  103. android:gravity="center"
  104. android:layout_centerHorizontal="true"
  105. android:id="@+id/login_footer_text"
  106. android:layout_marginTop="40dp"/>
  107. <TextView
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:textAppearance="?android:attr/textAppearanceMedium"
  111. android:text="Tap here"
  112. android:textColor="#000000"
  113. android:layout_below="@id/login_footer_text"
  114. android:gravity="center"
  115. android:layout_centerHorizontal="true"
  116. android:id="@+id/loginForgottenTapText"
  117. android:layout_marginTop="20dp"/>
  118. </RelativeLayout>
  119.  
  120. </LinearLayout>
  121.  
  122. <?xml version="1.0" encoding="utf-8"?>
  123. <ScrollView
  124. android:layout_width="match_parent"
  125. android:layout_height="wrap_content"
  126. xmlns:tools="http://schemas.android.com/tools"
  127. tools:context=".LoginFragment"
  128. xmlns:android="http://schemas.android.com/apk/res/android">
  129. <RelativeLayout
  130. android:layout_width="match_parent"
  131. android:layout_height="match_parent">
  132.  
  133. <TextView
  134. android:layout_width="wrap_content"
  135. android:layout_height="wrap_content"
  136. android:textAppearance="?android:attr/textAppearanceLarge"
  137. android:text="Login."
  138. android:id="@+id/login_title_text"
  139. android:layout_alignParentTop="true"
  140. android:textSize="40sp"
  141. android:layout_marginTop="100dp"
  142. android:fontFamily="sans-serif-light"
  143. android:textColor="@color/colorAlt"
  144. android:layout_centerHorizontal="true" />
  145.  
  146. <!-- E-mail section -->
  147. <EditText
  148. android:layout_width="match_parent"
  149. android:layout_height="wrap_content"
  150. android:inputType="textEmailAddress"
  151. android:hint="E-mail address"
  152. android:layout_marginLeft="50dp"
  153. android:layout_marginTop="10dp"
  154. android:layout_marginRight="20dp"
  155. android:ems="10"
  156. android:id="@+id/login_emailField"
  157. android:layout_below="@+id/login_title_text" />
  158. <ImageView
  159. android:layout_width="wrap_content"
  160. android:layout_height="wrap_content"
  161. android:src="@drawable/register_email_icon"
  162. android:layout_alignBottom="@id/login_emailField"
  163. android:layout_marginLeft="15dp"
  164. android:layout_marginBottom="10dp"/>
  165.  
  166.  
  167. <!-- Password section -->
  168.  
  169. <EditText
  170. android:layout_width="match_parent"
  171. android:layout_height="wrap_content"
  172. android:inputType="textPassword"
  173. android:layout_marginTop="10dp"
  174. android:hint="Password"
  175. android:layout_below="@id/login_emailField"
  176. android:id="@+id/login_passwordField"
  177. android:layout_marginLeft="50dp"
  178. android:layout_marginRight="20dp"
  179. android:layout_marginBottom="25dp" />
  180.  
  181. <ImageView
  182. android:layout_width="wrap_content"
  183. android:layout_height="wrap_content"
  184. android:src="@drawable/register_password_icon"
  185. android:layout_alignBottom="@id/login_passwordField"
  186. android:layout_marginLeft="15dp"
  187. android:layout_marginBottom="10dp"/>
  188.  
  189. <android.support.v7.widget.CardView
  190. xmlns:android="http://schemas.android.com/apk/res/android"
  191. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  192. android:layout_width="match_parent"
  193. android:layout_height="wrap_content"
  194. android:id="@+id/login_login_button"
  195. android:layout_gravity="center_vertical"
  196. android:layout_below="@+id/login_passwordField"
  197. android:layout_marginBottom="29dp"
  198. android:layout_marginLeft="20dp"
  199. android:layout_marginRight="20dp"
  200. card_view:cardBackgroundColor="#E91E63"
  201. card_view:cardCornerRadius="2dp"
  202. card_view:cardElevation="6dp">
  203.  
  204. <TextView
  205. android:id="@+id/material_text"
  206. android:layout_width="wrap_content"
  207. android:layout_height="wrap_content"
  208. android:paddingTop="13dp"
  209. android:paddingBottom="13dp"
  210. android:layout_gravity="center_horizontal"
  211. android:textColor="#FFF"
  212. android:textSize="19sp"
  213. android:fontFamily="sans-serif-medium"
  214. android:text="Login" />
  215. </android.support.v7.widget.CardView>
  216.  
  217.  
  218.  
  219. <TextView
  220. android:layout_width="wrap_content"
  221. android:layout_height="wrap_content"
  222. android:textAppearance="?android:attr/textAppearanceMedium"
  223. android:text="Forgotten your username or password?"
  224. android:layout_below="@id/login_login_button"
  225. android:gravity="center"
  226. android:layout_centerHorizontal="true"
  227. android:id="@+id/login_footer_text"
  228. android:layout_marginTop="20dp"/>
  229. <TextView
  230. android:layout_width="wrap_content"
  231. android:layout_height="wrap_content"
  232. android:textAppearance="?android:attr/textAppearanceMedium"
  233. android:text="Tap here"
  234. android:textColor="#000000"
  235. android:layout_below="@id/login_footer_text"
  236. android:gravity="center"
  237. android:layout_centerHorizontal="true"
  238. android:id="@+id/loginForgottenTapText"
  239. android:layout_marginTop="10dp"/>
  240. </RelativeLayout>
  241.  
  242. </ScrollView>
  243.  
  244. public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, LoginFragment.OnLoginCallback {
  245.  
  246. MenuItem register;
  247. MenuItem login;
  248. MenuItem logout;
  249. MenuItem completeListing;
  250. MenuItem expiredListing;
  251. MenuItem activeListing;
  252.  
  253. NavigationView navigationView;
  254. UserCredentialHandler userStatus;
  255.  
  256. protected static final String KEY_USER_STATUS = "USER_STATUS";
  257. protected static final String USER_PREFS = "userNamePrefs";
  258.  
  259. @Override
  260. protected void onCreate(Bundle savedInstanceState) {
  261. super.onCreate(savedInstanceState);
  262. setContentView(R.layout.activity_main);
  263. userStatus = new UserCredentialHandler();
  264.  
  265. final HomeFragment homeFragment = new HomeFragment();
  266.  
  267. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  268. setSupportActionBar(toolbar);
  269.  
  270. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  271. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  272. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  273. drawer.setDrawerListener(toggle);
  274. toggle.syncState();
  275.  
  276.  
  277. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  278. fab.setOnClickListener(new View.OnClickListener() {
  279. @Override
  280. public void onClick(View view) {
  281. Intent i = new Intent(getApplicationContext(), CreateListingActivity.class);
  282. startActivity(i);
  283. }
  284. });
  285.  
  286. navigationView = (NavigationView) findViewById(R.id.nav_view);
  287. navigationView.setNavigationItemSelectedListener(this);
  288.  
  289. getMenuItems(navigationView);
  290.  
  291. if(userStatus.checkIfUserIsLoggedIn(getApplicationContext())){
  292. userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
  293. updateNavDrawer("login", register, login, logout, expiredListing, completeListing, activeListing);
  294. } else {
  295. userStatus.setNavHeaderOnLogout(navigationView);
  296. updateNavDrawer("logout", register, login, logout, expiredListing, completeListing, activeListing);
  297. }
  298.  
  299. FragmentManager fragmentManager = getSupportFragmentManager();
  300. FragmentTransaction fragmentTransaction = fragmentManager
  301. .beginTransaction();
  302. fragmentTransaction.replace(R.id.main_container,homeFragment);
  303. fragmentTransaction.commit();
  304. }
  305.  
  306. @Override
  307. public void onLoginSuccess() {
  308. HomeFragment homeFragment = new HomeFragment();
  309. userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
  310.  
  311. updateNavDrawer("login",register,login,logout, expiredListing, completeListing, activeListing);
  312.  
  313. register.setVisible(false);
  314. login.setVisible(false);
  315. logout.setVisible(true);
  316.  
  317. FragmentManager fragmentManager = getSupportFragmentManager();
  318. FragmentTransaction fragmentTransaction = fragmentManager
  319. .beginTransaction();
  320. fragmentTransaction.replace(R.id.main_container,homeFragment);
  321. fragmentTransaction.commit();
  322. }
  323.  
  324.  
  325. @Override
  326. public void onBackPressed() {
  327. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  328. if (drawer.isDrawerOpen(GravityCompat.START)) {
  329. drawer.closeDrawer(GravityCompat.START);
  330. } else {
  331. getSupportFragmentManager().popBackStack();
  332. }
  333. }
  334.  
  335. @Override
  336. public boolean onCreateOptionsMenu(Menu menu) {
  337. // Inflate the menu; this adds items to the action bar if it is present.
  338. getMenuInflater().inflate(R.menu.main_activity_main, menu);
  339. return true;
  340. }
  341.  
  342. @Override
  343. public boolean onOptionsItemSelected(MenuItem item) {
  344. // Handle action bar item clicks here. The action bar will
  345. // automatically handle clicks on the Home/Up button, so long
  346. // as you specify a parent activity in AndroidManifest.xml.
  347. int id = item.getItemId();
  348.  
  349. //noinspection SimplifiableIfStatement
  350. if (id == R.id.action_settings) {
  351. return true;
  352. } else if (id == R.id.search_mag_icon){
  353. SearchFragment searchFragment = new SearchFragment();
  354. getSupportFragmentManager()
  355. .beginTransaction()
  356. .replace(R.id.main_container, searchFragment)
  357. .addToBackStack(null)
  358. .commit();
  359. }
  360. return super.onOptionsItemSelected(item);
  361. }
  362.  
  363. @SuppressWarnings("StatementWithEmptyBody")
  364. @Override
  365. public boolean onNavigationItemSelected(MenuItem item) {
  366. // Handle navigation view item clicks here.
  367. int id = item.getItemId();
  368.  
  369. if (id == R.id.nav_register) {
  370. final RegisterFragment registerFragment = new RegisterFragment();
  371.  
  372. getSupportFragmentManager()
  373. .beginTransaction()
  374. .replace(R.id.main_container, registerFragment)
  375. .addToBackStack(null)
  376. .commit();
  377.  
  378. } else if (id == R.id.nav_login) {
  379. final LoginFragment loginFragment = new LoginFragment();
  380. getSupportFragmentManager()
  381. .beginTransaction()
  382. .replace(R.id.main_container, loginFragment)
  383. .addToBackStack(null)
  384. .commit();
  385.  
  386. } else if(id == R.id.nav_logout) {
  387. userStatus.logoutUser(getApplicationContext());
  388. userStatus.setNavHeaderOnLogout(navigationView);
  389. updateNavDrawer("logout",register,login,logout, expiredListing, completeListing, activeListing);
  390.  
  391. } else if (id == R.id.nav_my_listings) {
  392. // navigate to my listings
  393. } else if (id == R.id.nav_how) {
  394. // navigate to how it works page
  395. } else if (id == R.id.nav_help) {
  396. // navigate to help page
  397. } else if (id == R.id.nav_contact_us) {
  398. // navigate to contact page,
  399. }
  400.  
  401. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  402. drawer.closeDrawer(GravityCompat.START);
  403. return true;
  404. }
  405.  
  406. private void updateNavDrawer(String action, MenuItem register, MenuItem login, MenuItem logout, MenuItem expiredListing, MenuItem completeListing, MenuItem activeListing){
  407. if(action=="login"){
  408. register.setVisible(false);
  409. login.setVisible(false);
  410. logout.setVisible(true);
  411. completeListing.setVisible(true);
  412. activeListing.setVisible(true);
  413. expiredListing.setVisible(true);
  414. } else {
  415. register.setVisible(true);
  416. login.setVisible(true);
  417. logout.setVisible(false);
  418. completeListing.setVisible(false);
  419. activeListing.setVisible(false);
  420. expiredListing.setVisible(false);
  421. }
  422. }
  423.  
  424. private void getMenuItems(NavigationView nv){
  425. register = nv.getMenu().getItem(0);
  426. login = nv.getMenu().getItem(1);
  427. logout = nv.getMenu().getItem(2);
  428. activeListing = nv.getMenu().getItem(3);
  429. completeListing = nv.getMenu().getItem(4);
  430. expiredListing = nv.getMenu().getItem(5);
  431. }
  432.  
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement