Skip to content

Added ability to center the text in the button #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions CustomGoogleSignInButton/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
compileSdkVersion 27

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"

Expand All @@ -23,5 +22,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:appcompat-v7:27.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import android.util.AttributeSet;
import android.util.TypedValue;

import static com.shobhitpuri.custombuttons.util.Constants.BUTTON_TEXT_SIZE;
import com.shobhitpuri.custombuttons.util.Constants;
import com.shobhitpuri.custombuttons.util.GenericUtils;

/**
* This is a customized Google Sign In button. Supports:
Expand All @@ -57,6 +58,12 @@ public class GoogleSignInButton extends AppCompatButton {
*/
private boolean mIsDarkTheme;

/**
* Flag that give the feeling of the text being in the middle of the button.
* P.S: Looks decent with light background but ugly with dark theme.
*/
private boolean mCenterTextInButton;

/**
* Constructor
*
Expand Down Expand Up @@ -120,6 +127,8 @@ private void parseAttributes(Context context, AttributeSet attributeSet, int def
mText = typedArray.getString(R.styleable.ButtonStyleable_android_text);
// Get the attribute to check if user wants dark theme.
mIsDarkTheme = typedArray.getBoolean(R.styleable.ButtonStyleable_isDarkTheme, false);
// Get the attribute to check if user wants to center in the text in the whole button.
mCenterTextInButton = typedArray.getBoolean(R.styleable.ButtonStyleable_centerTextInButton, false);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Expand All @@ -141,13 +150,15 @@ private void setButtonParams() {
setButtonTextColor();
// Set background of button
setButtonBackground();
// Set text in middle if set.
setButtonTextPosition();
}

/**
* Set the text size to standard as mentioned in guidelines.
*/
private void setButtonTextSize() {
this.setTextSize(TypedValue.COMPLEX_UNIT_SP, BUTTON_TEXT_SIZE);
this.setTextSize(TypedValue.COMPLEX_UNIT_SP, Constants.BUTTON_TEXT_SIZE);
}

/**
Expand All @@ -168,6 +179,21 @@ private void setButtonTextColor() {
this.setTextColor(ContextCompat.getColor(getContext(), textColor));
}

/**
* Give the feel of text being in the center of the whole button instead of just being in the
* center of the section to right of the Google icon.
*/
private void setButtonTextPosition() {
if (mCenterTextInButton) {
// Add more padding to the right of same width as the Google button.
// This gives the illusion of the text being in the center.
// P.S: The logic could be improved
int paddingRight = getPaddingRight() +
(int) GenericUtils.convertDpToPixel(Constants.GOOGLE_ICON_SIZE_DP, getContext());
setPadding(getPaddingLeft(), getPaddingTop(), paddingRight, getPaddingBottom());
}
}

/**
* If user has set text, that takes priority else use default button text.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@
public class Constants {
// Standard text size got SignInButton
public static final int BUTTON_TEXT_SIZE = 14;
// Width of just the Google icon on the button
public static final int GOOGLE_ICON_SIZE_DP = 40;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* MIT License
*
* Copyright (c) [2017] [Shobhit Puri]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.shobhitpuri.custombuttons.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;

/**
* Utility class.
*/
public class GenericUtils {
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
resources.getDisplayMetrics()
);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
1 change: 1 addition & 0 deletions CustomGoogleSignInButton/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ButtonStyleable">
<attr name="centerTextInButton" format="boolean" />
<attr name="isDarkTheme" format="boolean" />
<attr name="android:text" />
</declare-styleable>
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
---
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Custom%20Google%20SignInButton-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/7099)

You want to add a Google Sign-In button to your Android application. But you want to change the text on the Google Sign-In button or provide custom localization? You would think setting `android:text` on the `com.google.android.gms.common.SignInButton` in your layout file would do the trick. However it turns out that that attribute is not available for `SignInButton`.
You want to add a Google Sign-In button to your Android application. But you want to change the text on the Google Sign-In button or provide custom localization? You would think setting `android:text` on the `com.google.android.gms.common.SignInButton` in your layout file would do the trick. However it turns out that that attribute is not available for `SignInButton`. There are also issues like centering the text in the button. In the Google's `SignInButton`, the text is centered within the empty space next to the Google icon but not within the whole button.

**Solution**
---

This library helps you add text to Google Sign-In Button easily using standard `android:text` attribute. It also allows you to set the button theme to dark or light using `app:isDarkTheme="true"` attribute. It does so following Google's guidelines to create sign-in button.
This library helps you add text to Google Sign-In Button easily using standard `android:text` attribute. It also allows you to center the text in the button or set the button theme to dark or light using `app:isDarkTheme="true"` attribute. It does so following Google's guidelines to create sign-in button.

Light Theme (White) | Dark Theme (Blue)
:-------------------------:|:-------------------------:
Expand All @@ -21,7 +21,7 @@ Add the following to your `app` module level `build.gradle` file:

dependencies {
// Please Note: If you are using Android Studio older than 3.0 or Gradle plugin older than 3.0, then use the `compile` keyword instead of `implementation`.
implementation 'com.shobhitpuri.custombuttons:google-signin:1.0.0'
implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0'
}

In your XML Layout, have the following:
Expand All @@ -35,21 +35,22 @@ In your XML Layout, have the following:
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/google_sign_up"
app:centerTextInButton="false"
app:isDarkTheme="true" />
</RelativeLayout>


**Options**
---

- `app:isDarkTheme="{Boolean}"` : To switch between blue theme and gray white for the button. The library handles changing of text color and background color. It also handles the change of color on button press or button clicks.
- `android:text="{string}"`: As usual to set the text on the button.

- `app:isDarkTheme="{Boolean}"` : To switch between blue theme and gray white for the button. The library handles changing of text color and background color. It also handles the change of color on button press or button clicks.
- `app:centerTextInButton="{Boolean}"` : To center the text in the whole button. By default it is centered in within the emply space next to the Google icon.

**(dirty) Solutions**
---

On the Stackoverflow, there are obviously questions which have been asked for this issue. One of them is [Can I edit the text of sign in button on Google?](https://stackoverflow.com/questions/18040815/can-i-edit-the-text-of-sign-in-button-on-google) Many of them suggest using "hacks" like finding the first `TextView` in the button or finding any `TextView` in the button. The issue with them is they might stop working if Google updates the implementation.
On the Stackoverflow, there are obviously questions which have been asked for this issue. One of them is [Can I edit the text of sign in button on Google?](https://stackoverflow.com/questions/18040815/can-i-edit-the-text-of-sign-in-button-on-google) Many of them suggest using "hacks" like finding the first `TextView` in the button or finding any `TextView` in the button. The issue with them is they might stop working if Google updates the implementation. There are also questions like [How to center text in google sign in button android](https://stackoverflow.com/questions/41967615/how-to-center-text-in-google-sign-in-button-android), which are trying to deal with the issue of centering the text in whole button.

**Google's Suggestion**
---
Expand Down
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
compileSdkVersion 27
defaultConfig {
applicationId "com.shobhitpuri.googlecustomsigninbutton"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -28,8 +27,8 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support.constraint:constraint-layout:1.1.2'
testCompile 'junit:junit:4.12'
}

1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/google_sign_up"
app:centerTextInButton="false"
app:isDarkTheme="true" />
</RelativeLayout>
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.3.0-alpha03'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -14,7 +16,9 @@ buildscript {

allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip