Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****main acitvity******/
- package com.digipodium.cloud_demo;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- Button btnLogin=findViewById(R.id.btnlogin);
- TextView reg=findViewById(R.id.reg);
- final EditText id_pass = findViewById(R.id.id_pass);
- final EditText id_email=findViewById(R.id.id_email);
- final TextView tvError=findViewById(R.id.tvError);
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String email=id_email.getText().toString();
- String pass=id_pass.getText().toString();
- if(!email.isEmpty()&&email.contains("@")&&email.contains(".com")&&!pass.isEmpty()&&pass.length()>7)
- {
- FirebaseAuth fbase=FirebaseAuth.getInstance();
- fbase.signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if(task.getException()==null) {
- FirebaseUser user = task.getResult().getUser();
- updateUI(user);
- }
- else{try{
- String error=task.getException().getMessage();
- tvError.setText(error);
- updateUI(null);
- }
- catch(Exception e)
- {
- Toast.makeText(MainActivity.this, "ssss", Toast.LENGTH_SHORT).show();
- }}
- }
- });
- }
- else
- {
- tvError.setText("please enter valid email and password");
- }
- }
- });
- reg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(MainActivity.this,register.class));
- }
- });
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- }
- private void updateUI(FirebaseUser user) {
- //if user object is not null,use intend to take him to main activity
- //else do nothing
- if(user!=null)
- {
- startActivity(new Intent(this,HomeActivity.class));
- finish();//kill this activity
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
- updateUI(user);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- /******************add activity************/
- package com.digipodium.cloud_demo;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- public class AddActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_add);
- Button btnadd=findViewById(R.id.btnadd);
- final EditText etquote=findViewById(R.id.etquote);
- FirebaseDatabase db=FirebaseDatabase.getInstance();
- final DatabaseReference dref=db.getReference("quotes");
- btnadd.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String myQuote=etquote.getText().toString();
- if(myQuote.isEmpty())
- {
- etquote.setError("Field cannot be empty");
- }
- else
- {
- Quote q=new Quote(
- myQuote,
- "annonymous",
- System.currentTimeMillis()//
- );
- dref.push().setValue(q).addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- etquote.setText("");
- }
- });
- }
- }
- });
- }
- }
- //////********************home activity*********/
- package com.digipodium.cloud_demo;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.design.widget.BottomNavigationView;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.RecyclerView;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class HomeActivity extends AppCompatActivity {
- //private TextView mTextMessage;
- private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
- = new BottomNavigationView.OnNavigationItemSelectedListener() {
- @Override
- public boolean onNavigationItemSelected(@NonNull MenuItem item) {
- switch (item.getItemId()) {
- case R.id.navigation_home:
- //faltu
- //mTextMessage.setText(R.string.title_home);
- return true;
- case R.id.navigation_add:
- startActivity(new Intent(HomeActivity.this,AddActivity.class));
- //mTextMessage.setText(R.string.title_dashboard);
- return true;
- }
- return false;
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
- //mTextMessage = (TextView) findViewById(R.id.message);
- BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
- navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
- RecyclerView rvquotes=findViewById(R.id.RecyclerView);
- }
- }
- ///////////*quote*******/
- package com.digipodium.cloud_demo;
- public class Quote
- {
- String text;
- String user;
- long timestamp;
- public Quote() {//firebase k liye
- }
- public Quote(String text, String user, long timestamp) {//ctrl+shify+-
- this.text = text;
- this.user = user;
- this.timestamp = timestamp;
- }
- public void setText(String text) {
- this.text = text;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public void setTimestamp(long timestamp) {
- this.timestamp = timestamp;
- }
- }
- /////*register********/
- package com.digipodium.cloud_demo;
- import android.content.Intent;
- import android.support.annotation.NonNull;
- import android.support.design.widget.TextInputEditText;
- import android.support.design.widget.TextInputLayout;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- public class register extends AppCompatActivity {
- private EditText psswd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- final EditText email2 = findViewById(R.id.email2);
- psswd = findViewById(R.id.psswd);
- final TextInputEditText usrnme = findViewById(R.id.usrnme);
- Button regist = findViewById(R.id.regist);
- final TextView txtError = findViewById(R.id.txtError);
- regist.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String email = email2.getText().toString();
- String pass = psswd.getText().toString();
- String username = usrnme.getText().toString();
- if (!email.isEmpty() && email.contains("@") && email.contains(".com") && !pass.isEmpty() && pass.length() > 7) {
- FirebaseAuth fbase = FirebaseAuth.getInstance();
- fbase.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.getException() == null) {
- FirebaseUser user = task.getResult().getUser();
- updateUI(user);
- } else {
- try {
- String error = task.getException().getMessage();
- txtError.setText(error);
- updateUI(null);
- }
- catch (Exception e) {
- }
- }
- }
- });
- } else {
- txtError.setText("please enter valid email and password");
- }
- }
- });
- }
- private void updateUI(FirebaseUser user) {
- if(user!=null)
- {
- startActivity(new Intent(this,HomeActivity.class));
- finish();//kill this activity
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
- updateUI(user);
- }
- }
- //*********activity_add.xml*******/
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".AddActivity">
- <TextView
- android:id="@+id/tvtxt"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="16dp"
- android:text="Enter a Quote here"
- android:textSize="30sp"
- android:textStyle="bold|italic"
- app:layout_constraintBottom_toTopOf="@+id/etquote"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/etquote"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginBottom="68dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:background="#0000"
- android:ems="10"
- android:gravity="top"
- android:hint="write anything ...."
- android:inputType="textMultiLine"
- android:lines="6"
- android:textSize="24sp"
- app:layout_constraintBottom_toTopOf="@+id/btnadd"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/tvtxt" />
- <Button
- android:id="@+id/btnadd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="93dp"
- android:layout_marginStart="131dp"
- android:text="Add Quote"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etquote" />
- </android.support.constraint.ConstraintLayout>
- /***********activity_home**********/
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".HomeActivity">
- <android.support.design.widget.BottomNavigationView
- android:id="@+id/navigation"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="0dp"
- android:layout_marginStart="0dp"
- android:background="?android:attr/windowBackground"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:menu="@menu/navigation" />
- <android.support.v7.widget.RecyclerView
- android:id="@+id/RecyclerView"
- android:layout_width="368dp"
- android:layout_height="439dp"
- tools:layout_editor_absoluteX="8dp"
- tools:layout_editor_absoluteY="8dp" />
- </android.support.constraint.ConstraintLayout>
- /********activity_main********/
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- <android.support.design.widget.FloatingActionButton
- android:id="@+id/fab"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom|end"
- android:layout_margin="@dimen/fab_margin"
- app:srcCompat="@android:drawable/ic_dialog_email" />
- </android.support.design.widget.CoordinatorLayout>
- /*****activity_register******/
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".register">
- <android.support.design.widget.TextInputLayout
- android:id="@+id/textInputLayout"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="40dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/psswd">
- <android.support.design.widget.TextInputEditText
- android:id="@+id/usrnme"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Username" />
- </android.support.design.widget.TextInputLayout>
- <Button
- android:id="@+id/regist"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="116dp"
- android:layout_marginTop="65dp"
- android:text="Register"
- app:layout_constraintStart_toStartOf="@+id/textInputLayout"
- app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />
- <EditText
- android:id="@+id/psswd"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="110dp"
- android:ems="10"
- android:hint="Password"
- android:inputType="textPassword"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/email2"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginBottom="199dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="20dp"
- android:ems="10"
- android:hint="email"
- android:inputType="textEmailAddress"
- app:layout_constraintBottom_toTopOf="@+id/txtError"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/txtError"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginBottom="218dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/email2" />
- </android.support.constraint.ConstraintLayout>
- /********content_main.xml****/
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context=".MainActivity"
- tools:showIn="@layout/activity_main">
- <Button
- android:id="@+id/btnlogin"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="47dp"
- android:layout_marginStart="16dp"
- android:text="Login"
- app:layout_constraintBaseline_toBaselineOf="@+id/reg"
- app:layout_constraintEnd_toStartOf="@+id/reg"
- app:layout_constraintStart_toStartOf="parent" />
- <TextView
- android:id="@+id/reg"
- android:layout_width="0dp"
- android:layout_height="43dp"
- android:layout_marginEnd="70dp"
- android:text="Register"
- android:textSize="24sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/btnlogin"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/id_email"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginBottom="117dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="16dp"
- android:ems="10"
- android:hint="email"
- android:inputType="textEmailAddress"
- app:layout_constraintBottom_toTopOf="@+id/tvError"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/id_pass"
- android:layout_width="0dp"
- android:layout_height="56dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="56dp"
- android:ems="10"
- android:hint="Password"
- android:inputType="textPassword"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/id_email" />
- <TextView
- android:id="@+id/tvError"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginBottom="288dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/id_email" />
- </android.support.constraint.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement