Site icon C1CTech

Implementing Google Sign-In in Android App

&NewLine;<p>This post is about how to authenticate users via Google Sign-in using the Google sign-in API&comma; how to extract basic profile details like displayName&comma; email&comma; etc&period;&comma; and add signOut functionality in Android with the help of a simple demo app&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;060255&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;GoogleLoginExp"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<h4> <&sol;h4> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"7Zs4nawVIA4" title&equals;"Implementing Google Sign-In in Android App"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;7Zs4nawVIA4"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;7Zs4nawVIA4&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Implementing Google Sign-In in Android App"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Configuring a Google API Console project<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>Firstly&comma; we must configure the google API console account before configuring a Google API Console project&period;<&sol;p> &NewLine;<ul> &NewLine;<li> &NewLine;<p>To configure a Google API Console project&comma; u<span style&equals;"font-size&colon; 16px&semi;">se this <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;identity&sol;sign-in&sol;android&sol;start-integrating&num;configure&lowbar;a&lowbar;project">link<&sol;a><&sol;strong><&sol;span><span style&equals;"font-size&colon; 16px&semi;"> and click on<&sol;span><strong style&equals;"font-size&colon; 16px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"> &&num;8220&semi;Configure a project&&num;8221&semi;&period;<&sol;span><&sol;strong><span style&equals;"font-size&colon; 16px&semi;"> Enter the project name&comma; select <strong><span style&equals;"color&colon; &num;008000&semi;">Android<&sol;span><&sol;strong>&comma; enter the package name and SHA-1 certificate&period;<&sol;span><&sol;p> &NewLine;<&sol;li> &NewLine;<li>To generate the SHA1 follow the below steps&colon;<br &sol;>1&period; Click <span style&equals;"color&colon; &num;008000&semi;"><strong>Gradle<&sol;strong><&sol;span> in right side panel then navigate&colon; <strong><span style&equals;"color&colon; &num;0000ff&semi;">app &gt&semi; Tasks &gt&semi; android <&sol;span><&sol;strong><br &sol;>2&period; Double Click on <strong><span style&equals;"color&colon; &num;0000ff&semi;">signingReport<&sol;span><&sol;strong> &lpar;You will get SHA1 and MD5 in Run Bar &lpar;Sometimes it will be in Gradle Console&rpar;&rpar;&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating new project<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>1 &period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Android Project<&sol;strong>&comma;<&sol;span> select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty<&sol;strong><&sol;span> Activity&comma; provide app <span style&equals;"color&colon; &num;008000&semi;"><strong>name<&sol;strong><&sol;span>&comma; select language to <span style&equals;"color&colon; &num;008000&semi;"><strong>kotlin<&sol;strong><&sol;span> and then finally click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>2 &period; In your project’s top-level build&period;gradle file&comma; ensure that Google&&num;8217&semi;s Maven repository is included&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>buildscript &lbrace;<br &sol;> repositories &lbrace;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> google&lpar;&rpar;<&sol;strong><&sol;span><br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>3 &period; Open app-level build&period;gradle file and add the below library under dependencies section&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;> implementation 'com&period;google&period;android&period;gms&colon;play-services-auth&colon;20&period;0&period;1'<br &sol;>&rcub;<&sol;pre> &NewLine;<p>4 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> file and add the Internet permission above &lt&semi;application&gt&semi;&period;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET" &sol;&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The Layout File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>5 &period; Add the <strong><span style&equals;"color&colon; &num;008000&semi;">TextView<&sol;span><&sol;strong> and Google <strong><span style&equals;"color&colon; &num;008000&semi;">SignInButton<&sol;span><&sol;strong> in <span style&equals;"color&colon; &num;000080&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> file&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;textView"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;text&equals;"LOGIN"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;500"<br &sol;> android&colon;textSize&equals;"40sp"<br &sol;> android&colon;textStyle&equals;"bold"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;com&period;google&period;android&period;gms&period;common&period;SignInButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;sign&lowbar;in&lowbar;button"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginLeft&equals;"12dp"<br &sol;> android&colon;layout&lowbar;marginRight&equals;"12dp"<br &sol;> android&colon;layout&lowbar;marginBottom&equals;"30dp"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<p>6 &period; The <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;second&period;xml<&sol;span><&sol;strong> file defines the UI of the next activity&lpar;<span style&equals;"color&colon; &num;008000&semi;"><strong>SecondActivity&rpar; <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">that<&sol;span><&sol;span> opens after the user sign in&period; It consists of a <strong><span style&equals;"color&colon; &num;008000&semi;">SignOut<&sol;span><&sol;strong> button two <span style&equals;"color&colon; &num;008000&semi;"><strong>Textviews<&sol;strong><&sol;span> &lpar;to display user name and email&rpar;&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;second&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;SecondActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;pName"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;500"<br &sol;> android&colon;textSize&equals;"21sp"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;pEmail"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"25dp"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;500"<br &sol;> android&colon;textSize&equals;"21sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;pName"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;pName"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;pName" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnSignOut"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;padding&equals;"10dp"<br &sol;> android&colon;text&equals;"Sign Out"<br &sol;> android&colon;textAllCaps&equals;"false"<br &sol;> android&colon;textSize&equals;"20sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;pEmail"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;pEmail"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;pEmail" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<p>7 &period; Create a new kotlin object <strong><span style&equals;"color&colon; &num;008000&semi;">Utility<&sol;span><&sol;strong> and add the below code&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Utility&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>object Utility &lbrace;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Check for existing Google Sign In account&comma; if the user is already signed in<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; the GoogleSignInAccount will be non-null&period;<&sol;strong><&sol;span><br &sol;> fun isUserSignedIn&lpar;context&colon; Context&rpar;&colon; Boolean &lbrace;<br &sol;><br &sol;> val account &equals; GoogleSignIn&period;getLastSignedInAccount&lpar;context&rpar;<br &sol;> return account &excl;&equals; null<br &sol;> &rcub;<br &sol;><br &sol;> fun getGoogleSigninClient&lpar;activity&colon; Activity&rpar;&colon; GoogleSignInClient &lbrace;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Configure sign-in to request the user's ID&comma; email address&comma; and basic<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; profile&period; ID and basic profile are included in DEFAULT&lowbar;SIGN&lowbar;IN&period;<&sol;strong><&sol;span><br &sol;> val gso &equals; GoogleSignInOptions<br &sol;> &period;Builder&lpar;GoogleSignInOptions&period;DEFAULT&lowbar;SIGN&lowbar;IN&rpar;<br &sol;> &period;requestEmail&lpar;&rpar;<br &sol;> &period;requestProfile&lpar;&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Build a GoogleSignInClient with the options specified by gso&period;<&sol;strong><&sol;span><br &sol;> return GoogleSignIn&period;getClient&lpar;activity&comma; gso&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>8 &period; Open MainActivity&period;kt file and add the below code&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> private lateinit var activityMainBinding&colon; ActivityMainBinding<br &sol;><br &sol;> lateinit var mGoogleSignInClient&colon; GoogleSignInClient<br &sol;><br &sol;> lateinit var startActivityForResult&colon; ActivityResultLauncher&lt&semi;Intent&gt&semi;<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> activityMainBinding &equals; ActivityMainBinding&period;inflate&lpar;layoutInflater&rpar;<br &sol;> setContentView&lpar;activityMainBinding&period;root&rpar;<br &sol;><br &sol;> mGoogleSignInClient &equals; getGoogleSigninClient&lpar;this&commat;MainActivity&rpar;<br &sol;><br &sol;> activityMainBinding&period;signInButton&period;setSize&lpar;SignInButton&period;SIZE&lowbar;WIDE&rpar;<br &sol;><br &sol;> activityMainBinding&period;signInButton&period;setOnClickListener &lbrace;<br &sol;> GoogleSignIn&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> startActivityForResult &equals;<br &sol;> registerForActivityResult&lpar;ActivityResultContracts&period;StartActivityForResult&lpar;&rpar;&comma;<br &sol;> ActivityResultCallback&lt&semi;ActivityResult&gt&semi;&lpar;&rpar;<br &sol;> &lbrace;<br &sol;> if &lpar;it&period;resultCode &equals;&equals; Activity&period;RESULT&lowbar;OK&rpar; &lbrace;<br &sol;> handleSignData&lpar;it&period;data&rpar;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> &rcub;<br &sol;> <br &sol;> private fun GoogleSignIn&lpar;&rpar; &lbrace;<br &sol;><br &sol;> if &lpar;&excl;isUserSignedIn&lpar;this&rpar;&rpar; &lbrace;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;creating a sign-in intent with the getSignInIntent method&comma; <&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; and starting the intent by calling startActivityForResult&period;<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Starting the intent prompts the user to select a Google account to sign in with&period;<&sol;strong><&sol;span><br &sol;><br &sol;> val signInIntent &equals; mGoogleSignInClient&period;signInIntent<br &sol;> startActivityForResult&period;launch&lpar;signInIntent&rpar;<br &sol;> &rcub; else &lbrace;<br &sol;> Toast&period;makeText&lpar;this&comma; "User already signed-in"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> &rcub;<br &sol;><br &sol;> private fun handleSignData&lpar;data&colon; Intent&quest;&rpar; &lbrace;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;After the user signs in&comma; you can get a GoogleSignInAccount object for the user&comma;<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;The GoogleSignInAccount object contains information about the signed-in user&period;<&sol;strong><&sol;span><br &sol;> GoogleSignIn&period;getSignedInAccountFromIntent&lpar;data&rpar;<br &sol;> &period;addOnCompleteListener &lbrace;<br &sol;> "isSuccessful &dollar;&lbrace;it&period;isSuccessful&rcub;"&period;print&lpar;&rpar;<br &sol;> if &lpar;it&period;isSuccessful&rpar; &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; user successfully logged-in<&sol;span><&sol;strong><br &sol;> "account &dollar;&lbrace;it&period;result&quest;&period;account&rcub;"&period;print&lpar;&rpar;<br &sol;> "displayName &dollar;&lbrace;it&period;result&quest;&period;displayName&rcub;"&period;print&lpar;&rpar;<br &sol;> "Email &dollar;&lbrace;it&period;result&quest;&period;email&rcub;"&period;print&lpar;&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; start next activity<&sol;strong><&sol;span><br &sol;> val intent &equals; Intent&lpar;this&comma; SecondActivity&colon;&colon;class&period;java&rpar;<br &sol;> startActivity&lpar;intent&rpar;<br &sol;><br &sol;> &rcub; else &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; authentication failed<&sol;span><&sol;strong><br &sol;> "exception &dollar;&lbrace;it&period;exception&rcub;"&period;print&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> companion object &lbrace;<br &sol;> const val TAG&lowbar;MAINACTIVITY &equals; "TAG&lowbar;MAINACTIVITY"<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>fun Any&period;print&lpar;&rpar; &lbrace;<br &sol;> Log&period;v&lpar;MainActivity&period;TAG&lowbar;MAINACTIVITY&comma; " &dollar;this"&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>9 &period; The SecondActivity&period;kt file contains the logic related to sign-out button functionality and here&comma; we get the information related to the sign-in user and display it in textviews&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">SecondActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>class SecondActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> private lateinit var activitySecondBinding&colon; ActivitySecondBinding<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> activitySecondBinding &equals; ActivitySecondBinding&period;inflate&lpar;layoutInflater&rpar;<br &sol;><br &sol;> setContentView&lpar;activitySecondBinding&period;root&rpar;<br &sol;><br &sol;> val account &equals; GoogleSignIn&period;getLastSignedInAccount&lpar;this&rpar;<br &sol;> if &lpar;account &excl;&equals; null&rpar; &lbrace;<br &sol;> activitySecondBinding&period;pName&period;text &equals; account&period;displayName<br &sol;> activitySecondBinding&period;pEmail&period;text &equals; account&period;email<br &sol;> &rcub;<br &sol;><br &sol;> activitySecondBinding&period;btnSignOut&period;setOnClickListener &lbrace;<br &sol;> signout&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> private fun signout&lpar;&rpar; &lbrace;<br &sol;><br &sol;> if &lpar;isUserSignedIn&lpar;this&rpar;&rpar; &lbrace;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;clears the account&comma; which is connected to the app<&sol;strong><&sol;span><br &sol;> getGoogleSigninClient&lpar;this&commat;SecondActivity&rpar;&period;signOut&lpar;&rpar;&period;addOnCompleteListener &lbrace;<br &sol;> if &lpar;it&period;isSuccessful&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;this&commat;SecondActivity&comma; " Signed out "&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;opens login activity<&sol;strong><&sol;span><br &sol;> val intent &equals; Intent&lpar;this&comma; MainActivity&colon;&colon;class&period;java&rpar;<br &sol;> startActivity&lpar;intent&rpar;<br &sol;> &rcub; else &lbrace;<br &sol;> Toast&period;makeText&lpar;this&commat;SecondActivity&comma; " Error "&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;>&rcub;<&sol;pre> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3018" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;01&sol;Screenshot&lowbar;1-473x1024&period;png" alt&equals;"" width&equals;"353" height&equals;"765" &sol;>          <img class&equals;"alignnone wp-image-3019" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;01&sol;Screenshot&lowbar;2-473x1024&period;png" alt&equals;"" width&equals;"351" height&equals;"760" &sol;>&NewLine;

Exit mobile version