Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
13 kB
8
Indexable
Never
***main xml***
<?xml version="1.0" encoding="utf-8"?>                                                   
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/userdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
         />
    <Button
        android:text="@string/logout"
        android:id="@+id/logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

***main java***
package com.example.myapplication;

public class MainActivity extends AppCompatActivity {

    FirebaseAuth auth;
    Button button;
    TextView textview;
    FirebaseUser user;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        auth = FirebaseAuth.getInstance();
        button=findViewById(R.id.userdetails);
        user=auth.getCurrentUser();
        if(user==null){
            Intent intent=new Intent(getApplicationContext(),signin.class);
            startActivity(intent);
            finish();
        }
        else{
            textview.setText(user.getEmail());
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirebaseAuth.getInstance().signOut();
                Intent intent=new Intent(getApplicationContext(),signin.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
****signup xml***
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="15dp"
    tools:context=".signup">

  <TextView
      android:text="@string/signup"
      android:textSize="25sp"
      android:textStyle="bold"
      android:gravity="center"
      android:layout_marginBottom="20sp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

  <com.google.android.material.textfield.TextInputLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/email"
        android:hint="@string/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="InvalidId" />
  </com.google.android.material.textfield.TextInputLayout>

  <com.google.android.material.textfield.TextInputLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/password"
        android:hint="@string/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="InvalidId" />
  </com.google.android.material.textfield.TextInputLayout>
  <ProgressBar
      android:id="@+id/progressbar"
      android:visibility="gone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <Button
      android:id="@+id/btn_signup"
      android:text="@string/signup"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <TextView
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:id="@+id/signupnow"
    android:text="@string/clicktosignup"

      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>
***signup java***
package com.example.myapplication;

public class signup extends AppCompatActivity {
    TextInputEditText editTextEmail,editTextPassword;
    Button buttonReg;
    FirebaseAuth mAuth;
    ProgressBar progressBar;

    TextView textview;
    @Override
    public void onStart(){
        super.onStart();
        FirebaseUser curuser=mAuth.getCurrentUser();
        if(curuser!=null){
            Intent intent=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        mAuth=FirebaseAuth.getInstance();
        editTextEmail=findViewById(R.id.email);
        editTextPassword=findViewById(R.id.password);
        buttonReg=findViewById(R.id.btn_signup);
        progressBar=findViewById(R.id.progressbar);
        textview=findViewById(R.id.signupnow);
        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent=new Intent(getApplicationContext(),signin.class);
              startActivity(intent);
              finish();
            }
        });


        buttonReg.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                progressBar.setVisibility(View.VISIBLE);
                String email,password;
                email=String.valueOf(editTextEmail.getText());
                password=String.valueOf(editTextPassword.getText());

                if(TextUtils.isEmpty(email)){
                    Toast.makeText(signup.this,"enter email",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(signup.this,"enter password",Toast.LENGTH_SHORT).show();
                    return;
                }


                mAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            private Task<AuthResult> task;
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                this.task = task;
                                progressBar.setVisibility((View.GONE));
                                if (task.isSuccessful()) {

                                    Toast.makeText(signup.this, "Account created.",
                                            Toast.LENGTH_SHORT).show();

                                } else {
                                    Toast.makeText(signup.this, "Authentication failed.",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            }
        });
    }
}
***signin xml***
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="15dp"
    tools:context=".signin">

  <TextView
      android:text="@string/signin"
      android:textSize="25sp"
      android:textStyle="bold"
      android:gravity="center"
      android:layout_marginBottom="20sp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

  <com.google.android.material.textfield.TextInputLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/email"
        android:hint="@string/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="InvalidId" />
  </com.google.android.material.textfield.TextInputLayout>

  <com.google.android.material.textfield.TextInputLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/password"
        android:hint="@string/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="InvalidId" />
  </com.google.android.material.textfield.TextInputLayout>
  <ProgressBar
      android:id="@+id/progressbar"
      android:visibility="gone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <Button
      android:id="@+id/btn_signin"
      android:text="SignIn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <TextView
      android:textStyle="bold"
      android:textSize="20sp"
      android:layout_gravity="center"
      android:layout_marginTop="20dp"
      android:id="@+id/signinnow"
      android:text="@string/click_to_signin"

      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
  </LinearLayout>
***signin java***
package com.example.myapplication;

public class signin extends AppCompatActivity {
    TextInputEditText editTextEmail,editTextPassword;
    Button buttonSignin;
    FirebaseAuth mAuth;
    ProgressBar progressBar;
    TextView textview;
    @Override
    public void onStart(){
        super.onStart();
        FirebaseUser curuser=mAuth.getCurrentUser();
        if(curuser!=null){
            Intent intent=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        mAuth=FirebaseAuth.getInstance();
        editTextEmail=findViewById(R.id.email);
        editTextPassword=findViewById(R.id.password);
        buttonSignin=findViewById(R.id.btn_signin);
        progressBar=findViewById(R.id.progressbar);
        textview=findViewById(R.id.signinnow);
        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), signup.class);
                startActivity(intent);
                finish();
            }
        });

        buttonSignin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar.setVisibility(View.VISIBLE);
                String email,password;
                email=String.valueOf(editTextEmail.getText());
                password=String.valueOf(editTextPassword.getText());

                if(TextUtils.isEmpty(email)){
                    Toast.makeText(signin.this,"enter email",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(signin.this,"enter password",Toast.LENGTH_SHORT).show();
                    return;
                }
                mAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            private Task<AuthResult> task;
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                this.task = task;
                                progressBar.setVisibility((View.GONE));
                                if (task.isSuccessful()) {

                                    Toast.makeText(signin.this, "Sign in successful.",
                                            Toast.LENGTH_SHORT).show();
                                    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                                    startActivity(intent);
                                    finish();

                                } else {
                                    Toast.makeText(signin.this, "Authentication failed.",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

            }
        });
    }
}