Membuat Login Methode SignIn Google Tipd Android Studio Java Authentifikasi Firebase.

Pembuatan Aplikasi untuk halaman tampilan acyivity login dan register user nya mengunakan merthode sign in google atau penggunaan akun google. bagaimana cara nya om..? Dari temans yang kebetulan ingin belajar membuat aplikasi di android studio.

Saat ini perkembangan login via google Sign atau akun google untuk tutorial saat ini dari abjo, semoga terbantu bagi temans temans yang belajar android studio, semoga bermanfaat.

Membangun aplikasi untuk user atau pengguna aplikasi sebagai fitur login mengunakan java. Dimana hanya tinggal tombol login dengan simbol google atau register via akun google yang ada di android kita dengan akun google. step by step akan kita share di sini tutorial untuk android studio.

Go Kita langsung oprek oprek ke android studio. Buka flatform android studio dan buatlah project baru sesuai selera kita untuk nama aplikasi nya.

Langkah Membangun Halaman Activity Login Sign Google Disini.

Step 1

Koneksikan Apps yang kita bangun dengan Firebase akun google. Pada link google browser, ketik Firebase dan login via emai akun google anda, Langkah langkah dapat di pelajari disini bagi pemula.

Implementasika dan plugin kode di Build Grade dan Isi kodenya sesuai Versi ini dibangun melalui Android Studio Version Girafle.

kode di Build Gradle (module : app) 

plugins {
alias(libs.plugins.androidApplication)
id("com.google.gms.google-services")

Implementation "

implementation(libs.firebase.auth)
implementation(libs.firebase.database)
implementation(libs.firebase.storage)

kode di Build Gradle (project : nama project anda )

buildscript {
dependencies {
classpath(libs.google.services)
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.androidApplication) apply false

Step 3.

Kunjungi Firebase Akun anda lalu aktifkan methode sign in authentikasi dan sinkronkan dengan App anda dengan langkah langkah sbb :

1. Aktifkan Firebase Authentifikasi untuk metode sign in,

Pilih Google dan aktifkan.


2. Buat Penandatanganan App dengan Memasukan Kode SHI, yang Ada Di android studio,

Caranya :

Pilih Gradle, Ketik signingreport , lalu enter 

Pilih kode pada yang tertera Pada tampilan SHI dan Copy.


Pilih yang bertanda keterangan SHI, di Copy lalu di daftarkan pada Firebase Nantinya

Lanjutkan pada Akun Firebase pelajari di materi berikut tentang mendaftarkan kode SHI, bagi yang udah tahu caranya kita langsung saja kepada kodungannya ya,

Kita langsung saja kepada codingannya, step by step membangun halaman

Buat Halaman Flaschreen Activity

XML nya

<androidx.constraintlayout.widget.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/main"
android:background="@color/birusafir"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Flaschreen">

<ImageView
android:id="@+id/loloimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:foregroundGravity="center_horizontal"
android:src="@drawable/logosakatouji"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.263" />

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fontFamily="sans-serif-medium"
android:text="Selamat Datang"
android:textAlignment="center"
android:textColor="#FAED7C"
android:textSize="28sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loloimage" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java nya

package com.rimbocepo88.testlogin;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Flaschreen extends AppCompatActivity {
private ImageView logoImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_flaschreen);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// Inisialisasi ImageView
logoImageView = findViewById(R.id.loloimage);

// Memuat animasi
Animation animation = AnimationUtils.loadAnimation(this, R.anim.zoomoutbos);

// Menjalankan animasi
logoImageView.startAnimation(animation);

// Menunda perpindahan ke halaman berikutnya selama 3 detik
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Membuka halaman berikutnya
Intent intent = new Intent(Flaschreen.this, Login.class);
startActivity(intent);
finish();
}
}, 3000);

}
}

Buat Kode untuk Animasi.

Cara Mendapatkan animasi buat Direktori baru dengan nama anim, dan beri kode 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="3000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.10"
android:toYScale="1.10"
android:repeatMode="reverse"
android:repeatCount="infinite">
</scale>
</set>

Buat Halaman LoginSignin Activity

XML nya 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginSignin">

<ImageView
android:id="@+id/back2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/bgroundapppn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="275dp"
android:layout_height="420dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="@+id/back2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498">


<ImageView
android:layout_width="157dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:src="@drawable/baseline_airport_shuttle_24" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="cursive"
android:textColor="@color/white"
android:text="SELAMAT DATANG"
android:textAlignment="center"
android:textSize="30sp" />

<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Silahkan Via Akun Google"
android:textColor="@color/white" />

<com.google.android.gms.common.SignInButton
android:id="@+id/SigninGoogle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Java nya pada Login

package com.rimbocepo88.testlogin;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.database.FirebaseDatabase;
import com.rimbocepo88.testlogin.Model.Users;
import com.rimbocepo88.testlogin.MainActivity;

import java.util.Objects;


public class LoginSignin extends AppCompatActivity {
SignInButton btSignIn;

TextView prim, disb;
Button button;
GoogleSignInClient googleSignInClient;
FirebaseAuth firebaseAuth;
FirebaseDatabase database;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login_signin);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
btSignIn = findViewById(R.id.SigninGoogle);

// Initialize sign in client
        googleSignInClient = GoogleSignIn.getClient(LoginSignin.this, googleSignInOptions);

