Skip to content

Commit ac5c41c

Browse files
committed
v1.9 fix wrap_content issue
1 parent e504bac commit ac5c41c

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ native android button we will discuss all the available customization in details
2222
2323
//Then add these lines to your dependencies
2424
dependencies {
25-
implementation 'com.github.MrAndroi:CustomGradientButton:1.8'
25+
implementation 'com.github.MrAndroi:CustomGradientButton:1.9'
2626
2727
//Compose Dependencies
2828
def composeBom = platform('androidx.compose:compose-bom:2022.12.00')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<com.maf.custom.views.gradient_button.CustomGradientButton
3131
android:id="@+id/custom_button"
32-
android:layout_width="match_parent"
32+
android:layout_width="wrap_content"
3333
android:layout_height="wrap_content"
3434
app:onDebounceClick="@{() -> main.onButtonOneClick()}"
3535
android:layout_marginTop="20dp"
@@ -55,7 +55,7 @@
5555

5656
<com.maf.custom.views.gradient_button.CustomGradientButton
5757
android:id="@+id/custom_button_disabled"
58-
android:layout_width="match_parent"
58+
android:layout_width="wrap_content"
5959
android:layout_height="wrap_content"
6060
android:layout_marginHorizontal="10dp"
6161
android:layout_marginTop="20dp"

gradient-button/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ afterEvaluate {
7777

7878
groupId = 'com.maf.custom.views'
7979
artifactId = 'gradient-button'
80-
version = '1.8'
80+
version = '1.9'
8181
}
8282
}
8383
}

gradient-button/src/main/java/com/maf/custom/views/gradient_button/CustomGradientButton.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package com.maf.custom.views.gradient_button
22

33
import android.content.Context
44
import android.util.AttributeSet
5+
import android.view.ViewGroup
6+
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
7+
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
58
import androidx.compose.animation.animateColor
69
import androidx.compose.animation.core.*
710
import androidx.compose.foundation.*
@@ -72,6 +75,7 @@ class CustomGradientButton @JvmOverloads constructor(
7275
var animationSpeed by mutableStateOf(2000)
7376
var clickEffectColor by mutableStateOf(0xffffff)
7477
var fontFamily by mutableStateOf(0)
78+
var layoutWidth: String? by mutableStateOf(null)
7579
private var onClick: (() -> Unit)? = null
7680

7781
init {
@@ -109,6 +113,11 @@ class CustomGradientButton @JvmOverloads constructor(
109113

110114
clickEffectColor = getColor(R.styleable.CustomButton_clickEffectColor, 0xffffff)
111115
fontFamily = getResourceId(R.styleable.CustomButton_font, 0)
116+
117+
layoutWidth = attrs?.getAttributeValue(
118+
"http://schemas.android.com/apk/res/android",
119+
"layout_width"
120+
)
112121
}
113122
}
114123

@@ -211,6 +220,7 @@ class CustomGradientButton @JvmOverloads constructor(
211220
fontFamily.value
212221
)
213222
),
223+
layoutWidth = layoutWidth,
214224
listener = onClick
215225
)
216226
}
@@ -250,6 +260,7 @@ fun GradientButton(
250260
endIconPadding: Dp = 8.dp,
251261
clickEffectColor: Color = Color.White,
252262
fontFamily: FontFamily = FontFamily.Default,
263+
layoutWidth: String? = null,
253264
listener: (() -> Unit)? = null,
254265
) {
255266
var arrangement = when (iconsArrangement) {
@@ -295,7 +306,6 @@ fun GradientButton(
295306
}
296307

297308
val enabledModifier = Modifier
298-
.fillMaxWidth()
299309
.wrapContentHeight()
300310
.clip(RoundedCornerShape(borderRound))
301311
.background(background)
@@ -320,15 +330,31 @@ fun GradientButton(
320330
.clipToBounds()
321331
.padding(bottom = innerVerticalPadding, top = innerVerticalPadding)
322332

333+
when {
334+
layoutWidth.equals(MATCH_PARENT.toString()) ->
335+
enabledModifier.fillMaxWidth()
336+
layoutWidth.equals(WRAP_CONTENT.toString()) ->
337+
enabledModifier.wrapContentWidth()
338+
}
339+
323340
Box(
324341
modifier = enabledModifier
325342
) {
343+
344+
val rowModifier = Modifier
345+
.wrapContentWidth()
346+
.align(Center)
347+
.padding(start = innerHorizontalPadding, end = innerHorizontalPadding)
348+
349+
when {
350+
layoutWidth.equals(MATCH_PARENT.toString()) ->
351+
rowModifier.fillMaxWidth()
352+
layoutWidth.equals(WRAP_CONTENT.toString()) ->
353+
rowModifier.wrapContentWidth()
354+
}
326355
Row(
327356
verticalAlignment = CenterVertically,
328-
modifier = Modifier
329-
.fillMaxWidth()
330-
.align(Center)
331-
.padding(start = innerHorizontalPadding, end = innerHorizontalPadding),
357+
modifier = rowModifier,
332358
horizontalArrangement = arrangement
333359
) {
334360
startIconRes?.let {

0 commit comments

Comments
 (0)