Skip to content

Commit d43ace4

Browse files
committed
Fixing padding for icon
1 parent 72d0c1c commit d43ace4

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dependencies {
2424
implementation project(':appcompaticonbuttonlibrary')
2525
implementation 'androidx.appcompat:appcompat:1.2.0'
2626
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
27-
testImplementation 'junit:junit:4.13.1'
27+
testImplementation 'junit:junit:4.13.2'
2828
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
2929
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
3030
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
tools:context=".MainActivity">
99

1010
<com.swrmedia.appcompaticonbuttonlibrary.AppCompatIconButton
11+
android:id="@+id/btn_proceed"
1112
android:layout_width="match_parent"
12-
android:layout_height="wrap_content"
13-
android:background="@color/colorAccent"
13+
android:layout_height="50dp"
14+
android:background="#000000"
1415
android:gravity="center"
15-
android:text="@string/app_name"
16-
android:textColor="@android:color/white"
17-
app:ib_iconAlign="left_of_title"
18-
app:ib_iconSrc="@android:drawable/ic_lock_idle_alarm"
16+
android:text="Продолжить"
17+
android:textColor="#ffffff"
18+
app:ib_iconPadding="25dp"
19+
app:ib_iconSrc="@drawable/abc_vector_test"
20+
app:ib_iconAlign="right"
1921
app:ib_textAllCaps="true" />
22+
23+
24+
2025
</LinearLayout>

appcompaticonbuttonlibrary/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 16
1010
targetSdkVersion 29
11-
versionCode 2
12-
versionName "1.0.1"
11+
versionCode 1
12+
versionName "1.0"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles 'consumer-rules.pro'
@@ -28,7 +28,7 @@ dependencies {
2828
implementation fileTree(dir: 'libs', include: ['*.jar'])
2929

3030
implementation 'androidx.appcompat:appcompat:1.2.0'
31-
testImplementation 'junit:junit:4.13.1'
31+
testImplementation 'junit:junit:4.13.2'
3232
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
3333
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
3434
}

appcompaticonbuttonlibrary/src/main/java/com/swrmedia/appcompaticonbuttonlibrary/AppCompatIconButton.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,35 @@ private void applyStyles(Context context, @Nullable AttributeSet attrs, int defS
7676
typedArray.recycle();
7777
}
7878

79+
@Override
80+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
81+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
82+
int desiredWidth = getPaddingLeft() + getPaddingRight();
83+
int desiredHeight = getPaddingTop() + getPaddingBottom();
84+
85+
int measuredHeight = measureDimension(desiredHeight, heightMeasureSpec);
86+
float textWidth = getPaint().measureText((String) getText());
87+
88+
if (drawableId != 0) {
89+
desiredWidth += textWidth + iconSize + iconPadding;
90+
}
91+
int measuredWidth = measureDimension(desiredWidth, widthMeasureSpec);
92+
setMeasuredDimension(measuredWidth, measuredHeight);
93+
}
94+
95+
private int measureDimension(int contentSize, int measureSpec) {
96+
int mode = MeasureSpec.getMode(measureSpec);
97+
int specSize = MeasureSpec.getSize(measureSpec);
98+
switch (mode) {
99+
case MeasureSpec.EXACTLY:
100+
return specSize;
101+
case MeasureSpec.AT_MOST:
102+
return Math.min(contentSize, specSize);
103+
default:
104+
return contentSize;
105+
}
106+
}
107+
79108
@Override
80109
protected void onDraw(Canvas canvas) {
81110
canvas.save();
@@ -88,13 +117,13 @@ protected void onDraw(Canvas canvas) {
88117

89118
if (icon != null) {
90119
float textWidth = getPaint().measureText((String) getText());
91-
int left = 0;
120+
int left = iconPadding;
92121
if (direction == Align.left_of_text) {
93122
left = (int) ((getWidth() / 2f) - (textWidth / 2f) - iconSize - iconPadding);
94123
} else if (direction == Align.right_of_text) {
95124
left = (int) ((getWidth() / 2f) + (textWidth / 2f) + dx + iconPadding);
96125
} else if (direction == Align.right) {
97-
left = getWidth() - iconSize;
126+
left = getWidth() - iconSize - iconPadding;
98127
}
99128
int top = getHeight() / 2 - iconSize / 2;
100129

sh.exe.stackdump

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Stack trace:
2+
Frame Function Args
3+
00000010000 0018006137E (0018025BEB0, 0018024E199, 00000010000, 000FFFFBA00)
4+
00000010000 00180049229 (00100002000, 00000000002, 00000000002, 00180339570)
5+
00000010000 00180049262 (00000000002, 00180339880, 00000010000, 00000000008)
6+
00000010000 0018005A9EC (00000000000, 0018024E48F, 0018004A6FE, 635C655C785C655C)
7+
000FFFFCCD0 0018005AA90 (745C685C275C205C, 3A5C735C705C745C, 695C675C2F5C2F5C, 625C755C685C745C)
8+
000FFFFCCD0 001800499EC (00000000000, 00000000000, 00000000000, 00000000000)
9+
00000000000 00180048343 (00000000000, 00000000000, 00000000000, 00000000000)
10+
000FFFFFFF0 001800483F4 (00000000000, 00000000000, 00000000000, 00000000000)
11+
End of stack trace

0 commit comments

Comments
 (0)