From 09bcf484d65c362cfd6e61006463cdc5b974d253 Mon Sep 17 00:00:00 2001 From: zboot Date: Wed, 3 Apr 2024 15:10:03 +0100 Subject: [PATCH] Updated Gradle and AGP; Updated dependencies; Optimized some code; Using ImageView instead of TextView compound drawable to show icons to control icon size. --- build.gradle | 6 +- demo/build.gradle | 47 +++---- demo/src/main/AndroidManifest.xml | 37 +++--- gradle/wrapper/gradle-wrapper.properties | 4 +- styleabletoast/build.gradle | 12 +- .../muddz/styleabletoast/StyleableToast.java | 119 ++++++++++++------ .../io/github/muddz/styleabletoast/Utils.java | 5 +- .../src/main/res/drawable-v27/shape_toast.xml | 6 - .../src/main/res/drawable/shape_toast.xml | 6 +- .../main/res/layout-v27/styleable_layout.xml | 22 ---- .../src/main/res/layout/styleable_layout.xml | 57 ++++++--- .../src/main/res/values-v27/colors.xml | 4 +- .../src/main/res/values-v27/dimens.xml | 14 +-- styleabletoast/src/main/res/values/attrs.xml | 4 +- styleabletoast/src/main/res/values/colors.xml | 6 +- styleabletoast/src/main/res/values/dimens.xml | 22 ++-- 16 files changed, 203 insertions(+), 168 deletions(-) delete mode 100644 styleabletoast/src/main/res/drawable-v27/shape_toast.xml delete mode 100644 styleabletoast/src/main/res/layout-v27/styleable_layout.xml diff --git a/build.gradle b/build.gradle index f950184..b00bfc2 100644 --- a/build.gradle +++ b/build.gradle @@ -7,9 +7,9 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:7.0.0' - classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.5.0' - classpath 'com.vanniktech:gradle-maven-publish-plugin:0.17.0' + classpath 'com.android.tools.build:gradle:8.3.1' + classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.9.20' + classpath 'com.vanniktech:gradle-maven-publish-plugin:0.28.0' } } diff --git a/demo/build.gradle b/demo/build.gradle index 061b6ad..892411c 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -1,33 +1,34 @@ plugins { - id 'com.android.application' + id 'com.android.application' } android { - compileSdk 30 + compileSdk 34 - defaultConfig { - applicationId "io.github.muddz.styleabletoast.demo" - minSdk 16 - targetSdk 30 - versionCode 1 - versionName "1.0" - } + namespace "io.github.muddz.styleabletoast.demo" + defaultConfig { + applicationId "io.github.muddz.styleabletoast.demo" + minSdk 16 + targetSdk 34 + versionCode 1 + versionName "1.0" + } - buildFeatures { - dataBinding true - } + buildFeatures { + dataBinding true + } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - } - } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } } dependencies { - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'com.google.android.material:material:1.4.0' - implementation 'io.github.muddz:styleabletoast:2.4.0' -// implementation project(':styleabletoast') -} + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'com.google.android.material:material:1.11.0' +// implementation 'io.github.muddz:styleabletoast:2.4.0' + implementation project(':styleabletoast') +} \ No newline at end of file diff --git a/demo/src/main/AndroidManifest.xml b/demo/src/main/AndroidManifest.xml index 655e7dd..70fbdbc 100644 --- a/demo/src/main/AndroidManifest.xml +++ b/demo/src/main/AndroidManifest.xml @@ -1,22 +1,23 @@ + xmlns:tools="http://schemas.android.com/tools" + package="io.github.muddz.styleabletoast.demo"> - + - - - - - - + + + + + + - - + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3d21570..e977bb7 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME +zipStoreBase=GRADLE_USER_HOME \ No newline at end of file diff --git a/styleabletoast/build.gradle b/styleabletoast/build.gradle index e5f1fe3..a020809 100644 --- a/styleabletoast/build.gradle +++ b/styleabletoast/build.gradle @@ -1,15 +1,17 @@ plugins { id 'com.android.library' - id 'com.vanniktech.maven.publish' + id 'maven-publish' } android { - compileSdk 30 + compileSdk 34 + namespace "io.github.muddz.styleabletoast" defaultConfig { minSdk 16 - targetSdk 30 + targetSdk 34 } + buildTypes { release { minifyEnabled false @@ -19,5 +21,5 @@ android { } dependencies { - implementation 'androidx.appcompat:appcompat:1.3.1' -} + implementation 'androidx.appcompat:appcompat:1.6.1' +} \ No newline at end of file diff --git a/styleabletoast/src/main/java/io/github/muddz/styleabletoast/StyleableToast.java b/styleabletoast/src/main/java/io/github/muddz/styleabletoast/StyleableToast.java index d08bb78..f326b7f 100644 --- a/styleabletoast/src/main/java/io/github/muddz/styleabletoast/StyleableToast.java +++ b/styleabletoast/src/main/java/io/github/muddz/styleabletoast/StyleableToast.java @@ -11,11 +11,13 @@ import android.util.TypedValue; import android.view.Gravity; import android.view.View; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.ColorInt; +import androidx.annotation.ColorRes; import androidx.annotation.DrawableRes; import androidx.annotation.FontRes; import androidx.annotation.NonNull; @@ -24,15 +26,20 @@ import androidx.core.content.res.ResourcesCompat; import androidx.core.widget.TextViewCompat; +import org.jetbrains.annotations.Contract; + + @SuppressLint("ViewConstructor") public class StyleableToast extends LinearLayout { + @DrawableRes private int iconStart; + @DrawableRes private int iconEnd; + private int iconSize; private int cornerRadius; private int backgroundColor; private int strokeColor; private int strokeWidth; - private int iconStart; - private int iconEnd; + private int verticalPadding; private int textColor; private int font; private int length; @@ -41,17 +48,23 @@ public class StyleableToast extends LinearLayout { private boolean isTextSizeFromStyleXml = false; private boolean solidBackground; private boolean textBold; - private String text; + private final String text; private TypedArray typedArray; private TextView textView; + private ImageView iconStartView; + private ImageView iconEndView; private int gravity; private Toast toast; private LinearLayout rootLayout; + @NonNull + @Contract("_, _, _, _ -> new") public static StyleableToast makeText(@NonNull Context context, String text, int length, @StyleRes int style) { return new StyleableToast(context, text, length, style); } + @NonNull + @Contract("_, _, _ -> new") public static StyleableToast makeText(@NonNull Context context, String text, @StyleRes int style) { return new StyleableToast(context, text, Toast.LENGTH_SHORT, style); } @@ -63,12 +76,18 @@ private StyleableToast(@NonNull Context context, String text, int length, @Style this.style = style; } - private StyleableToast(Builder builder) { + private StyleableToast(@NonNull Builder builder) { super(builder.context); this.backgroundColor = builder.backgroundColor; this.cornerRadius = builder.cornerRadius; this.iconEnd = builder.iconEnd; this.iconStart = builder.iconStart; + this.iconSize = + builder.iconSize == -1 ? (int) getResources().getDimension(R.dimen.default_icon_size) : + builder.iconSize; + this.verticalPadding = + builder.verticalPadding == -1 ? (int) getResources().getDimension(R.dimen.toast_vertical_padding) : + builder.verticalPadding; this.strokeColor = builder.strokeColor; this.strokeWidth = builder.strokeWidth; this.solidBackground = builder.solidBackground; @@ -84,7 +103,10 @@ private StyleableToast(Builder builder) { private void inflateToastLayout() { View v = inflate(getContext(), R.layout.styleable_layout, null); rootLayout = (LinearLayout) v.getRootView(); - textView = v.findViewById(R.id.textview); + textView = v.findViewById(R.id.text); + iconStartView = v.findViewById(R.id.iconStart); + iconEndView = v.findViewById(R.id.iconEnd); + if (style > 0) { typedArray = getContext().obtainStyledAttributes(style, R.styleable.StyleableToast); } @@ -144,6 +166,7 @@ private void makeShape() { private void makeTextView() { loadTextViewAttributes(); textView.setText(text); + if (textColor != 0) { textView.setTextColor(textColor); } @@ -161,53 +184,45 @@ private void makeTextView() { private void makeIcon() { loadIconAttributes(); - int paddingVertical = (int) getResources().getDimension(R.dimen.toast_vertical_padding); - int paddingHorizontal1 = (int) getResources().getDimension(R.dimen.toast_horizontal_padding_icon_side); + int paddingHorizontal = (int) getResources().getDimension(R.dimen.toast_horizontal_padding_icon_side); int paddingNoIcon = (int) getResources().getDimension(R.dimen.toast_horizontal_padding_empty_side); - int iconSize = (int) getResources().getDimension(R.dimen.icon_size); if (iconStart != 0) { - Drawable drawable = ContextCompat.getDrawable(getContext(), iconStart); - if (drawable != null) { - drawable.setBounds(0, 0, iconSize, iconSize); - TextViewCompat.setCompoundDrawablesRelative(textView, drawable, null, null, null); - if (Utils.isRTL()) { - rootLayout.setPadding(paddingNoIcon, paddingVertical, paddingHorizontal1, paddingVertical); - } else { - rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingNoIcon, paddingVertical); - } + iconStartView.setVisibility(VISIBLE); + iconStartView.setMaxWidth(iconSize); + iconStartView.setMaxHeight(iconSize); + iconStartView.setLayoutParams(new LayoutParams(iconSize, iconSize)); + iconStartView.setImageDrawable(ContextCompat.getDrawable(getContext(), iconStart)); + + if (Utils.isRTL()) { + rootLayout.setPadding(paddingNoIcon, verticalPadding, paddingHorizontal, verticalPadding); + } else { + rootLayout.setPadding(paddingHorizontal, verticalPadding, paddingNoIcon, verticalPadding); } } if (iconEnd != 0) { - Drawable drawable = ContextCompat.getDrawable(getContext(), iconEnd); - if (drawable != null) { - drawable.setBounds(0, 0, iconSize, iconSize); - TextViewCompat.setCompoundDrawablesRelative(textView, null, null, drawable, null); - if (Utils.isRTL()) { - rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingNoIcon, paddingVertical); - } else { - rootLayout.setPadding(paddingNoIcon, paddingVertical, paddingHorizontal1, paddingVertical); - } + iconEndView.setVisibility(VISIBLE); + iconEndView.setMaxWidth(iconSize); + iconEndView.setMaxHeight(iconSize); + iconEndView.setLayoutParams(new LayoutParams(iconSize, iconSize)); + iconEndView.setImageDrawable(ContextCompat.getDrawable(getContext(), iconEnd)); + + if (Utils.isRTL()) { + rootLayout.setPadding(paddingHorizontal, verticalPadding, paddingNoIcon, verticalPadding); + } else { + rootLayout.setPadding(paddingNoIcon, verticalPadding, paddingHorizontal, verticalPadding); } } if (iconStart != 0 && iconEnd != 0) { - Drawable drawableLeft = ContextCompat.getDrawable(getContext(), iconStart); - Drawable drawableRight = ContextCompat.getDrawable(getContext(), iconEnd); - if (drawableLeft != null && drawableRight != null) { - drawableLeft.setBounds(0, 0, iconSize, iconSize); - drawableRight.setBounds(0, 0, iconSize, iconSize); - textView.setCompoundDrawables(drawableLeft, null, drawableRight, null); - rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingHorizontal1, paddingVertical); - } + rootLayout.setPadding(paddingHorizontal, verticalPadding, paddingHorizontal, verticalPadding); } } /** * loads style attributes from styles.xml if a style resource is used. */ - private void loadShapeAttributes() { if (style == 0) { return; @@ -215,10 +230,12 @@ private void loadShapeAttributes() { int defaultBackgroundColor = ContextCompat.getColor(getContext(), R.color.default_background_color); int defaultCornerRadius = (int) getResources().getDimension(R.dimen.default_corner_radius); + int defaultVerticalPadding = (int) getResources().getDimension(R.dimen.toast_vertical_padding); solidBackground = typedArray.getBoolean(R.styleable.StyleableToast_stSolidBackground, false); backgroundColor = typedArray.getColor(R.styleable.StyleableToast_stColorBackground, defaultBackgroundColor); cornerRadius = (int) typedArray.getDimension(R.styleable.StyleableToast_stRadius, defaultCornerRadius); + verticalPadding = (int) typedArray.getDimension(R.styleable.StyleableToast_stVerticalPadding, defaultVerticalPadding); length = typedArray.getInt(R.styleable.StyleableToast_stLength, 0); gravity = typedArray.getInt(R.styleable.StyleableToast_stGravity, Gravity.BOTTOM); @@ -251,18 +268,24 @@ private void loadIconAttributes() { if (style == 0) { return; } + + int defaultIconSize = (int) getResources().getDimension(R.dimen.default_icon_size); iconStart = typedArray.getResourceId(R.styleable.StyleableToast_stIconStart, 0); iconEnd = typedArray.getResourceId(R.styleable.StyleableToast_stIconEnd, 0); + iconSize = (int) typedArray.getDimension(R.styleable.StyleableToast_stIconSize, defaultIconSize); } public static class Builder { + private final Context context; + @DrawableRes private int iconStart; + @DrawableRes private int iconEnd; + @ColorInt private int textColor; + @ColorInt private int strokeColor; private int cornerRadius = -1; private int backgroundColor; - private int strokeColor; private int strokeWidth; - private int iconStart; - private int iconEnd; - private int textColor; + private int verticalPadding = -1; + private int iconSize = -1; private int font; private int length; private float textSize; @@ -271,7 +294,6 @@ public static class Builder { private String text; private int gravity = Gravity.BOTTOM; private StyleableToast toast; - private final Context context; public Builder(@NonNull Context context) { this.context = context; @@ -324,6 +346,21 @@ public Builder stroke(int strokeWidth, @ColorInt int strokeColor) { return this; } + /** + * @param verticalPadding Sets the vertical padding of the toast. + */ + public Builder verticalPadding(int verticalPadding) { + this.verticalPadding = Utils.toDp(context, verticalPadding); + return this; + } + /** + * @param iconSize Sets the icon size for both start and end icons. + */ + public Builder iconSize(int iconSize) { + this.iconSize = Utils.toDp(context, iconSize); + return this; + } + /** * @param cornerRadius Sets the corner radius of the StyleableToast's shape. */ @@ -376,4 +413,4 @@ public void cancel() { } } } -} +} \ No newline at end of file diff --git a/styleabletoast/src/main/java/io/github/muddz/styleabletoast/Utils.java b/styleabletoast/src/main/java/io/github/muddz/styleabletoast/Utils.java index ce8b1fc..671c4bb 100644 --- a/styleabletoast/src/main/java/io/github/muddz/styleabletoast/Utils.java +++ b/styleabletoast/src/main/java/io/github/muddz/styleabletoast/Utils.java @@ -3,17 +3,18 @@ import android.content.Context; import android.util.TypedValue; +import androidx.annotation.NonNull; import androidx.core.text.TextUtilsCompat; import androidx.core.view.ViewCompat; import java.util.Locale; class Utils { - static int toDp(Context context, int value) { + static int toDp(@NonNull Context context, int value) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()); } static boolean isRTL() { return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL; } -} +} \ No newline at end of file diff --git a/styleabletoast/src/main/res/drawable-v27/shape_toast.xml b/styleabletoast/src/main/res/drawable-v27/shape_toast.xml deleted file mode 100644 index 3ba4b06..0000000 --- a/styleabletoast/src/main/res/drawable-v27/shape_toast.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/styleabletoast/src/main/res/drawable/shape_toast.xml b/styleabletoast/src/main/res/drawable/shape_toast.xml index 992d6b6..a9b0587 100644 --- a/styleabletoast/src/main/res/drawable/shape_toast.xml +++ b/styleabletoast/src/main/res/drawable/shape_toast.xml @@ -1,7 +1,7 @@ + android:shape="rectangle"> - - + + \ No newline at end of file diff --git a/styleabletoast/src/main/res/layout-v27/styleable_layout.xml b/styleabletoast/src/main/res/layout-v27/styleable_layout.xml deleted file mode 100644 index 0b52d77..0000000 --- a/styleabletoast/src/main/res/layout-v27/styleable_layout.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - \ No newline at end of file diff --git a/styleabletoast/src/main/res/layout/styleable_layout.xml b/styleabletoast/src/main/res/layout/styleable_layout.xml index b261da2..6b1e583 100644 --- a/styleabletoast/src/main/res/layout/styleable_layout.xml +++ b/styleabletoast/src/main/res/layout/styleable_layout.xml @@ -1,24 +1,43 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:background="@drawable/shape_toast" + android:gravity="center" + android:orientation="horizontal" + android:paddingHorizontal="@dimen/toast_horizontal_padding" + android:paddingVertical="@dimen/toast_vertical_padding"> - + + + + \ No newline at end of file diff --git a/styleabletoast/src/main/res/values-v27/colors.xml b/styleabletoast/src/main/res/values-v27/colors.xml index 2cb5fc8..a6dc161 100644 --- a/styleabletoast/src/main/res/values-v27/colors.xml +++ b/styleabletoast/src/main/res/values-v27/colors.xml @@ -1,6 +1,6 @@ - #EEEEEE - #de000000 + #EEEEEE + #de000000 \ No newline at end of file diff --git a/styleabletoast/src/main/res/values-v27/dimens.xml b/styleabletoast/src/main/res/values-v27/dimens.xml index 25c78ba..70a20ee 100644 --- a/styleabletoast/src/main/res/values-v27/dimens.xml +++ b/styleabletoast/src/main/res/values-v27/dimens.xml @@ -1,14 +1,14 @@ - 14sp - 22dp + 14sp + 22dp - - 21dp - 23dp + + 21dp + 23dp - 24dp - 15dp + 24dp + 15dp \ No newline at end of file diff --git a/styleabletoast/src/main/res/values/attrs.xml b/styleabletoast/src/main/res/values/attrs.xml index 39bfdbe..a26129a 100644 --- a/styleabletoast/src/main/res/values/attrs.xml +++ b/styleabletoast/src/main/res/values/attrs.xml @@ -13,6 +13,8 @@ + + @@ -23,4 +25,4 @@ - + \ No newline at end of file diff --git a/styleabletoast/src/main/res/values/colors.xml b/styleabletoast/src/main/res/values/colors.xml index 2294290..3539051 100644 --- a/styleabletoast/src/main/res/values/colors.xml +++ b/styleabletoast/src/main/res/values/colors.xml @@ -1,6 +1,6 @@ - #555555 - #ffffff - + #555555 + #ffffff + \ No newline at end of file diff --git a/styleabletoast/src/main/res/values/dimens.xml b/styleabletoast/src/main/res/values/dimens.xml index a29895c..708a50d 100644 --- a/styleabletoast/src/main/res/values/dimens.xml +++ b/styleabletoast/src/main/res/values/dimens.xml @@ -1,16 +1,16 @@ + - 16sp - 30dp + 16sp + 30dp + 36dp - - 20dp - 25dp + + 20dp + 25dp - 25dp - 11.2dp + 25dp + 11.2dp + 8dp - 8dp - 19dp - - + \ No newline at end of file