Quick Summary
In this post, I will show you how to change the text on Google's Sign-In button using standard android:text
attribute, which is missing from the Google's SignInButton. I'll also show you, how you can switch between dark and light button themes. At the end of the post, a ready to use small Android library is included along with a sample application to try the same.
Requirements
- Android Studio.
compileSdkVersion 25
andbuildToolsVersion "25.0.3"
. This is what the sample app is using. You can change the values in thebuild.gradle
, to use the SDK version you have installed.minSdkVersion
supported is16
.
Introduction
Do you want to provide localization for the Google Sign-In button? Maybe you want to change the default "Sign In" text to "Sign in with Google". Or you want switch between "Sign in with Google" and "Sign up with Google" text based on whether it is a sign-in or sign-up flow. As you might already know, to set the text on an Android Button
, you can use android:text="{string}"
attribute in your layout XML. If you want to do the same for Google Sign-In, this attribute is not available. On Stackoverflow, there are obviously many questions like Can I edit the text of sign in button on Google?, which have been asked for this problem. Most of the existing answers were hacks at the time of writing.
Follow along as I show you how you can use a very small library, created using Google's recommended way to customize the SignInButton, that will enable you to use android:text
attribute to set any text on the button. The library also enables you to change the theme of the button to light or dark easily.
Usage
Add the following to your
app
module levelbuild.gradle
file:dependencies { // To include this library. // For Android Studio 3.0 or above, use implementation keyword instead compile 'com.shobhitpuri.custombuttons:google-signin:1.0.0' }
In your XML Layout, have the following:
<RelativeLayout ... xmlns:app="http://schemas.android.com/apk/res-auto"> <com.shobhitpuri.custombuttons.GoogleSignInButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/google_sign_up" app:isDarkTheme="true" /> </RelativeLayout>
Library Options
If you notice the XML, there are two attributes:
app:isDarkTheme="{Boolean}"
: This is to enable you to switch between the light (gray-white) theme and the dark (blue) theme for the button. The library handles changing the text color, background color and color change on button press based on Google's recommended guidelines.android:text="{string}"
: This sets the text on the custom button.
Light Theme | Dark Theme |
---|---|
Why a library?
"Why create a library for this? Aren't there already tons of libraries already?", you would say. This library was result of the issues I faced when trying to set text on the SignInButton
. I don't like using hacks and since you are here, this far in the article, I would assume you don't either. If the underlying implementation of Google's SignInButton
changes, the hack would break. The ideal Google recommended solution from the documentation is to create a custom button as mentioned on Customizing the Sign-In Button. It specifies the branding guidelines which includes using custom icons and images for the button, setting specific text size, paddings and other do's and don'ts for the logo.
As you can see the ideal solution involves some extra work. Instead of creating a custom button just for my usage, I wanted to write some re-usable code, which I can drag and drop in any of my projects and it would work out of the box. That's why I decided to create a small 3.93 KB library, so that anyone facing this issue need not spend time implementing a custom solution and can get the custom Google Sign-In button working in no time.
Source Code and Sample Android Application
You can find the implementation of the library and a sample Android application that is using the library here: https://github.com/shobhitpuri/custom-google-signin-button. Feel free to give any feedback on the article or library. If you come across any issues, you are more than welcome to create an issue or open a pull request.
Note: The images used in this article are trademark of Google. They have been just used for instructional purposes.
'[ 모바일 관련 ] > 안드로이드' 카테고리의 다른 글
2019년 8월 부터 신규 앱 런칭시 target Version 28이상 적용 (0) | 2019.07.22 |
---|---|
홈버튼 누른 후 아이콘 터치시 액티비티 재실행 되는 현상 관련 (0) | 2018.10.15 |
안드로이드 앱 종료 방법 (0) | 2018.10.14 |
이미지뷰 라운딩 방법 (0) | 2018.10.08 |
firebase Authentication과 구글 로그인 연동 (0) | 2018.10.02 |