btSignIn.setOnClickListener((View.OnClickListener) view -> {
Intent intent = googleSignInClient.getSignInIntent();
// Start activity for result
startActivityForResult(intent, 100);
});

// Initialize firebase auth
firebaseAuth = FirebaseAuth.getInstance();
// Initialize firebase database
database = FirebaseDatabase.getInstance();
// Initialize firebase user
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
// Check condition
if (firebaseUser != null) {
startActivity(new Intent(LoginSignin.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
}

}
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken,null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
FirebaseUser user = firebaseAuth.getCurrentUser();
//logic
//extracting the user information from google Api.
Users current_user = new Users();
current_user.setId(Objects.requireNonNull(user).getUid());
current_user.setEmail(user.getEmail());
current_user.setName(user.getDisplayName());
current_user.setProfilePic(Objects.requireNonNull(user.getPhotoUrl()).toString());
// user.getMetadata().
database.getReference().child("USERS").child(user.getUid()).setValue(current_user);

Intent intent = new Intent(LoginSignin.this, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(LoginSignin.this,"Success with Google",Toast.LENGTH_SHORT)
.show();
}else{
Toast.makeText(LoginSignin.this,task.getException().getMessage(),Toast.LENGTH_SHORT)
.show();

}

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check condition
if (requestCode == 100) {
// When request code is equal to 100 initialize task
Task<GoogleSignInAccount> signInAccountTask = GoogleSignIn.getSignedInAccountFromIntent(data);
// check condition
if (signInAccountTask.isSuccessful()) {
// When google sign in successful initialize string
String s = "Google sign in successful";
// Display Toast
displayToast(s);
// Initialize sign in account
try {
// Initialize sign in account
GoogleSignInAccount googleSignInAccount = signInAccountTask.getResult(ApiException.class);
firebaseAuthWithGoogle(googleSignInAccount.getIdToken());
// Check condition
if (googleSignInAccount != null) {
// When sign in account is not equal to null initialize auth credential
AuthCredential authCredential = GoogleAuthProvider.getCredential(googleSignInAccount.getIdToken(), null);
// Check credential
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// Check condition
if (task.isSuccessful()) {
// When task is successful redirect to profile activity display Toast
startActivity(new Intent(LoginSignin.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
displayToast("authentication successful");
} else {
// When task is unsuccessful display Toast
displayToast("Authentication Failed :" + task.getException().getMessage());
}
}
});
}
} catch (ApiException e) {
e.printStackTrace();
}
}
}
}

private void displayToast(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}

Buat Model Java Activity User

package com.rimbocepo88.testlogin.Model;

public class Users {
private String profilePic,Name,Email,Id,Password,status;

public Users(String profilePic, String name, String id, String password, String email, String status) {
this.profilePic = profilePic;
Name = name;
this.Email = email;
Id = id;
Password = password;
this.status = status;

}

public Users() {
}

public Users(String name, String password, String email){
this.Name = name;
this.Email = email;
this.Password = password;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getProfilePic() {
return profilePic;
}

public void setProfilePic(String profilePic) {
this.profilePic = profilePic;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getEmail() {
return Email;
}

public void setEmail(String email) {
Email = email;
}

public String getId() {
return Id;
}

public void setId(String id) {
Id = id;
}

public String getPassword() {
return Password;
}

public void setPassword(String password) {
Password = password;
}

}

Isi Halaman Main Activity

<androidx.constraintlayout.widget.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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvadmin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Ibu Kos"
android:textSize="28sp" />

<ImageView
android:id="@+id/ivKeluar"
android:layout_width="50dp"
android:background="@color/black"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_margin="20dp"
android:src="@drawable/round_apps_24"/>

</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Java MainActivity nya

package com.rimbocepo88.sakatotrip.Admin.User;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rimbocepo88.sakatotrip.Admin.Tagihan.TagihannyaGuys;
import com.rimbocepo88.sakatotrip.AksesLogReg.Login;
import com.rimbocepo88.sakatotrip.AksesLogReg.LoginCrew;
import com.rimbocepo88.sakatotrip.MainActivity;
import com.rimbocepo88.sakatotrip.Model.User;
import com.rimbocepo88.sakatotrip.R;
import com.rimbocepo88.sakatotrip.User.BerandaMulai;

import java.util.ArrayList;

public class AdminRoom extends AppCompatActivity {
private ImageView ivKeluar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_admin_room);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
ivKeluar = findViewById(R.id.ivKeluar);

ivKeluar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
auth.signOut();
Intent returnIntent = new Intent(MainActivity.this, Splasscren.class);
startActivity(returnIntent);
finish();
}
});

}

}

Sebelum di Run untuk uji Google SignIn sangat di perlukan API SignIn konfigurasi dari Google jika uji login google SignIn di lakukan Uji aplikasi secara Release di apk. ya,,, saat di Apk Debug ga masalah, masalah nga bisa aksesnya adalah saat apk anda pada posisi release dan perlu mendapatkan penenda tanganan atau mendaftarkan SHI dari Google ke Firebase anda. Mengenai Tips tersebut akan kita share pada materi berikutnya.  

Nah demikianlah codingan untuk metode login akun google SignIn membangun aplikasi di android studio koneksi firebase, Semoga bermanfaat.

0 Komentar

Terbaru