├── .gitignore
├── StyleableToastDemo.apk
├── build.gradle
├── cases.png
├── demo
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── io
│ │ └── github
│ │ └── muddz
│ │ └── styleabletoast
│ │ └── demo
│ │ └── MainActivity.java
│ └── res
│ ├── drawable
│ ├── ic_autorenew_black_24dp.xml
│ └── ic_autorenew_white_24dp.xml
│ ├── font
│ └── dosis.otf
│ ├── layout
│ └── activity_main.xml
│ ├── values-v27
│ └── styles.xml
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── license
├── readme.md
├── settings.gradle
└── styleabletoast
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
├── java
└── io
│ └── github
│ └── muddz
│ └── styleabletoast
│ ├── StyleableToast.java
│ └── Utils.java
└── res
├── drawable-v27
└── shape_toast.xml
├── drawable
└── shape_toast.xml
├── layout-v27
└── styleable_layout.xml
├── layout
└── styleable_layout.xml
├── values-v27
├── colors.xml
└── dimens.xml
└── values
├── attrs.xml
├── colors.xml
├── dimens.xml
└── integers.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | .idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 | .cxx
10 | local.properties
11 |
--------------------------------------------------------------------------------
/StyleableToastDemo.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Muddz/StyleableToast/48883c4ec1addba90e061b9a3699ca6680559a23/StyleableToastDemo.apk
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | mavenCentral()
7 | }
8 |
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:7.0.0'
11 | classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.5.0'
12 | classpath 'com.vanniktech:gradle-maven-publish-plugin:0.17.0'
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | google()
19 | mavenCentral()
20 | }
21 | plugins.withId("com.vanniktech.maven.publish") {
22 | mavenPublish {
23 | sonatypeHost = "S01"
24 | }
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
--------------------------------------------------------------------------------
/cases.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Muddz/StyleableToast/48883c4ec1addba90e061b9a3699ca6680559a23/cases.png
--------------------------------------------------------------------------------
/demo/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/demo/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | }
4 |
5 | android {
6 | compileSdk 30
7 |
8 | defaultConfig {
9 | applicationId "io.github.muddz.styleabletoast.demo"
10 | minSdk 16
11 | targetSdk 30
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 |
16 | buildFeatures {
17 | dataBinding true
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | implementation 'androidx.appcompat:appcompat:1.3.1'
30 | implementation 'com.google.android.material:material:1.4.0'
31 | implementation 'io.github.muddz:styleabletoast:2.4.0'
32 | // implementation project(':styleabletoast')
33 | }
34 |
--------------------------------------------------------------------------------
/demo/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/demo/src/main/java/io/github/muddz/styleabletoast/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package io.github.muddz.styleabletoast.demo;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 |
6 | import androidx.appcompat.app.AppCompatActivity;
7 | import androidx.databinding.DataBindingUtil;
8 |
9 | import io.github.muddz.styleabletoast.StyleableToast;
10 | import io.github.muddz.styleabletoast.demo.databinding.ActivityMainBinding;
11 |
12 |
13 | public class MainActivity extends AppCompatActivity {
14 |
15 | String toastMsg = "Hello World!";
16 | int redColor = Color.parseColor("#FF5A5F");
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
22 | binding.setMain(this);
23 |
24 | }
25 |
26 | public void coloredBackground() {
27 | StyleableToast.Builder st = new StyleableToast.Builder(this)
28 | .text(toastMsg)
29 | .backgroundColor(redColor)
30 | .build();
31 |
32 | st.show();
33 | }
34 |
35 | public boolean coloredBackgroundXML() {
36 | StyleableToast.makeText(this, toastMsg, R.style.ColoredBackground).show();
37 | return true;
38 | }
39 |
40 |
41 | public void coloredText() {
42 | new StyleableToast.Builder(this)
43 | .text(toastMsg)
44 | .textColor(redColor)
45 | .show();
46 | }
47 |
48 | public boolean coloredTextXML() {
49 | StyleableToast.makeText(this, toastMsg, R.style.ColoredText).show();
50 | return true;
51 | }
52 |
53 |
54 | public void coloredBoldText() {
55 | new StyleableToast.Builder(this)
56 | .text(toastMsg)
57 | .textColor(redColor)
58 | .textBold()
59 | .show();
60 | }
61 |
62 | public boolean coloredBoldTextXML() {
63 | StyleableToast.makeText(this, toastMsg, R.style.ColoredBoldText).show();
64 | return true;
65 | }
66 |
67 |
68 | public void customFont() {
69 | new StyleableToast.Builder(this)
70 | .text(toastMsg)
71 | .font(R.font.dosis)
72 | .show();
73 | }
74 |
75 | public boolean customFontXML() {
76 | StyleableToast.makeText(this, toastMsg, R.style.CustomFont).show();
77 | return true;
78 | }
79 |
80 | public void cornerRadius() {
81 | new StyleableToast.Builder(this)
82 | .text(toastMsg)
83 | .cornerRadius(5)
84 | .show();
85 | }
86 |
87 | public boolean cornerRadiusXML() {
88 | StyleableToast.makeText(this, toastMsg, R.style.CornerRadius5Dp).show();
89 | return true;
90 | }
91 |
92 | public void iconStart() {
93 | new StyleableToast.Builder(this)
94 | .text(toastMsg)
95 | .iconStart(getIcon())
96 | .show();
97 | }
98 |
99 | public boolean iconStartXML() {
100 | StyleableToast.makeText(this, toastMsg, R.style.IconStart).show();
101 | return true;
102 | }
103 |
104 |
105 | public void iconEnd() {
106 | new StyleableToast.Builder(this)
107 | .text(toastMsg)
108 | .iconEnd(getIcon())
109 | .show();
110 | }
111 |
112 | public boolean iconEndXML() {
113 | StyleableToast.makeText(this, toastMsg, R.style.IconEnd).show();
114 | return true;
115 | }
116 |
117 |
118 | public void iconStartEnd() {
119 | new StyleableToast.Builder(this)
120 | .text(toastMsg)
121 | .iconStart(getIcon())
122 | .iconEnd(getIcon())
123 | .show();
124 | }
125 |
126 | public boolean iconStartEndXML() {
127 | StyleableToast.makeText(this, toastMsg, R.style.IconStartEnd).show();
128 | return true;
129 | }
130 |
131 | public void coloredStroke() {
132 | new StyleableToast.Builder(this)
133 | .text(toastMsg)
134 | .stroke(2, redColor)
135 | .show();
136 | }
137 |
138 | public boolean coloredStrokeXML() {
139 | StyleableToast.makeText(this, toastMsg, R.style.ColoredStroke).show();
140 | return true;
141 | }
142 |
143 | public void allStyles() {
144 | new StyleableToast.Builder(this)
145 | .text(toastMsg)
146 | .stroke(2, Color.BLACK)
147 | .backgroundColor(Color.WHITE)
148 | .solidBackground()
149 | .textColor(Color.RED)
150 | .textBold()
151 | .font(R.font.dosis)
152 | .iconStart(getIcon())
153 | .iconEnd(getIcon())
154 | .cornerRadius(12)
155 | .textSize(18)
156 | .show();
157 | }
158 |
159 | public boolean allStylesXML() {
160 | StyleableToast.makeText(this, toastMsg, R.style.AllStyles).show();
161 | return true;
162 | }
163 |
164 | public int getIcon() {
165 | if (android.os.Build.VERSION.SDK_INT >= 27) {
166 | return R.drawable.ic_autorenew_black_24dp;
167 | } else {
168 | return R.drawable.ic_autorenew_white_24dp;
169 | }
170 | }
171 |
172 | }
173 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/ic_autorenew_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/ic_autorenew_white_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/demo/src/main/res/font/dosis.otf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Muddz/StyleableToast/48883c4ec1addba90e061b9a3699ca6680559a23/demo/src/main/res/font/dosis.otf
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
14 |
15 |
23 |
24 |
28 |
29 |
37 |
38 |
44 |
45 |
51 |
52 |
53 |
59 |
60 |
66 |
67 |
73 |
74 |
80 |
81 |
87 |
88 |
94 |
95 |
101 |
102 |
108 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-v27/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
17 |
18 |
32 |
33 |
34 |
37 |
38 |
42 |
43 |
46 |
47 |
50 |
51 |
55 |
56 |
59 |
60 |
63 |
64 |
67 |
68 |
71 |
72 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | StyleableToast demo
3 |
4 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
17 |
18 |
32 |
33 |
34 |
37 |
38 |
42 |
43 |
46 |
47 |
50 |
51 |
55 |
56 |
59 |
60 |
63 |
64 |
67 |
68 |
71 |
72 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | android.useAndroidX=true
2 | android.enableJetifier=true
3 |
4 | #MavenCentral publish informations
5 | #Publish plugin: https://github.com/vanniktech/gradle-maven-publish-plugin
6 | GROUP=io.github.muddz
7 | POM_ARTIFACT_ID=styleabletoast
8 | VERSION_NAME=2.4.0
9 |
10 | POM_NAME=styleabletoast
11 | POM_DESCRIPTION=An Android library that takes the standard toast to the next level with many styling options. Works on all Android versions.
12 | POM_INCEPTION_YEAR=2021
13 | POM_URL=https://github.com/Muddz/StyleableToast
14 |
15 | POM_LICENSE_NAME=The Apache Software License, Version 2.0
16 | POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
17 | POM_LICENSE_DIST=repo
18 |
19 | POM_SCM_URL=https://github.com/Muddz/StyleableToast
20 | POM_SCM_CONNECTION=scm:git:git://github.com/Muddz/StyleableToast.git
21 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/Muddz/StyleableToast.git
22 |
23 | POM_DEVELOPER_ID=Muddz
24 | POM_DEVELOPER_NAME=Muddi Walid
25 | POM_DEVELOPER_URL=https://github.com/Muddz
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Muddz/StyleableToast/48883c4ec1addba90e061b9a3699ca6680559a23/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
3 | distributionPath=wrapper/dists
4 | zipStorePath=wrapper/dists
5 | zipStoreBase=GRADLE_USER_HOME
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/license:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | Copyright 2016 Muddi Walid.
179 |
180 | Licensed under the Apache License, Version 2.0 (the "License");
181 | you may not use this file except in compliance with the License.
182 | You may obtain a copy of the License at
183 |
184 | http://www.apache.org/licenses/LICENSE-2.0
185 |
186 | Unless required by applicable law or agreed to in writing, software
187 | distributed under the License is distributed on an "AS IS" BASIS,
188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189 | See the License for the specific language governing permissions and
190 | limitations under the License.
191 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # StyleableToast
2 | [](https://android-arsenal.com/api?level=16#l16)
3 | [](https://github.com/Muddz/StyleableToast/raw/master/StyleableToastDemo.apk)
4 |
5 | An Android library that takes the standard toast to the next level with many styling options. Style your toasts either by code or with a style in `styles.xml`.
6 | ## Cases
7 |
8 |
9 |
10 |
11 | ## Example with a style
12 |
13 | 1) Define a style in `styles.xml`. All available attributes:
14 | ```xml
15 |
30 |
31 | ```
32 |
33 | 2) Pass your style in the static constructor and call `show();`
34 |
35 | ```java
36 | StyleableToast.makeText(context, "Hello World!", Toast.LENGTH_LONG, R.style.mytoast).show();
37 | ```
38 |
39 | ## Example with builder pattern
40 | ```java
41 | new StyleableToast
42 | .Builder(context)
43 | .text("Hello world!")
44 | .textColor(Color.WHITE)
45 | .backgroundColor(Color.BLUE)
46 | .show();
47 | ```
48 |
49 |
50 | ## Installation
51 |
52 | Add the dependency in your `build.gradle`
53 | ```groovy
54 | dependencies {
55 | implementation 'io.github.muddz:styleabletoast:2.4.0'
56 | }
57 | ```
58 |
59 | ## License
60 |
61 | Copyright 2016 Muddi Walid
62 |
63 | Licensed under the Apache License, Version 2.0 (the "License");
64 | you may not use this file except in compliance with the License.
65 | You may obtain a copy of the License at
66 |
67 | http://www.apache.org/licenses/LICENSE-2.0
68 |
69 | Unless required by applicable law or agreed to in writing, software
70 | distributed under the License is distributed on an "AS IS" BASIS,
71 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72 | See the License for the specific language governing permissions and
73 | limitations under the License.
74 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':demo', ':styleabletoast'
2 |
--------------------------------------------------------------------------------
/styleabletoast/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/styleabletoast/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'com.vanniktech.maven.publish'
4 | }
5 |
6 | android {
7 | compileSdk 30
8 |
9 | defaultConfig {
10 | minSdk 16
11 | targetSdk 30
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation 'androidx.appcompat:appcompat:1.3.1'
23 | }
24 |
--------------------------------------------------------------------------------
/styleabletoast/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/java/io/github/muddz/styleabletoast/StyleableToast.java:
--------------------------------------------------------------------------------
1 | package io.github.muddz.styleabletoast;
2 |
3 |
4 | import android.annotation.SuppressLint;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Color;
8 | import android.graphics.Typeface;
9 | import android.graphics.drawable.Drawable;
10 | import android.graphics.drawable.GradientDrawable;
11 | import android.util.TypedValue;
12 | import android.view.Gravity;
13 | import android.view.View;
14 | import android.widget.LinearLayout;
15 | import android.widget.TextView;
16 | import android.widget.Toast;
17 |
18 | import androidx.annotation.ColorInt;
19 | import androidx.annotation.DrawableRes;
20 | import androidx.annotation.FontRes;
21 | import androidx.annotation.NonNull;
22 | import androidx.annotation.StyleRes;
23 | import androidx.core.content.ContextCompat;
24 | import androidx.core.content.res.ResourcesCompat;
25 | import androidx.core.widget.TextViewCompat;
26 |
27 | @SuppressLint("ViewConstructor")
28 | public class StyleableToast extends LinearLayout {
29 |
30 | private int cornerRadius;
31 | private int backgroundColor;
32 | private int strokeColor;
33 | private int strokeWidth;
34 | private int iconStart;
35 | private int iconEnd;
36 | private int textColor;
37 | private int font;
38 | private int length;
39 | private int style;
40 | private float textSize;
41 | private boolean isTextSizeFromStyleXml = false;
42 | private boolean solidBackground;
43 | private boolean textBold;
44 | private String text;
45 | private TypedArray typedArray;
46 | private TextView textView;
47 | private int gravity;
48 | private Toast toast;
49 | private LinearLayout rootLayout;
50 |
51 | public static StyleableToast makeText(@NonNull Context context, String text, int length, @StyleRes int style) {
52 | return new StyleableToast(context, text, length, style);
53 | }
54 |
55 | public static StyleableToast makeText(@NonNull Context context, String text, @StyleRes int style) {
56 | return new StyleableToast(context, text, Toast.LENGTH_SHORT, style);
57 | }
58 |
59 | private StyleableToast(@NonNull Context context, String text, int length, @StyleRes int style) {
60 | super(context);
61 | this.text = text;
62 | this.length = length;
63 | this.style = style;
64 | }
65 |
66 | private StyleableToast(Builder builder) {
67 | super(builder.context);
68 | this.backgroundColor = builder.backgroundColor;
69 | this.cornerRadius = builder.cornerRadius;
70 | this.iconEnd = builder.iconEnd;
71 | this.iconStart = builder.iconStart;
72 | this.strokeColor = builder.strokeColor;
73 | this.strokeWidth = builder.strokeWidth;
74 | this.solidBackground = builder.solidBackground;
75 | this.textColor = builder.textColor;
76 | this.textSize = builder.textSize;
77 | this.textBold = builder.textBold;
78 | this.font = builder.font;
79 | this.text = builder.text;
80 | this.gravity = builder.gravity;
81 | this.length = builder.length;
82 | }
83 |
84 | private void inflateToastLayout() {
85 | View v = inflate(getContext(), R.layout.styleable_layout, null);
86 | rootLayout = (LinearLayout) v.getRootView();
87 | textView = v.findViewById(R.id.textview);
88 | if (style > 0) {
89 | typedArray = getContext().obtainStyledAttributes(style, R.styleable.StyleableToast);
90 | }
91 |
92 | makeShape();
93 | makeTextView();
94 | makeIcon();
95 |
96 | // Very important to recycle AFTER the make() methods!
97 | if (typedArray != null) {
98 | typedArray.recycle();
99 | }
100 | }
101 |
102 | private void createAndShowToast() {
103 | inflateToastLayout();
104 | toast = new Toast(getContext());
105 | toast.setGravity(gravity, 0, gravity == Gravity.CENTER ? 0 : toast.getYOffset());
106 | toast.setDuration(length == Toast.LENGTH_LONG ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
107 | toast.setView(rootLayout);
108 | toast.show();
109 | }
110 |
111 | public void show() {
112 | createAndShowToast();
113 | }
114 |
115 |
116 | public void cancel() {
117 | if (toast != null) {
118 | toast.cancel();
119 | }
120 | }
121 |
122 |
123 | private void makeShape() {
124 | loadShapeAttributes();
125 | GradientDrawable gradientDrawable = (GradientDrawable) rootLayout.getBackground().mutate();
126 | gradientDrawable.setAlpha(getResources().getInteger(R.integer.defaultBackgroundAlpha));
127 |
128 | if (strokeWidth > 0) {
129 | gradientDrawable.setStroke(strokeWidth, strokeColor);
130 | }
131 | if (cornerRadius > -1) {
132 | gradientDrawable.setCornerRadius(cornerRadius);
133 | }
134 | if (backgroundColor != 0) {
135 | gradientDrawable.setColor(backgroundColor);
136 | }
137 | if (solidBackground) {
138 | gradientDrawable.setAlpha(getResources().getInteger(R.integer.fullBackgroundAlpha));
139 | }
140 |
141 | rootLayout.setBackground(gradientDrawable);
142 | }
143 |
144 | private void makeTextView() {
145 | loadTextViewAttributes();
146 | textView.setText(text);
147 | if (textColor != 0) {
148 | textView.setTextColor(textColor);
149 | }
150 | if (textSize > 0) {
151 | textView.setTextSize(isTextSizeFromStyleXml ? 0 : TypedValue.COMPLEX_UNIT_SP, textSize);
152 | }
153 | if (font > 0) {
154 | textView.setTypeface(ResourcesCompat.getFont(getContext(), font), textBold ? Typeface.BOLD : Typeface.NORMAL);
155 | }
156 | if (textBold && font == 0) {
157 | textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
158 | }
159 | }
160 |
161 |
162 | private void makeIcon() {
163 | loadIconAttributes();
164 | int paddingVertical = (int) getResources().getDimension(R.dimen.toast_vertical_padding);
165 | int paddingHorizontal1 = (int) getResources().getDimension(R.dimen.toast_horizontal_padding_icon_side);
166 | int paddingNoIcon = (int) getResources().getDimension(R.dimen.toast_horizontal_padding_empty_side);
167 | int iconSize = (int) getResources().getDimension(R.dimen.icon_size);
168 |
169 | if (iconStart != 0) {
170 | Drawable drawable = ContextCompat.getDrawable(getContext(), iconStart);
171 | if (drawable != null) {
172 | drawable.setBounds(0, 0, iconSize, iconSize);
173 | TextViewCompat.setCompoundDrawablesRelative(textView, drawable, null, null, null);
174 | if (Utils.isRTL()) {
175 | rootLayout.setPadding(paddingNoIcon, paddingVertical, paddingHorizontal1, paddingVertical);
176 | } else {
177 | rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingNoIcon, paddingVertical);
178 | }
179 | }
180 | }
181 |
182 | if (iconEnd != 0) {
183 | Drawable drawable = ContextCompat.getDrawable(getContext(), iconEnd);
184 | if (drawable != null) {
185 | drawable.setBounds(0, 0, iconSize, iconSize);
186 | TextViewCompat.setCompoundDrawablesRelative(textView, null, null, drawable, null);
187 | if (Utils.isRTL()) {
188 | rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingNoIcon, paddingVertical);
189 | } else {
190 | rootLayout.setPadding(paddingNoIcon, paddingVertical, paddingHorizontal1, paddingVertical);
191 | }
192 | }
193 | }
194 |
195 | if (iconStart != 0 && iconEnd != 0) {
196 | Drawable drawableLeft = ContextCompat.getDrawable(getContext(), iconStart);
197 | Drawable drawableRight = ContextCompat.getDrawable(getContext(), iconEnd);
198 | if (drawableLeft != null && drawableRight != null) {
199 | drawableLeft.setBounds(0, 0, iconSize, iconSize);
200 | drawableRight.setBounds(0, 0, iconSize, iconSize);
201 | textView.setCompoundDrawables(drawableLeft, null, drawableRight, null);
202 | rootLayout.setPadding(paddingHorizontal1, paddingVertical, paddingHorizontal1, paddingVertical);
203 | }
204 | }
205 | }
206 |
207 | /**
208 | * loads style attributes from styles.xml if a style resource is used.
209 | */
210 |
211 | private void loadShapeAttributes() {
212 | if (style == 0) {
213 | return;
214 | }
215 |
216 | int defaultBackgroundColor = ContextCompat.getColor(getContext(), R.color.default_background_color);
217 | int defaultCornerRadius = (int) getResources().getDimension(R.dimen.default_corner_radius);
218 |
219 | solidBackground = typedArray.getBoolean(R.styleable.StyleableToast_stSolidBackground, false);
220 | backgroundColor = typedArray.getColor(R.styleable.StyleableToast_stColorBackground, defaultBackgroundColor);
221 | cornerRadius = (int) typedArray.getDimension(R.styleable.StyleableToast_stRadius, defaultCornerRadius);
222 | length = typedArray.getInt(R.styleable.StyleableToast_stLength, 0);
223 | gravity = typedArray.getInt(R.styleable.StyleableToast_stGravity, Gravity.BOTTOM);
224 |
225 | if (gravity == 1) {
226 | gravity = Gravity.CENTER;
227 | } else if (gravity == 2) {
228 | gravity = Gravity.TOP;
229 | }
230 |
231 | if (typedArray.hasValue(R.styleable.StyleableToast_stStrokeColor) && typedArray.hasValue(R.styleable.StyleableToast_stStrokeWidth)) {
232 | strokeWidth = (int) typedArray.getDimension(R.styleable.StyleableToast_stStrokeWidth, 0);
233 | strokeColor = typedArray.getColor(R.styleable.StyleableToast_stStrokeColor, Color.TRANSPARENT);
234 | }
235 | }
236 |
237 | private void loadTextViewAttributes() {
238 | if (style == 0) {
239 | return;
240 | }
241 |
242 | textColor = typedArray.getColor(R.styleable.StyleableToast_stTextColor, textView.getCurrentTextColor());
243 | textBold = typedArray.getBoolean(R.styleable.StyleableToast_stTextBold, false);
244 | textSize = typedArray.getDimension(R.styleable.StyleableToast_stTextSize, 0);
245 | font = typedArray.getResourceId(R.styleable.StyleableToast_stFont, 0);
246 | isTextSizeFromStyleXml = textSize > 0;
247 | }
248 |
249 |
250 | private void loadIconAttributes() {
251 | if (style == 0) {
252 | return;
253 | }
254 | iconStart = typedArray.getResourceId(R.styleable.StyleableToast_stIconStart, 0);
255 | iconEnd = typedArray.getResourceId(R.styleable.StyleableToast_stIconEnd, 0);
256 | }
257 |
258 | public static class Builder {
259 | private int cornerRadius = -1;
260 | private int backgroundColor;
261 | private int strokeColor;
262 | private int strokeWidth;
263 | private int iconStart;
264 | private int iconEnd;
265 | private int textColor;
266 | private int font;
267 | private int length;
268 | private float textSize;
269 | private boolean solidBackground;
270 | private boolean textBold;
271 | private String text;
272 | private int gravity = Gravity.BOTTOM;
273 | private StyleableToast toast;
274 | private final Context context;
275 |
276 | public Builder(@NonNull Context context) {
277 | this.context = context;
278 | }
279 |
280 | public Builder text(String text) {
281 | this.text = text;
282 | return this;
283 | }
284 |
285 | public Builder textColor(@ColorInt int textColor) {
286 | this.textColor = textColor;
287 | return this;
288 | }
289 |
290 | public Builder textBold() {
291 | this.textBold = true;
292 | return this;
293 | }
294 |
295 | public Builder textSize(float textSize) {
296 | this.textSize = textSize;
297 | return this;
298 | }
299 |
300 | /**
301 | * @param font A font resource id like R.font.somefont as introduced with the new font api in Android 8
302 | */
303 | public Builder font(@FontRes int font) {
304 | this.font = font;
305 | return this;
306 | }
307 |
308 | public Builder backgroundColor(@ColorInt int backgroundColor) {
309 | this.backgroundColor = backgroundColor;
310 | return this;
311 | }
312 |
313 | /**
314 | * This call will make the StyleableToast's background completely solid without any opacity.
315 | */
316 | public Builder solidBackground() {
317 | this.solidBackground = true;
318 | return this;
319 | }
320 |
321 | public Builder stroke(int strokeWidth, @ColorInt int strokeColor) {
322 | this.strokeWidth = Utils.toDp(context, strokeWidth);
323 | this.strokeColor = strokeColor;
324 | return this;
325 | }
326 |
327 | /**
328 | * @param cornerRadius Sets the corner radius of the StyleableToast's shape.
329 | */
330 | public Builder cornerRadius(int cornerRadius) {
331 | this.cornerRadius = Utils.toDp(context, cornerRadius);
332 | return this;
333 | }
334 |
335 | public Builder iconStart(@DrawableRes int iconStart) {
336 | this.iconStart = iconStart;
337 | return this;
338 | }
339 |
340 | public Builder iconEnd(@DrawableRes int iconEnd) {
341 | this.iconEnd = iconEnd;
342 | return this;
343 | }
344 |
345 | /**
346 | * Sets where the StyleableToast will appear on the screen
347 | */
348 | public Builder gravity(int gravity) {
349 | this.gravity = gravity;
350 | return this;
351 | }
352 |
353 | /**
354 | * @param length {@link Toast#LENGTH_SHORT} or {@link Toast#LENGTH_LONG}
355 | */
356 | public Builder length(int length) {
357 | this.length = length;
358 | return this;
359 | }
360 |
361 | /**
362 | * @return an mutable instance of the build
363 | */
364 | public Builder build() {
365 | return this;
366 | }
367 |
368 | public void show() {
369 | toast = new StyleableToast(this);
370 | toast.show();
371 | }
372 |
373 | public void cancel() {
374 | if (toast != null) {
375 | toast.cancel();
376 | }
377 | }
378 | }
379 | }
380 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/java/io/github/muddz/styleabletoast/Utils.java:
--------------------------------------------------------------------------------
1 | package io.github.muddz.styleabletoast;
2 |
3 | import android.content.Context;
4 | import android.util.TypedValue;
5 |
6 | import androidx.core.text.TextUtilsCompat;
7 | import androidx.core.view.ViewCompat;
8 |
9 | import java.util.Locale;
10 |
11 | class Utils {
12 | static int toDp(Context context, int value) {
13 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
14 | }
15 |
16 | static boolean isRTL() {
17 | return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/drawable-v27/shape_toast.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/drawable/shape_toast.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/layout-v27/styleable_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
22 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/layout/styleable_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
23 |
24 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values-v27/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #EEEEEE
5 | #de000000
6 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values-v27/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 14sp
5 | 22dp
6 |
7 |
8 | 21dp
9 | 23dp
10 |
11 | 24dp
12 | 15dp
13 |
14 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #555555
5 | #ffffff
6 |
7 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16sp
4 | 30dp
5 |
6 |
7 | 20dp
8 | 25dp
9 |
10 | 25dp
11 | 11.2dp
12 |
13 | 8dp
14 | 19dp
15 |
16 |
17 |
--------------------------------------------------------------------------------
/styleabletoast/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 255
4 | 230
5 |
--------------------------------------------------------------------------------