Open MUHOOZId opened 2 days ago
To implement Google sign-in functionality in your Android application, you need to make changes to both the front end and back end of your app. This involves updating the layout to include a Google sign-in button, configuring the necessary dependencies and permissions, and implementing the sign-in logic in your main activity. The goal is to allow users to sign in using their Google accounts, which enhances user experience by providing a quick and secure authentication method.
Update the Layout File (activity_main.xml
):
<com.google.android.gms.common.SignInButton
android:id="@+id/googleSignInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
Modify the Build Configuration (build.gradle.kts
):
build.gradle.kts
file to enable Google sign-in features.dependencies {
implementation("com.google.android.gms:play-services-auth:20.4.1")
}
Update the Android Manifest (AndroidManifest.xml
):
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
"YOUR_API_KEY_HERE"
with your actual Google API key.Implement Sign-In Logic in MainActivity.kt
:
GoogleSignInClient
with the appropriate options in the onCreate
method.onClickListener
for the Google sign-in button to trigger the sign-in intent.onActivityResult
method.import android.content.Intent
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
class MainActivity : AppCompatActivity() {
private lateinit var googleSignInClient: GoogleSignInClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
val googleSignInButton = findViewById<SignInButton>(R.id.googleSignInButton)
googleSignInButton.setOnClickListener {
signIn()
}
}
private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)
updateUI(account)
} catch (e: ApiException) {
updateUI(null)
}
}
}
private fun updateUI(account: GoogleSignInAccount?) {
if (account != null) {
// User is signed in
} else {
// User is not signed in
}
}
companion object {
private const val RC_SIGN_IN = 9001
}
}
By following these steps, you will have successfully implemented Google sign-in functionality in your Android application, allowing users to authenticate using their Google accounts.
Click here to create a Pull Request with the proposed solution
Files used for this task:
What - description of what you me to do Example: Hey @autopilot implement a Google sign-in on my website. Make changes to the front end and the back end of the application
Why - explain why this is important Example: I want to allow users to signup and login using their Google account