Skip to content

Commit 6a79b59

Browse files
committed
Allow centering text within the whole button instead of just the space other than Google icon. This resolves #3
1 parent fbf85a4 commit 6a79b59

File tree

66 files changed

+101
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+101
-19
lines changed

CustomGoogleSignInButton/build.gradle

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion "25.0.3"
4+
compileSdkVersion 27
65

76
defaultConfig {
87
minSdkVersion 16
9-
targetSdkVersion 25
8+
targetSdkVersion 27
109
versionCode 1
1110
versionName "1.0"
1211

@@ -23,5 +22,5 @@ android {
2322

2423
dependencies {
2524
compile fileTree(dir: 'libs', include: ['*.jar'])
26-
compile 'com.android.support:appcompat-v7:25.3.1'
25+
compile 'com.android.support:appcompat-v7:27.1.1'
2726
}

CustomGoogleSignInButton/src/main/java/com/shobhitpuri/custombuttons/GoogleSignInButton.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
import android.util.AttributeSet;
3131
import android.util.TypedValue;
3232

33-
import static com.shobhitpuri.custombuttons.util.Constants.BUTTON_TEXT_SIZE;
33+
import com.shobhitpuri.custombuttons.util.Constants;
34+
import com.shobhitpuri.custombuttons.util.GenericUtils;
3435

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

61+
/**
62+
* Flag that give the feeling of the text being in the middle of the button.
63+
* P.S: Looks decent with light background but ugly with dark theme.
64+
*/
65+
private boolean mCenterTextInButton;
66+
6067
/**
6168
* Constructor
6269
*
@@ -120,6 +127,8 @@ private void parseAttributes(Context context, AttributeSet attributeSet, int def
120127
mText = typedArray.getString(R.styleable.ButtonStyleable_android_text);
121128
// Get the attribute to check if user wants dark theme.
122129
mIsDarkTheme = typedArray.getBoolean(R.styleable.ButtonStyleable_isDarkTheme, false);
130+
// Get the attribute to check if user wants to center in the text in the whole button.
131+
mCenterTextInButton = typedArray.getBoolean(R.styleable.ButtonStyleable_centerTextInButton, false);
123132
} catch (Exception ex) {
124133
ex.printStackTrace();
125134
} finally {
@@ -141,13 +150,15 @@ private void setButtonParams() {
141150
setButtonTextColor();
142151
// Set background of button
143152
setButtonBackground();
153+
// Set text in middle if set.
154+
setButtonTextPosition();
144155
}
145156

146157
/**
147158
* Set the text size to standard as mentioned in guidelines.
148159
*/
149160
private void setButtonTextSize() {
150-
this.setTextSize(TypedValue.COMPLEX_UNIT_SP, BUTTON_TEXT_SIZE);
161+
this.setTextSize(TypedValue.COMPLEX_UNIT_SP, Constants.BUTTON_TEXT_SIZE);
151162
}
152163

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

182+
/**
183+
* Give the feel of text being in the center of the whole button instead of just being in the
184+
* center of the section to right of the Google icon.
185+
*/
186+
private void setButtonTextPosition() {
187+
if (mCenterTextInButton) {
188+
// Add more padding to the right of same width as the Google button.
189+
// This gives the illusion of the text being in the center.
190+
// P.S: The logic could be improved
191+
int paddingRight = getPaddingRight() +
192+
(int) GenericUtils.convertDpToPixel(Constants.GOOGLE_ICON_SIZE_DP, getContext());
193+
setPadding(getPaddingLeft(), getPaddingTop(), paddingRight, getPaddingBottom());
194+
}
195+
}
196+
171197
/**
172198
* If user has set text, that takes priority else use default button text.
173199
*/

CustomGoogleSignInButton/src/main/java/com/shobhitpuri/custombuttons/util/Constants.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@
3232
public class Constants {
3333
// Standard text size got SignInButton
3434
public static final int BUTTON_TEXT_SIZE = 14;
35+
// Width of just the Google icon on the button
36+
public static final int GOOGLE_ICON_SIZE_DP = 40;
3537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) [2017] [Shobhit Puri]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.shobhitpuri.custombuttons.util;
25+
26+
import android.content.Context;
27+
import android.content.res.Resources;
28+
import android.util.TypedValue;
29+
30+
/**
31+
* Utility class.
32+
*/
33+
public class GenericUtils {
34+
/**
35+
* This method converts dp unit to equivalent pixels, depending on device density.
36+
*
37+
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
38+
* @param context Context to get resources and device specific display metrics
39+
* @return A float value to represent px equivalent to dp depending on device density
40+
*/
41+
public static float convertDpToPixel(float dp, Context context) {
42+
Resources resources = context.getResources();
43+
return TypedValue.applyDimension(
44+
TypedValue.COMPLEX_UNIT_DIP,
45+
dp,
46+
resources.getDisplayMetrics()
47+
);
48+
}
49+
}
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.
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.

CustomGoogleSignInButton/src/main/res/values/attrs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<declare-styleable name="ButtonStyleable">
4+
<attr name="centerTextInButton" format="boolean" />
45
<attr name="isDarkTheme" format="boolean" />
56
<attr name="android:text" />
67
</declare-styleable>

README.md

+7-6

app/build.gradle

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
apply plugin: 'com.android.application'
22
android {
3-
compileSdkVersion 25
4-
buildToolsVersion "25.0.3"
3+
compileSdkVersion 27
54
defaultConfig {
65
applicationId "com.shobhitpuri.googlecustomsigninbutton"
76
minSdkVersion 16
8-
targetSdkVersion 25
7+
targetSdkVersion 27
98
versionCode 1
109
versionName "1.0"
1110
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -28,8 +27,8 @@ dependencies {
2827
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2928
exclude group: 'com.android.support', module: 'support-annotations'
3029
})
31-
compile 'com.android.support:appcompat-v7:25.3.1'
32-
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
30+
compile 'com.android.support:appcompat-v7:27.1.1'
31+
compile 'com.android.support.constraint:constraint-layout:1.1.2'
3332
testCompile 'junit:junit:4.12'
3433
}
3534

app/src/main/res/layout/activity_main.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
android:layout_height="wrap_content"
1010
android:layout_centerInParent="true"
1111
android:text="@string/google_sign_up"
12+
app:centerTextInButton="false"
1213
app:isDarkTheme="true" />
1314
</RelativeLayout>

build.gradle

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
buildscript {
44
repositories {
5+
google()
56
jcenter()
7+
mavenCentral()
68
}
79
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.3'
10+
classpath 'com.android.tools.build:gradle:3.3.0-alpha03'
911

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

1517
allprojects {
1618
repositories {
19+
google()
1720
jcenter()
21+
mavenCentral()
1822
}
1923
}
2024

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip

0 commit comments

Comments
 (0)