Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.17 KB | None | 0 0
  1. /*****main acitvity******/
  2. package com.digipodium.cloud_demo;
  3.  
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.support.design.widget.FloatingActionButton;
  8. import android.support.design.widget.Snackbar;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.support.v7.widget.Toolbar;
  11. import android.view.View;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import com.google.android.gms.tasks.OnCompleteListener;
  20. import com.google.android.gms.tasks.Task;
  21. import com.google.firebase.auth.AuthResult;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.auth.FirebaseUser;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  32. setSupportActionBar(toolbar);
  33. Button btnLogin=findViewById(R.id.btnlogin);
  34. TextView reg=findViewById(R.id.reg);
  35. final EditText id_pass = findViewById(R.id.id_pass);
  36. final EditText id_email=findViewById(R.id.id_email);
  37. final TextView tvError=findViewById(R.id.tvError);
  38. btnLogin.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. String email=id_email.getText().toString();
  42. String pass=id_pass.getText().toString();
  43. if(!email.isEmpty()&&email.contains("@")&&email.contains(".com")&&!pass.isEmpty()&&pass.length()>7)
  44. {
  45. FirebaseAuth fbase=FirebaseAuth.getInstance();
  46. fbase.signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  47. @Override
  48. public void onComplete(@NonNull Task<AuthResult> task) {
  49. if(task.getException()==null) {
  50. FirebaseUser user = task.getResult().getUser();
  51. updateUI(user);
  52. }
  53. else{try{
  54. String error=task.getException().getMessage();
  55. tvError.setText(error);
  56. updateUI(null);
  57. }
  58. catch(Exception e)
  59. {
  60. Toast.makeText(MainActivity.this, "ssss", Toast.LENGTH_SHORT).show();
  61. }}
  62. }
  63. });
  64. }
  65. else
  66. {
  67. tvError.setText("please enter valid email and password");
  68. }
  69. }
  70. });
  71.  
  72.  
  73.  
  74.  
  75. reg.setOnClickListener(new View.OnClickListener() {
  76. @Override
  77. public void onClick(View v) {
  78. startActivity(new Intent(MainActivity.this,register.class));
  79.  
  80. }
  81. });
  82.  
  83.  
  84. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  85. fab.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View view) {
  88. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  89. .setAction("Action", null).show();
  90. }
  91. });
  92. }
  93.  
  94. private void updateUI(FirebaseUser user) {
  95. //if user object is not null,use intend to take him to main activity
  96. //else do nothing
  97. if(user!=null)
  98. {
  99. startActivity(new Intent(this,HomeActivity.class));
  100. finish();//kill this activity
  101. }
  102. }
  103.  
  104. @Override
  105. protected void onStart() {
  106. super.onStart();
  107. FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
  108. updateUI(user);
  109. }
  110.  
  111. @Override
  112. public boolean onCreateOptionsMenu(Menu menu) {
  113. // Inflate the menu; this adds items to the action bar if it is present.
  114. getMenuInflater().inflate(R.menu.menu_main, menu);
  115. return true;
  116. }
  117.  
  118. @Override
  119. public boolean onOptionsItemSelected(MenuItem item) {
  120. // Handle action bar item clicks here. The action bar will
  121. // automatically handle clicks on the Home/Up button, so long
  122. // as you specify a parent activity in AndroidManifest.xml.
  123. int id = item.getItemId();
  124.  
  125. //noinspection SimplifiableIfStatement
  126. if (id == R.id.action_settings) {
  127. return true;
  128. }
  129.  
  130. return super.onOptionsItemSelected(item);
  131. }
  132. }
  133. /******************add activity************/
  134. package com.digipodium.cloud_demo;
  135.  
  136. import android.support.v7.app.AppCompatActivity;
  137. import android.os.Bundle;
  138. import android.view.View;
  139. import android.widget.Button;
  140. import android.widget.EditText;
  141.  
  142. import com.google.android.gms.tasks.OnSuccessListener;
  143. import com.google.firebase.database.DatabaseReference;
  144. import com.google.firebase.database.FirebaseDatabase;
  145.  
  146. public class AddActivity extends AppCompatActivity {
  147.  
  148. @Override
  149. protected void onCreate(Bundle savedInstanceState) {
  150. super.onCreate(savedInstanceState);
  151. setContentView(R.layout.activity_add);
  152. Button btnadd=findViewById(R.id.btnadd);
  153. final EditText etquote=findViewById(R.id.etquote);
  154. FirebaseDatabase db=FirebaseDatabase.getInstance();
  155. final DatabaseReference dref=db.getReference("quotes");
  156. btnadd.setOnClickListener(new View.OnClickListener() {
  157. @Override
  158. public void onClick(View v) {
  159. String myQuote=etquote.getText().toString();
  160. if(myQuote.isEmpty())
  161. {
  162. etquote.setError("Field cannot be empty");
  163. }
  164. else
  165. {
  166. Quote q=new Quote(
  167. myQuote,
  168. "annonymous",
  169. System.currentTimeMillis()//
  170. );
  171. dref.push().setValue(q).addOnSuccessListener(new OnSuccessListener<Void>() {
  172. @Override
  173. public void onSuccess(Void aVoid) {
  174. etquote.setText("");
  175. }
  176. });
  177.  
  178. }
  179. }
  180. });
  181.  
  182. }
  183. }
  184. //////********************home activity*********/
  185. package com.digipodium.cloud_demo;
  186.  
  187. import android.content.Intent;
  188. import android.os.Bundle;
  189. import android.support.annotation.NonNull;
  190. import android.support.design.widget.BottomNavigationView;
  191. import android.support.v7.app.AppCompatActivity;
  192. import android.support.v7.widget.RecyclerView;
  193. import android.view.MenuItem;
  194. import android.widget.TextView;
  195.  
  196. public class HomeActivity extends AppCompatActivity {
  197.  
  198. //private TextView mTextMessage;
  199.  
  200.  
  201. private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
  202. = new BottomNavigationView.OnNavigationItemSelectedListener() {
  203.  
  204. @Override
  205. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  206. switch (item.getItemId()) {
  207. case R.id.navigation_home:
  208. //faltu
  209. //mTextMessage.setText(R.string.title_home);
  210. return true;
  211. case R.id.navigation_add:
  212. startActivity(new Intent(HomeActivity.this,AddActivity.class));
  213. //mTextMessage.setText(R.string.title_dashboard);
  214. return true;
  215. }
  216. return false;
  217. }
  218. };
  219.  
  220. @Override
  221. protected void onCreate(Bundle savedInstanceState) {
  222. super.onCreate(savedInstanceState);
  223. setContentView(R.layout.activity_home);
  224.  
  225. //mTextMessage = (TextView) findViewById(R.id.message);
  226. BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  227. navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
  228. RecyclerView rvquotes=findViewById(R.id.RecyclerView);
  229. }
  230.  
  231. }
  232. ///////////*quote*******/
  233. package com.digipodium.cloud_demo;
  234.  
  235. public class Quote
  236. {
  237. String text;
  238. String user;
  239. long timestamp;
  240.  
  241. public Quote() {//firebase k liye
  242. }
  243.  
  244. public Quote(String text, String user, long timestamp) {//ctrl+shify+-
  245. this.text = text;
  246. this.user = user;
  247. this.timestamp = timestamp;
  248. }
  249.  
  250. public void setText(String text) {
  251. this.text = text;
  252. }
  253.  
  254. public void setUser(String user) {
  255. this.user = user;
  256. }
  257.  
  258. public void setTimestamp(long timestamp) {
  259. this.timestamp = timestamp;
  260. }
  261. }
  262. /////*register********/
  263. package com.digipodium.cloud_demo;
  264.  
  265. import android.content.Intent;
  266. import android.support.annotation.NonNull;
  267. import android.support.design.widget.TextInputEditText;
  268. import android.support.design.widget.TextInputLayout;
  269. import android.support.v7.app.AppCompatActivity;
  270. import android.os.Bundle;
  271. import android.view.View;
  272. import android.widget.Button;
  273. import android.widget.EditText;
  274. import android.widget.TextView;
  275.  
  276. import com.google.android.gms.tasks.OnCompleteListener;
  277. import com.google.android.gms.tasks.Task;
  278. import com.google.firebase.auth.AuthResult;
  279. import com.google.firebase.auth.FirebaseAuth;
  280. import com.google.firebase.auth.FirebaseUser;
  281.  
  282. public class register extends AppCompatActivity {
  283.  
  284. private EditText psswd;
  285.  
  286. @Override
  287. protected void onCreate(Bundle savedInstanceState) {
  288. super.onCreate(savedInstanceState);
  289. setContentView(R.layout.activity_register);
  290. final EditText email2 = findViewById(R.id.email2);
  291. psswd = findViewById(R.id.psswd);
  292. final TextInputEditText usrnme = findViewById(R.id.usrnme);
  293. Button regist = findViewById(R.id.regist);
  294. final TextView txtError = findViewById(R.id.txtError);
  295. regist.setOnClickListener(new View.OnClickListener() {
  296. @Override
  297. public void onClick(View v) {
  298. String email = email2.getText().toString();
  299. String pass = psswd.getText().toString();
  300. String username = usrnme.getText().toString();
  301. if (!email.isEmpty() && email.contains("@") && email.contains(".com") && !pass.isEmpty() && pass.length() > 7) {
  302. FirebaseAuth fbase = FirebaseAuth.getInstance();
  303. fbase.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  304. @Override
  305. public void onComplete(@NonNull Task<AuthResult> task) {
  306. if (task.getException() == null) {
  307. FirebaseUser user = task.getResult().getUser();
  308. updateUI(user);
  309. } else {
  310. try {
  311. String error = task.getException().getMessage();
  312. txtError.setText(error);
  313. updateUI(null);
  314. }
  315. catch (Exception e) {
  316.  
  317. }
  318. }
  319. }
  320. });
  321. } else {
  322. txtError.setText("please enter valid email and password");
  323. }
  324. }
  325. });
  326.  
  327. }
  328.  
  329. private void updateUI(FirebaseUser user) {
  330. if(user!=null)
  331. {
  332. startActivity(new Intent(this,HomeActivity.class));
  333. finish();//kill this activity
  334. }
  335.  
  336. }
  337. @Override
  338. protected void onStart() {
  339. super.onStart();
  340. FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
  341. updateUI(user);
  342. }
  343.  
  344. }
  345.  
  346. //*********activity_add.xml*******/
  347. <?xml version="1.0" encoding="utf-8"?>
  348. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  349. xmlns:app="http://schemas.android.com/apk/res-auto"
  350. xmlns:tools="http://schemas.android.com/tools"
  351. android:layout_width="match_parent"
  352. android:layout_height="match_parent"
  353. tools:context=".AddActivity">
  354.  
  355. <TextView
  356. android:id="@+id/tvtxt"
  357. android:layout_width="0dp"
  358. android:layout_height="0dp"
  359. android:layout_marginBottom="8dp"
  360. android:layout_marginEnd="8dp"
  361. android:layout_marginStart="8dp"
  362. android:layout_marginTop="16dp"
  363. android:text="Enter a Quote here"
  364. android:textSize="30sp"
  365. android:textStyle="bold|italic"
  366. app:layout_constraintBottom_toTopOf="@+id/etquote"
  367. app:layout_constraintEnd_toEndOf="parent"
  368. app:layout_constraintStart_toStartOf="parent"
  369. app:layout_constraintTop_toTopOf="parent" />
  370.  
  371. <EditText
  372. android:id="@+id/etquote"
  373. android:layout_width="0dp"
  374. android:layout_height="0dp"
  375. android:layout_marginBottom="68dp"
  376. android:layout_marginEnd="8dp"
  377. android:layout_marginStart="8dp"
  378. android:background="#0000"
  379. android:ems="10"
  380. android:gravity="top"
  381. android:hint="write anything ...."
  382. android:inputType="textMultiLine"
  383. android:lines="6"
  384. android:textSize="24sp"
  385. app:layout_constraintBottom_toTopOf="@+id/btnadd"
  386. app:layout_constraintEnd_toEndOf="parent"
  387. app:layout_constraintStart_toStartOf="parent"
  388. app:layout_constraintTop_toBottomOf="@+id/tvtxt" />
  389.  
  390. <Button
  391. android:id="@+id/btnadd"
  392. android:layout_width="wrap_content"
  393. android:layout_height="wrap_content"
  394. android:layout_marginBottom="93dp"
  395. android:layout_marginStart="131dp"
  396. android:text="Add Quote"
  397. app:layout_constraintBottom_toBottomOf="parent"
  398. app:layout_constraintStart_toStartOf="parent"
  399. app:layout_constraintTop_toBottomOf="@+id/etquote" />
  400. </android.support.constraint.ConstraintLayout>
  401. /***********activity_home**********/
  402. <?xml version="1.0" encoding="utf-8"?>
  403. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  404. xmlns:app="http://schemas.android.com/apk/res-auto"
  405. xmlns:tools="http://schemas.android.com/tools"
  406. android:id="@+id/container"
  407. android:layout_width="match_parent"
  408. android:layout_height="match_parent"
  409. tools:context=".HomeActivity">
  410.  
  411. <android.support.design.widget.BottomNavigationView
  412. android:id="@+id/navigation"
  413. android:layout_width="0dp"
  414. android:layout_height="wrap_content"
  415. android:layout_marginEnd="0dp"
  416. android:layout_marginStart="0dp"
  417. android:background="?android:attr/windowBackground"
  418. app:layout_constraintBottom_toBottomOf="parent"
  419. app:layout_constraintLeft_toLeftOf="parent"
  420. app:layout_constraintRight_toRightOf="parent"
  421. app:menu="@menu/navigation" />
  422.  
  423. <android.support.v7.widget.RecyclerView
  424. android:id="@+id/RecyclerView"
  425. android:layout_width="368dp"
  426. android:layout_height="439dp"
  427. tools:layout_editor_absoluteX="8dp"
  428. tools:layout_editor_absoluteY="8dp" />
  429.  
  430. </android.support.constraint.ConstraintLayout>
  431.  
  432.  
  433.  
  434. /********activity_main********/
  435. <?xml version="1.0" encoding="utf-8"?>
  436. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  437. xmlns:app="http://schemas.android.com/apk/res-auto"
  438. xmlns:tools="http://schemas.android.com/tools"
  439. android:layout_width="match_parent"
  440. android:layout_height="match_parent"
  441. tools:context=".MainActivity">
  442.  
  443. <android.support.design.widget.AppBarLayout
  444. android:layout_width="match_parent"
  445. android:layout_height="wrap_content"
  446. android:theme="@style/AppTheme.AppBarOverlay">
  447.  
  448. <android.support.v7.widget.Toolbar
  449. android:id="@+id/toolbar"
  450. android:layout_width="match_parent"
  451. android:layout_height="?attr/actionBarSize"
  452. android:background="?attr/colorPrimary"
  453. app:popupTheme="@style/AppTheme.PopupOverlay" />
  454.  
  455. </android.support.design.widget.AppBarLayout>
  456.  
  457. <include layout="@layout/content_main" />
  458.  
  459. <android.support.design.widget.FloatingActionButton
  460. android:id="@+id/fab"
  461. android:layout_width="wrap_content"
  462. android:layout_height="wrap_content"
  463. android:layout_gravity="bottom|end"
  464. android:layout_margin="@dimen/fab_margin"
  465. app:srcCompat="@android:drawable/ic_dialog_email" />
  466.  
  467. </android.support.design.widget.CoordinatorLayout>
  468. /*****activity_register******/
  469. <?xml version="1.0" encoding="utf-8"?>
  470. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  471. xmlns:app="http://schemas.android.com/apk/res-auto"
  472. xmlns:tools="http://schemas.android.com/tools"
  473. android:layout_width="match_parent"
  474. android:layout_height="match_parent"
  475. tools:context=".register">
  476.  
  477. <android.support.design.widget.TextInputLayout
  478. android:id="@+id/textInputLayout"
  479. android:layout_width="0dp"
  480. android:layout_height="wrap_content"
  481. android:layout_marginEnd="8dp"
  482. android:layout_marginStart="8dp"
  483. android:layout_marginTop="40dp"
  484. app:layout_constraintEnd_toEndOf="parent"
  485. app:layout_constraintStart_toStartOf="parent"
  486. app:layout_constraintTop_toBottomOf="@+id/psswd">
  487.  
  488. <android.support.design.widget.TextInputEditText
  489. android:id="@+id/usrnme"
  490. android:layout_width="match_parent"
  491. android:layout_height="wrap_content"
  492. android:hint="Username" />
  493. </android.support.design.widget.TextInputLayout>
  494.  
  495. <Button
  496. android:id="@+id/regist"
  497. android:layout_width="wrap_content"
  498. android:layout_height="wrap_content"
  499. android:layout_marginStart="116dp"
  500. android:layout_marginTop="65dp"
  501. android:text="Register"
  502. app:layout_constraintStart_toStartOf="@+id/textInputLayout"
  503. app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />
  504.  
  505. <EditText
  506. android:id="@+id/psswd"
  507. android:layout_width="0dp"
  508. android:layout_height="wrap_content"
  509. android:layout_marginEnd="8dp"
  510. android:layout_marginStart="8dp"
  511. android:layout_marginTop="110dp"
  512. android:ems="10"
  513. android:hint="Password"
  514. android:inputType="textPassword"
  515. app:layout_constraintEnd_toEndOf="parent"
  516. app:layout_constraintStart_toStartOf="parent"
  517. app:layout_constraintTop_toTopOf="parent" />
  518.  
  519. <EditText
  520. android:id="@+id/email2"
  521. android:layout_width="0dp"
  522. android:layout_height="wrap_content"
  523. android:layout_marginBottom="199dp"
  524. android:layout_marginEnd="8dp"
  525. android:layout_marginStart="8dp"
  526. android:layout_marginTop="20dp"
  527. android:ems="10"
  528. android:hint="email"
  529. android:inputType="textEmailAddress"
  530. app:layout_constraintBottom_toTopOf="@+id/txtError"
  531. app:layout_constraintEnd_toEndOf="parent"
  532. app:layout_constraintStart_toStartOf="parent"
  533. app:layout_constraintTop_toTopOf="parent" />
  534.  
  535. <TextView
  536. android:id="@+id/txtError"
  537. android:layout_width="0dp"
  538. android:layout_height="0dp"
  539. android:layout_marginBottom="218dp"
  540. android:layout_marginEnd="8dp"
  541. android:layout_marginStart="8dp"
  542. app:layout_constraintBottom_toBottomOf="parent"
  543. app:layout_constraintEnd_toEndOf="parent"
  544. app:layout_constraintStart_toStartOf="parent"
  545. app:layout_constraintTop_toBottomOf="@+id/email2" />
  546. </android.support.constraint.ConstraintLayout>
  547. /********content_main.xml****/
  548. <?xml version="1.0" encoding="utf-8"?>
  549. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  550. xmlns:app="http://schemas.android.com/apk/res-auto"
  551. xmlns:tools="http://schemas.android.com/tools"
  552. android:layout_width="match_parent"
  553. android:layout_height="match_parent"
  554. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  555. tools:context=".MainActivity"
  556. tools:showIn="@layout/activity_main">
  557.  
  558. <Button
  559. android:id="@+id/btnlogin"
  560. android:layout_width="0dp"
  561. android:layout_height="wrap_content"
  562. android:layout_marginEnd="47dp"
  563. android:layout_marginStart="16dp"
  564. android:text="Login"
  565. app:layout_constraintBaseline_toBaselineOf="@+id/reg"
  566. app:layout_constraintEnd_toStartOf="@+id/reg"
  567. app:layout_constraintStart_toStartOf="parent" />
  568.  
  569. <TextView
  570. android:id="@+id/reg"
  571. android:layout_width="0dp"
  572. android:layout_height="43dp"
  573. android:layout_marginEnd="70dp"
  574. android:text="Register"
  575. android:textSize="24sp"
  576. app:layout_constraintBottom_toBottomOf="parent"
  577. app:layout_constraintEnd_toEndOf="parent"
  578. app:layout_constraintStart_toEndOf="@+id/btnlogin"
  579. app:layout_constraintTop_toTopOf="parent" />
  580.  
  581. <EditText
  582. android:id="@+id/id_email"
  583. android:layout_width="0dp"
  584. android:layout_height="0dp"
  585. android:layout_marginBottom="117dp"
  586. android:layout_marginEnd="8dp"
  587. android:layout_marginStart="8dp"
  588. android:layout_marginTop="16dp"
  589. android:ems="10"
  590. android:hint="email"
  591. android:inputType="textEmailAddress"
  592. app:layout_constraintBottom_toTopOf="@+id/tvError"
  593. app:layout_constraintEnd_toEndOf="parent"
  594. app:layout_constraintStart_toStartOf="parent"
  595. app:layout_constraintTop_toTopOf="parent" />
  596.  
  597. <EditText
  598. android:id="@+id/id_pass"
  599. android:layout_width="0dp"
  600. android:layout_height="56dp"
  601. android:layout_marginEnd="8dp"
  602. android:layout_marginStart="8dp"
  603. android:layout_marginTop="56dp"
  604. android:ems="10"
  605. android:hint="Password"
  606. android:inputType="textPassword"
  607. app:layout_constraintEnd_toEndOf="parent"
  608. app:layout_constraintHorizontal_bias="0.0"
  609. app:layout_constraintStart_toStartOf="parent"
  610. app:layout_constraintTop_toBottomOf="@+id/id_email" />
  611.  
  612. <TextView
  613. android:id="@+id/tvError"
  614. android:layout_width="0dp"
  615. android:layout_height="0dp"
  616. android:layout_marginBottom="288dp"
  617. android:layout_marginEnd="8dp"
  618. android:layout_marginStart="8dp"
  619. app:layout_constraintBottom_toBottomOf="parent"
  620. app:layout_constraintEnd_toEndOf="parent"
  621. app:layout_constraintStart_toStartOf="parent"
  622. app:layout_constraintTop_toBottomOf="@+id/id_email" />
  623.  
  624. </android.support.constraint.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement