Skip to content

Commit

Permalink
added auto text size
Browse files Browse the repository at this point in the history
  • Loading branch information
kewl.apps.dev1 committed Nov 14, 2019
1 parent d55afc1 commit 1dc64d5
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 14 deletions.
116 changes: 116 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
app:buttonTextColor="#FFEB3B"
app:buttonTextColor="#000000"

app:buttonAutoSizeStepGranularity="1"

app:buttonTextSize="30"

app:minButtonTextSize="10"
app:maxButtonTextSize="30"

app:isHapticFeedBack="true"
app:buttonOkBackground="@drawable/v_tick"
app:buttonClearBackground="@drawable/v_back" />
Expand Down
48 changes: 46 additions & 2 deletions pinview/src/main/java/com/gamemalt/pinview/PinView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.widget.TextViewCompat;

import java.util.Arrays;
import java.util.Collections;

Expand All @@ -27,6 +31,10 @@ public class PinView extends LinearLayout {
private int buttonTextColor;
private int buttonTextSize;

private int minButtonTextSize;
private int maxButtonTextSize;

private int autoSizeStepGranularity;

private ImageButton buttonClear;
private ImageButton buttonOk;
Expand Down Expand Up @@ -66,7 +74,14 @@ private void setDefaultAttr(AttributeSet attrs) {
buttonOkBackground = typedArray.getResourceId(R.styleable.PinView_buttonOkBackground, R.drawable.v_tick);
pinButtonBackground = typedArray.getResourceId(R.styleable.PinView_pinButtonBackground, android.R.color.transparent);
buttonTextColor = typedArray.getColor(R.styleable.PinView_buttonTextColor, Color.WHITE);

buttonTextSize = typedArray.getInt(R.styleable.PinView_buttonTextSize, 24);

minButtonTextSize= typedArray.getInt(R.styleable.PinView_minButtonTextSize, -1);
maxButtonTextSize= typedArray.getInt(R.styleable.PinView_maxButtonTextSize, -1);

autoSizeStepGranularity= typedArray.getInt(R.styleable.PinView_buttonAutoSizeStepGranularity, 1);

isHapticFeedBack = typedArray.getBoolean(R.styleable.PinView_isHapticFeedBack, false);
showOkButton = typedArray.getBoolean(R.styleable.PinView_showOkButton, true);
showClearButton = typedArray.getBoolean(R.styleable.PinView_showClearButton, true);
Expand Down Expand Up @@ -141,8 +156,7 @@ private void findAllViews() {
}

setPinButtonTextColor(buttonTextColor);
setPinButtonTextSize(buttonTextSize);

checkAndSetButtonTextSize();
}


Expand Down Expand Up @@ -215,6 +229,20 @@ public void setPinButtonTextColor(int pinButtonTextColor) {
}


private void checkAndSetButtonTextSize(){
if(minButtonTextSize<0 || maxButtonTextSize<0){

setPinButtonTextSize(buttonTextSize);
}else {
setPinButtonAutoTextSize(minButtonTextSize,maxButtonTextSize,autoSizeStepGranularity);
}
}


/*
* Sets button text
* size in SP
* */
public void setPinButtonTextSize(int pinButtonTextSize) {
buttonTextSize = pinButtonTextSize;

Expand All @@ -223,6 +251,22 @@ public void setPinButtonTextSize(int pinButtonTextSize) {
}
}

/*
* Sets auto text size automatically within the range of given min and max sizes
* Size is in SP
* */
public void setPinButtonAutoTextSize(int minSize,int maxSize,int autoSizeStepGranularity){
this.minButtonTextSize=minSize;
this.maxButtonTextSize=maxSize;
this.autoSizeStepGranularity=autoSizeStepGranularity;

for (TextView button : pinButtons) {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(button,minSize,maxSize,autoSizeStepGranularity, TypedValue.COMPLEX_UNIT_SP);
}


}


/*
* Provide resource ID
Expand Down
20 changes: 10 additions & 10 deletions pinview/src/main/res/layout/pin_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:weightSum="3">


<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_1"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -24,7 +24,7 @@
android:text="1"
android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_2"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -35,7 +35,7 @@
android:text="2"
android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_3"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -57,7 +57,7 @@
android:orientation="horizontal"
android:weightSum="3">

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_4"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -68,7 +68,7 @@

android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_5"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -79,7 +79,7 @@
android:text="5"
android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_6"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -100,7 +100,7 @@
android:orientation="horizontal"
android:weightSum="3">

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_7"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -114,7 +114,7 @@

/>

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_8"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand All @@ -125,7 +125,7 @@

android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_9"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand Down Expand Up @@ -155,7 +155,7 @@
android:src="@drawable/v_tick"
android:theme="@style/PinPadButton" />

<TextView
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/button_0"
style="@style/PinPadButton"
android:layout_width="0dp"
Expand Down
5 changes: 5 additions & 0 deletions pinview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
<attr name="buttonClearBackground" format="reference" />
<attr name="buttonOkBackground" format="reference" />
<attr name="buttonTextColor" format="color" />

<attr name="buttonTextSize" format="integer" />
<attr name="minButtonTextSize" format="integer" />
<attr name="maxButtonTextSize" format="integer" />
<attr name="buttonAutoSizeStepGranularity" format="integer" />

<attr name="isHapticFeedBack" format="boolean" />
<attr name="showOkButton" format="boolean" />
<attr name="showClearButton" format="boolean" />
Expand Down

0 comments on commit 1dc64d5

Please sign in to comment.