├── .github
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ └── issue_template.md
├── PULL_REQUEST_TEMPLATE.md
└── workflows
│ └── build.yml
├── .gitignore
├── .travis.yml
├── CNAME
├── CODE_OF_CONDUCT.md
├── LICENSE
├── README.md
├── build.gradle
├── dynamic-toasts
├── build.gradle
├── maven.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── pranavpandey
│ │ └── android
│ │ └── dynamic
│ │ └── toasts
│ │ ├── DynamicHint.java
│ │ ├── DynamicToast.java
│ │ └── internal
│ │ ├── ToastCompat.java
│ │ └── ToastContext.java
│ └── res
│ ├── drawable-hdpi
│ ├── adt_ic_error.png
│ ├── adt_ic_success.png
│ └── adt_ic_warning.png
│ ├── drawable-mdpi
│ ├── adt_ic_error.png
│ ├── adt_ic_success.png
│ └── adt_ic_warning.png
│ ├── drawable-xhdpi
│ ├── adt_ic_error.png
│ ├── adt_ic_success.png
│ └── adt_ic_warning.png
│ ├── drawable-xxhdpi
│ ├── adt_ic_error.png
│ ├── adt_ic_success.png
│ └── adt_ic_warning.png
│ ├── drawable-xxxhdpi
│ ├── adt_ic_error.png
│ ├── adt_ic_success.png
│ └── adt_ic_warning.png
│ ├── drawable
│ ├── adt_hint_background.xml
│ └── adt_toast_background.xml
│ ├── layout
│ ├── adt_layout_hint.xml
│ └── adt_layout_toast.xml
│ └── values
│ └── dimens.xml
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── graphics
├── icon.png
├── icon.psd
├── legacy
│ ├── icon-alt.png
│ ├── icon.png
│ └── icon.psd
├── play
│ ├── feature-graphic.png
│ ├── promo-graphic.png
│ └── screenshots
│ │ ├── phone-1.png
│ │ ├── phone-2.png
│ │ ├── phone-3.png
│ │ ├── phone-4.png
│ │ ├── phone-5.png
│ │ ├── phone-6.png
│ │ ├── phone-7.png
│ │ └── phone-8.png
├── preview.png
└── preview.psd
├── sample
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── pranavpandey
│ │ └── android
│ │ └── dynamic
│ │ └── toasts
│ │ └── sample
│ │ ├── DynamicToastsActivity.kt
│ │ └── dialog
│ │ └── AboutDialogFragment.kt
│ └── res
│ ├── drawable
│ ├── app_bar_shadow.xml
│ ├── bg_custom_toast.xml
│ ├── ic_info.xml
│ ├── ic_launcher_foreground.xml
│ ├── ic_social_github.xml
│ └── ic_toast_icon.xml
│ ├── layout
│ ├── activity_dynamic_toasts.xml
│ ├── content_dynamic_toasts.xml
│ └── dialog_about.xml
│ ├── menu
│ └── main.xml
│ ├── mipmap-anydpi-v26
│ └── ic_launcher.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── settings.gradle
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: pranavpandey
4 | patreon: pranavpandey
5 | open_collective: pranavpandeydev
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | otechie: # Replace with a single Otechie username
12 | custom: ['paypal.me/pranavpandeydev']
13 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/issue_template.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Issue
3 | about: Create a issue to help us improve
4 | title: "Short description of the issue"
5 | ---
6 |
7 | **Description:** Full description of the issue.
8 |
9 | **Expected behavior:** Screenshots and/or description of the expected behavior.
10 |
11 | **Source code:** [OPTIONAL] The code snippet which is causing this issue.
12 |
13 | **Sample app and/or repro:** [OPTIONAL] A sample app or steps to reproduce the issue. You may attach a `zip` or `APK` file of the sample app or a link to the GitHub repository.
14 |
15 | **Android API version:** Android API version. `API 19`
16 |
17 | **Library version:** The Library version you are using. `1.0.0`
18 |
19 | **Device:** Device on which the bug was encountered. `Emulator or Brand Model`
20 |
21 | *Please make sure that you are using the [latest version](https://github.com/pranavpandey/dynamic-toasts/releases) of the library and we also accept [pull requests](https://github.com/pranavpandey/dynamic-toasts/pulls).*
22 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ### Thanks for starting a pull request!
2 |
3 | ## Changes
4 |
5 | -
6 | -
7 |
8 | ## Testing
9 |
10 | Describe how you tested your changes.
11 |
12 | ## Issues
13 |
14 | [OPTIONAL] Link to GitHub issues it solves. `Resolve #1234`
15 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 |
8 | jobs:
9 | build:
10 | runs-on: ubuntu-latest
11 | timeout-minutes: 60
12 |
13 | steps:
14 | - name: Checkout
15 | uses: actions/checkout@v3
16 |
17 | - name: Setup JDK 17
18 | uses: actions/setup-java@v3
19 | with:
20 | distribution: 'zulu'
21 | java-version: 17
22 |
23 | - name: Setup Gradle
24 | uses: gradle/gradle-build-action@v2
25 |
26 | - name: Build with Gradle
27 | run: |
28 | chmod +x gradlew
29 | ./gradlew build
30 |
31 | - name: Generate Javadoc
32 | if: github.ref_type == 'tag'
33 | run: ./gradlew generateJavadoc
34 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | release/
19 |
20 | # IntelliJ project files
21 | **.iml
22 | .idea
23 |
24 | # Android Studio captures folder
25 | captures/
26 |
27 | # Local configuration file (sdk path, etc)
28 | local.properties
29 |
30 | # Proguard folder generated by Eclipse
31 | proguard/
32 |
33 | # Log Files
34 | *.log
35 |
36 | # Misc
37 | .DS_Store
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk: oraclejdk17
3 |
4 | before_install:
5 | - mkdir "$ANDROID_HOME/licenses" || true
6 | - echo -e "\n24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"
7 | - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"
8 |
9 | android:
10 | components:
11 | - tools
12 | - platform-tools
13 | - build-tools-35.0.0
14 | - android-35
15 | - extra-android-support
16 | - extra-android-m2repository
17 | - extra-google-m2repository
18 | before_script:
19 | - chmod +x gradlew
20 | script:
21 | - ./gradlew build
22 |
23 | after_success:
24 | - ./gradlew generateJavadoc
25 |
26 | deploy:
27 | provider: pages
28 | token: $GITHUB_TOKEN
29 | edge: true
30 | keep_history: true
31 | local_dir: dynamic-toasts/build/docs/javadoc/release
32 | on:
33 | branch: master
34 | tags: true
35 |
--------------------------------------------------------------------------------
/CNAME:
--------------------------------------------------------------------------------
1 | pranavpandey.org
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, caste, color, religion, or sexual
10 | identity and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the overall
26 | community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or advances of
31 | any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email address,
35 | without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | support@pranavpandey.com.
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series of
86 | actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or permanent
93 | ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within the
113 | community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.1, available at
119 | [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120 |
121 | Community Impact Guidelines were inspired by
122 | [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123 |
124 | For answers to common questions about this code of conduct, see the FAQ at
125 | [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126 | [https://www.contributor-covenant.org/translations][translations].
127 |
128 | [homepage]: https://www.contributor-covenant.org
129 | [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130 | [Mozilla CoC]: https://github.com/mozilla/diversity
131 | [FAQ]: https://www.contributor-covenant.org/faq
132 | [translations]: https://www.contributor-covenant.org/translations
133 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2017-2024 Pranav Pandey
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # Dynamic Toasts
4 |
5 | [](https://www.apache.org/licenses/LICENSE-2.0.html)
6 | [](https://travis-ci.org/pranavpandey/dynamic-toasts)
7 | [](https://search.maven.org/artifact/com.pranavpandey.android/dynamic-toasts)
8 |
9 | **A simple library to display themed toasts with icon and text on Android 2.3 (API 9) and above.**
10 |
11 | > [!IMPORTANT]
12 | > Since v0.4.0, it uses [26.x.x support libraries][android-support] so, minimum SDK will be
13 | Android 4.0 (API 14).
14 |
Since v2.0.0, it uses [AndroidX][androidx] so, first [migrate][androidx-migrate] your
15 | project to AndroidX.
16 |
Since v4.1.0, it is dependent on Java 8 due to the dependency on
17 | [Dynamic Utils][dynamic-utils].
18 |
Since v4.2.0, it is targeting Java 17 to provide maximum compatibility.
19 |
Since v4.3.0, the minimum SDK is Android 4.4 (API 19) to comply with the latest policies.
20 |
21 |
22 |
23 | ---
24 |
25 | ## Contents
26 |
27 | - [Installation](#installation)
28 | - [Usage](#usage)
29 | - [Configuration](#configuration)
30 | - [Default toast](#default-toast)
31 | - [Default toast with duration](#default-toast-with-duration)
32 | - [Default toast with icon](#default-toast-with-icon)
33 | - [Default toast with icon and duration](#default-toast-with-icon-and-duration)
34 | - [Error toast](#error-toast)
35 | - [Error toast with duration](#error-toast-with-duration)
36 | - [Success toast](#success-toast)
37 | - [Success toast with duration](#success-toast-with-duration)
38 | - [Warning toast](#warning-toast)
39 | - [Warning toast with duration](#warning-toast-with-duration)
40 | - [Custom toast](#custom-toast)
41 | - [Custom toast with duration](#custom-toast-with-duration)
42 | - [Custom toast with icon](#custom-toast-with-icon)
43 | - [Custom toast with icon and duration](#custom-toast-with-icon-and-duration)
44 | - [Cheat sheets](#cheat-sheets)
45 | - [Dependency](#dependency)
46 | - [License](#license)
47 |
48 | ---
49 |
50 | ## Installation
51 |
52 | It can be installed by adding the following dependency to your `build.gradle` file:
53 |
54 | ```groovy
55 | dependencies {
56 | // For AndroidX enabled projects.
57 | implementation 'com.pranavpandey.android:dynamic-toasts:4.3.0'
58 |
59 | // For legacy projects.
60 | implementation 'com.pranavpandey.android:dynamic-toasts:1.3.0'
61 | }
62 | ```
63 |
64 | ---
65 |
66 | ## Usage
67 |
68 | It has several method to display toasts based on the requirement. Each method returns a `Toast`
69 | object which can be customised further.
70 |
71 | Please call `show()` method to display the toast.
72 |
73 | > For a complete reference, please read the [documentation][documentation].
74 |
75 | ### Configuration
76 |
77 | Optional configuration to customise the toasts further like custom background color or drawable,
78 | custom text size, typeface or icon size, etc.
79 |
80 | Various methods can be called anywhere in the app to do customisations.
81 |
82 | ```java
83 | DynamicToast.Config.getInstance()
84 | // Background color for default toast.
85 | .setDefaultBackgroundColor(@ColorInt int defaultBackgroundColor)
86 | // Tint color for default toast.
87 | .setDefaultTintColor(@ColorInt int defaultTintColor)
88 | // Background color for error toast.
89 | .setErrorBackgroundColor(@ColorInt int errorBackgroundColor)
90 | // Background color for success toast.
91 | .setSuccessBackgroundColor(@ColorInt int successBackgroundColor)
92 | // Background color for warning toast.
93 | .setWarningBackgroundColor(@ColorInt int warningBackgroundColor)
94 | // Custom icon for error toast. Pass `null` to use default icon.
95 | .setErrorIcon(@Nullable Drawable errorIcon)
96 | // Custom icon for success toast. Pass `null` to use default icon.
97 | .setSuccessIcon(@Nullable Drawable successIcon)
98 | // Custom icon for warning toast. Pass `null` to use default icon.
99 | .setWarningIcon(@Nullable Drawable warningIcon)
100 | // Disable icon for all the toasts.
101 | .setDisableIcon(boolean disableIcon)
102 | // Custom icon size in `pixels` for all the toasts.
103 | .setIconSize(int iconSize)
104 | // Custom text size in `SP` for all the toasts.
105 | .setTextSize(int textSize)
106 | // Custom text typeface for all the toasts. Pass `null` to use system typeface.
107 | .setTextTypeface(@Nullable Typeface textTypeface)
108 | // Custom background drawable for all the toasts. Pass `null` to use default background.
109 | .setToastBackground(@Nullable Drawable toastBackground)
110 | // Apply customisations.
111 | .apply();
112 | ```
113 |
114 | Call `reset()` method to reset all the customisations.
115 |
116 | ```java
117 | // Reset customisations.
118 | DynamicToast.Config.getInstance().reset();
119 | ```
120 |
121 | ### Default toast
122 |
123 | Simple toast based on the vanilla Android theme for `Toast.LENGTH_SHORT` duration.
124 |
125 | ```java
126 | DynamicToast.make(context, "Default toast").show();
127 | ```
128 |
129 | ### Default toast with duration
130 |
131 | Simple toast based on the vanilla Android theme for supplied duration.
132 |
133 | ```java
134 | DynamicToast.make(context, "Default toast with duration", duration).show();
135 | ```
136 |
137 | ### Default toast with icon
138 |
139 | Simple toast based on the vanilla Android theme with a icon for `Toast.LENGTH_SHORT` duration.
140 |
141 | ```java
142 | DynamicToast.make(context, "Default toast with icon", drawable).show();
143 | ```
144 |
145 | ### Default toast with icon and duration
146 |
147 | Simple toast based on the vanilla Android theme with a icon for supplied duration.
148 |
149 | ```java
150 | DynamicToast.make(context, "Default toast with icon and duration", drawable, duration).show();
151 | ```
152 |
153 | ### Error toast
154 |
155 | Error toast with `#F44336` background for `Toast.LENGTH_SHORT` duration.
156 |
157 | ```java
158 | DynamicToast.makeError(context, "Error toast").show();
159 | ```
160 |
161 | ### Error toast with duration
162 |
163 | Error toast with `#F44336` background for supplied duration.
164 |
165 | ```java
166 | DynamicToast.makeError(context, "Error toast with duration", duration).show();
167 | ```
168 |
169 | ### Success toast
170 |
171 | Success toast with `#4CAF50` background for `Toast.LENGTH_SHORT` duration.
172 |
173 | ```java
174 | DynamicToast.makeSuccess(context, "Success toast").show();
175 | ```
176 |
177 | ### Success toast with duration
178 |
179 | Success toast with `#4CAF50` background for supplied duration.
180 |
181 | ```java
182 | DynamicToast.makeSuccess(context, "Success toast with duration", duration).show();
183 | ```
184 |
185 | ### Warning toast
186 |
187 | Warning toast with `#FFEB3B` background for `Toast.LENGTH_SHORT` duration.
188 |
189 | ```java
190 | DynamicToast.makeWarning(context, "Warning toast").show();
191 | ```
192 |
193 | ### Warning toast with duration
194 |
195 | Warning toast with `#FFEB3B` background for supplied duration.
196 |
197 | ```java
198 | DynamicToast.makeWarning(context, "Warning toast with duration", duration).show();
199 | ```
200 |
201 | ### Custom toast
202 |
203 | Custom toast based on the supplied background and tint color for `Toast.LENGTH_SHORT` duration.
204 |
205 | ```java
206 | DynamicToast.make(context, "Custom toast", tintColor, backgroundColor).show();
207 | ```
208 |
209 | ### Custom toast with duration
210 |
211 | Custom toast based on the supplied background and tint color for supplied duration.
212 |
213 | ```java
214 | DynamicToast.make(context, "Custom toast with duration", tintColor, backgroundColor, duration).show();
215 | ```
216 |
217 | ### Custom toast with icon
218 |
219 | Custom toast based on the supplied icon, background and tint color theme for `Toast.LENGTH_SHORT`
220 | duration.
221 |
222 | ```java
223 | DynamicToast.make(context, "Custom toast with icon", drawable, tintColor, backgroundColor).show();
224 | ```
225 |
226 | ### Custom toast with icon and duration
227 |
228 | Custom toast based on the supplied icon, background and tint color theme for supplied duration.
229 |
230 | ```java
231 | DynamicToast.make(context, "Custom toast with icon and duration", drawable,
232 | tintColor, backgroundColor, duration).show();
233 | ```
234 |
235 | ### Cheat sheets
236 |
237 | Use dynamic hint to display cheat sheets for any `view`. All the methods are same as explained
238 | above, just replace `DynamicToast` with `DynamicHint` to create a cheat sheet.
239 |
240 | > Use `DynamicHint.show(view, toast)` method to display it according to the anchor view position.
241 |
242 | ### Dependency
243 |
244 | It depends on the [dynamic-utils][dynamic-utils] to perform various internal operations.
245 | So, its functions can also be used to perform other useful operations.
246 |
247 | ---
248 |
249 | ## Author
250 |
251 | Pranav Pandey
252 |
253 | [](https://github.com/pranavpandey)
254 | [](https://twitter.com/intent/follow?screen_name=pranavpandeydev)
255 | [](https://paypal.me/pranavpandeydev)
256 |
257 | ---
258 |
259 | ## License
260 |
261 | Copyright 2017-2024 Pranav Pandey
262 |
263 | Licensed under the Apache License, Version 2.0 (the "License");
264 | you may not use this file except in compliance with the License.
265 | You may obtain a copy of the License at
266 |
267 | http://www.apache.org/licenses/LICENSE-2.0
268 |
269 | Unless required by applicable law or agreed to in writing, software
270 | distributed under the License is distributed on an "AS IS" BASIS,
271 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
272 | See the License for the specific language governing permissions and
273 | limitations under the License.
274 |
275 |
276 | [android-support]: https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0
277 | [androidx]: https://developer.android.com/jetpack/androidx
278 | [androidx core]: https://developer.android.com/jetpack/androidx/releases/core
279 | [androidx-migrate]: https://developer.android.com/jetpack/androidx/migrate
280 | [documentation]: https://pranavpandey.github.io/dynamic-toasts
281 | [dynamic-utils]: https://github.com/pranavpandey/dynamic-utils
282 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2025 Pranav Pandey
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | buildscript {
18 | ext.versions = [
19 | 'compileSdk' : 35,
20 | 'minSdk' : 21,
21 | 'targetSdk' : 35,
22 | 'buildTools' : '35.0.0',
23 | 'constraintlayout': '2.1.4',
24 | 'dynamic' : '4.6.1',
25 | 'dialogs' : '4.5.0',
26 | 'flexbox' : '3.0.0',
27 | 'kotlin' : '1.9.24'
28 | ]
29 |
30 | repositories {
31 | mavenCentral()
32 | google()
33 | }
34 |
35 | dependencies {
36 | classpath 'com.android.tools.build:gradle:8.7.3'
37 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
38 | }
39 | }
40 |
41 | plugins {
42 | id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
43 | }
44 |
45 | allprojects {
46 | repositories {
47 | mavenCentral()
48 | google()
49 | }
50 | }
51 |
52 | tasks.register('clean', Delete) {
53 | delete rootProject.layout.buildDirectory
54 | }
55 |
56 | ext {
57 | projectName = 'dynamic-toasts'
58 | projectDesc = 'A simple library to display themed toasts with icon and text on Android.'
59 | versionDesc = 'A simple library to display themed toasts with icon and text on Android 4.0 ' +
60 | '(API 14) and above.'
61 | referenceTitle = 'Dynamic Toasts API reference'
62 |
63 | siteUrl = 'https://github.com/pranavpandey/dynamic-toasts'
64 | gitUrl = 'https://github.com/pranavpandey/dynamic-toasts'
65 | issueUrl = 'https://github.com/pranavpandey/dynamic-toasts/issues'
66 | githubUrl = 'pranavpandey/dynamic-toasts'
67 |
68 | mavenRepo = 'android'
69 | mavenGroup = 'com.pranavpandey.android'
70 | mavenDir = 'com/pranavpandey/android'
71 | mavenArtifactId = 'dynamic-toasts'
72 | mavenInceptionYear = '2017'
73 | mavenVersion = '4.3.0'
74 | mavenVersionCode = 34
75 | sampleVersionCode = 35
76 |
77 | developerId = 'pranavpandey'
78 | developerName = 'Pranav Pandey'
79 | developerEmail = 'dynamic@pranavpandey.com'
80 |
81 | licenseName = 'The Apache Software License, Version 2.0'
82 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
83 | licenseDistribution = 'repo'
84 | allLicenses = ["Apache-2.0"]
85 |
86 | publication = 'local.properties'
87 |
88 | ext["signing.keyId"] = ''
89 | ext["signing.password"] = ''
90 | ext["signing.secretKeyRingFile"] = ''
91 |
92 | ossrhUsername = ''
93 | ossrhPassword = ''
94 | sonatypeStagingProfileId = ''
95 | }
96 |
97 | apply plugin: 'io.github.gradle-nexus.publish-plugin'
98 |
99 | File publish = project.rootProject.file("${publication}")
100 | if (publish.exists()) {
101 | Properties properties = new Properties()
102 | new FileInputStream(publish).withCloseable { is -> properties.load(is) }
103 | properties.each { name, value -> ext[name] = value }
104 | }
105 |
106 | nexusPublishing {
107 | repositories {
108 | sonatype {
109 | username = ossrhUsername
110 | password = ossrhPassword
111 | stagingProfileId = sonatypeStagingProfileId
112 | }
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/dynamic-toasts/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2024 Pranav Pandey
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.library'
18 |
19 | android {
20 | compileSdkVersion versions.compileSdk
21 | buildToolsVersion versions.buildTools
22 | namespace 'com.pranavpandey.android.dynamic.toasts'
23 |
24 | defaultConfig {
25 | minSdkVersion versions.minSdk
26 | targetSdkVersion versions.targetSdk
27 | }
28 |
29 | sourceSets {
30 | main.res.srcDirs 'res'
31 | }
32 |
33 | compileOptions {
34 | sourceCompatibility JavaVersion.VERSION_17
35 | targetCompatibility JavaVersion.VERSION_17
36 | }
37 | }
38 |
39 | dependencies {
40 | implementation(platform("org.jetbrains.kotlin:kotlin-bom:${versions.kotlin}"))
41 |
42 | api "com.pranavpandey.android:dynamic-utils:${versions.dynamic}"
43 | }
44 |
45 | if (project.rootProject.file("${publication}").exists()) {
46 | apply from: 'maven.gradle'
47 | }
48 |
49 | tasks.register('generateJavadoc') {
50 | description "Generates Javadoc."
51 | }
52 |
53 | project.afterEvaluate {
54 | android.libraryVariants.configureEach { variant ->
55 | def task = project.tasks.create(
56 | "generate${variant.name.capitalize()}Javadoc", Javadoc) {
57 | title "${referenceTitle}
If no color is supplied, it will display default toast based on the vanilla Android. 45 | */ 46 | public class DynamicToast { 47 | 48 | /** 49 | * Default background color for the toast. 50 | */ 51 | private static final @ColorInt int ADT_DEFAULT_BG_COLOR = 52 | Color.parseColor("#454545"); 53 | 54 | /** 55 | * Default tint color for the toast. 56 | */ 57 | private static final @ColorInt int ADT_DEFAULT_TINT_COLOR = 58 | Color.parseColor("#FFFFFF"); 59 | 60 | /** 61 | * Default background color for the error toast. 62 | */ 63 | private static final @ColorInt int ADT_DEFAULT_ERROR_BG_COLOR = 64 | Color.parseColor("#F44336"); 65 | 66 | /** 67 | * Default background color for the success toast. 68 | */ 69 | private static final @ColorInt int ADT_DEFAULT_SUCCESS_BG_COLOR = 70 | Color.parseColor("#4CAF50"); 71 | 72 | /** 73 | * Default background color for the warning toast. 74 | */ 75 | private static final @ColorInt int ADT_DEFAULT_WARNING_BG_COLOR = 76 | Color.parseColor("#FFEB3B"); 77 | 78 | /** 79 | * Default value for the {@link #disableIcon}. 80 | *
{@code false} to enable the toast icon. 81 | */ 82 | private static final boolean ADT_DEFAULT_DISABLE_ICON = false; 83 | 84 | /** 85 | * Default value for the {@link #tintIcon}. 86 | *
{@code true} to tint the toast icon. 87 | */ 88 | private static final boolean ADT_DEFAULT_TINT_ICON = true; 89 | 90 | /** 91 | * Default icon size for the toast in pixels. 92 | *
{@code -1} to use in-built icon size. 93 | */ 94 | private static final @ColorInt int ADT_DEFAULT_ICON_SIZE = -1; 95 | 96 | /** 97 | * Default text size for the toast in SP. 98 | *
{@code -1} to use system text size. 99 | * 100 | * @see TypedValue#COMPLEX_UNIT_SP; 101 | */ 102 | private static final @ColorInt int ADT_DEFAULT_TEXT_SIZE = -1; 103 | 104 | /** 105 | * Background color for the default toast. 106 | */ 107 | private static @Nullable @ColorInt Integer defaultBackgroundColor = ADT_DEFAULT_BG_COLOR; 108 | 109 | /** 110 | * Tint color for the default toast. 111 | */ 112 | private static @Nullable @ColorInt Integer defaultTintColor = ADT_DEFAULT_TINT_COLOR; 113 | 114 | /** 115 | * Background color for the error toast. 116 | */ 117 | private static @Nullable @ColorInt Integer errorBackgroundColor = ADT_DEFAULT_ERROR_BG_COLOR; 118 | 119 | /** 120 | * Background color for the success toast. 121 | */ 122 | private static @Nullable @ColorInt Integer successBackgroundColor = 123 | ADT_DEFAULT_SUCCESS_BG_COLOR; 124 | 125 | /** 126 | * Background color for the warning toast. 127 | */ 128 | private static @Nullable @ColorInt Integer warningBackgroundColor = 129 | ADT_DEFAULT_WARNING_BG_COLOR; 130 | 131 | /** 132 | * Custom icon for the error toast. 133 | *
{@code null} to use the default icon. 134 | */ 135 | private static Drawable errorIcon = null; 136 | 137 | /** 138 | * Custom icon for the success toast. 139 | *
{@code null} to use the default icon. 140 | */ 141 | private static Drawable successIcon = null; 142 | 143 | /** 144 | * Custom icon for the warning toast. 145 | *
{@code null} to use the default icon. 146 | */ 147 | private static Drawable warningIcon = null; 148 | 149 | /** 150 | * {@code true} to disable icon for all the toasts. 151 | */ 152 | private static boolean disableIcon = ADT_DEFAULT_DISABLE_ICON; 153 | 154 | /** 155 | * {@code true} to tint icon for all the toasts. 156 | */ 157 | private static boolean tintIcon = ADT_DEFAULT_TINT_ICON; 158 | 159 | /** 160 | * Icon size for the toast in pixels. 161 | */ 162 | private static int iconSize = ADT_DEFAULT_ICON_SIZE; 163 | 164 | /** 165 | * Text size for the toast in SP. 166 | * 167 | * @see TypedValue#COMPLEX_UNIT_SP; 168 | */ 169 | private static int textSize = ADT_DEFAULT_TEXT_SIZE; 170 | 171 | /** 172 | * Custom typeface used by the toast. 173 | *
{@code null} to use the system typeface. 174 | */ 175 | private static Typeface textTypeface = null; 176 | 177 | /** 178 | * Custom background used by the toast. 179 | *
{@code null} to use the default background. 180 | */ 181 | private static Drawable toastBackground = null; 182 | 183 | /** 184 | * Generate tint color according to the supplied color, otherwise return the default value. 185 | * 186 | * @param color The color to be used to generate the tint color. 187 | * @param defaultColor The default value for the tint color. 188 | * 189 | * @return The generated tint color according to the supplied color, otherwise return the 190 | * default value. 191 | */ 192 | private static @Nullable @ColorInt Integer generateTintColor( 193 | @Nullable @ColorInt Integer color, @Nullable @ColorInt Integer defaultColor) { 194 | if (color != null) { 195 | return DynamicColorUtils.getTintColor(color); 196 | } 197 | 198 | return defaultColor; 199 | } 200 | 201 | /** 202 | * Make a standard toast that just contains a text view. 203 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 204 | * 205 | * @param context The context to use. 206 | * @param text The text to show. Can be formatted text. 207 | * 208 | * @return The toast with the supplied parameters. 209 | *
Use {@link Toast#show()} to display the toast. 210 | */ 211 | public static @NonNull Toast make(@NonNull Context context, @Nullable CharSequence text) { 212 | return make(context, text, null, defaultTintColor, 213 | defaultBackgroundColor, Toast.LENGTH_SHORT); 214 | } 215 | 216 | /** 217 | * Make a standard toast that just contains a text view. 218 | * 219 | * @param context The context to use. 220 | * @param text The text to show. Can be formatted text. 221 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 222 | * or {@link Toast#LENGTH_LONG}. 223 | * 224 | * @return The toast with the supplied parameters. 225 | *
Use {@link Toast#show()} to display the toast. 226 | */ 227 | public static @NonNull Toast make(@NonNull Context context, 228 | @Nullable CharSequence text, int duration) { 229 | return make(context, text, null, defaultTintColor, 230 | defaultBackgroundColor, duration); 231 | } 232 | 233 | /** 234 | * Make a error toast with icon and the text. 235 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 236 | * 237 | * @param context The context to use. 238 | * @param text The text to show. Can be formatted text. 239 | * 240 | * @return The toast with the supplied parameters. 241 | *
Use {@link Toast#show()} to display the toast. 242 | */ 243 | public static @NonNull Toast makeError(@NonNull Context context, @Nullable CharSequence text) { 244 | return make(context, text, errorIcon != null ? errorIcon 245 | : ContextCompat.getDrawable(context, R.drawable.adt_ic_error), 246 | generateTintColor(errorBackgroundColor, defaultTintColor), errorBackgroundColor); 247 | } 248 | 249 | /** 250 | * Make a error toast with icon and the text. 251 | * 252 | * @param context The context to use. 253 | * @param text The text to show. Can be formatted text. 254 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 255 | * or {@link Toast#LENGTH_LONG}. 256 | * 257 | * @return The toast with the supplied parameters. 258 | *
Use {@link Toast#show()} to display the toast. 259 | */ 260 | public static @NonNull Toast makeError(@NonNull Context context, 261 | @Nullable CharSequence text, int duration) { 262 | return make(context, text, ContextCompat.getDrawable(context, R.drawable.adt_ic_error), 263 | generateTintColor(errorBackgroundColor, defaultTintColor), 264 | errorBackgroundColor, duration); 265 | } 266 | 267 | /** 268 | * Make a success toast with icon and the text. 269 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 270 | * 271 | * @param context The context to use. 272 | * @param text The text to show. Can be formatted text. 273 | * 274 | * @return The toast with the supplied parameters. 275 | *
Use {@link Toast#show()} to display the toast. 276 | */ 277 | public static @NonNull Toast makeSuccess(@NonNull Context context, 278 | @Nullable CharSequence text) { 279 | return make(context, text, successIcon != null ? successIcon 280 | : ContextCompat.getDrawable(context, R.drawable.adt_ic_success), 281 | generateTintColor(successBackgroundColor, defaultTintColor), successBackgroundColor); 282 | } 283 | 284 | /** 285 | * Make a success toast with icon and the text. 286 | * 287 | * @param context The context to use. 288 | * @param text The text to show. Can be formatted text. 289 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 290 | * or {@link Toast#LENGTH_LONG}. 291 | * 292 | * @return The toast with the supplied parameters. 293 | *
Use {@link Toast#show()} to display the toast. 294 | */ 295 | public static @NonNull Toast makeSuccess(@NonNull Context context, 296 | @Nullable CharSequence text, int duration) { 297 | return make(context, text, ContextCompat.getDrawable(context, R.drawable.adt_ic_success), 298 | generateTintColor(successBackgroundColor, defaultTintColor), 299 | successBackgroundColor, duration); 300 | } 301 | 302 | /** 303 | * Make a warning toast with icon and the text. 304 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 305 | * 306 | * @param context The context to use. 307 | * @param text The text to show. Can be formatted text. 308 | * 309 | * @return The toast with the supplied parameters. 310 | *
Use {@link Toast#show()} to display the toast. 311 | */ 312 | public static @NonNull Toast makeWarning(@NonNull Context context, 313 | @Nullable CharSequence text) { 314 | return make(context, text, warningIcon != null ? warningIcon 315 | : ContextCompat.getDrawable(context, R.drawable.adt_ic_warning), 316 | generateTintColor(warningBackgroundColor, defaultTintColor), warningBackgroundColor); 317 | } 318 | 319 | /** 320 | * Make a warning toast with icon and the text. 321 | * 322 | * @param context The context to use. 323 | * @param text The text to show. Can be formatted text. 324 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 325 | * or {@link Toast#LENGTH_LONG}. 326 | * 327 | * @return The toast with the supplied parameters. 328 | *
Use {@link Toast#show()} to display the toast. 329 | */ 330 | public static @NonNull Toast makeWarning(@NonNull Context context, 331 | @Nullable CharSequence text, int duration) { 332 | return make(context, text, ContextCompat.getDrawable(context, R.drawable.adt_ic_warning), 333 | generateTintColor(warningBackgroundColor, defaultTintColor), 334 | warningBackgroundColor, duration); 335 | } 336 | 337 | /** 338 | * Make a error toast with icon and the text. 339 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 340 | * 341 | * @param context The context to use. 342 | * @param text The text to show. Can be formatted text. 343 | * @param icon The toast icon to show. 344 | * 345 | * @return The toast with the supplied parameters. 346 | *
Use {@link Toast#show()} to display the toast. 347 | */ 348 | public static @NonNull Toast make(@NonNull Context context, 349 | @Nullable CharSequence text, @Nullable Drawable icon) { 350 | return make(context, text, icon, defaultTintColor, 351 | defaultBackgroundColor, Toast.LENGTH_SHORT); 352 | } 353 | 354 | /** 355 | * Make a themed toast with icon and the text. 356 | * 357 | * @param context The context to use. 358 | * @param text The text to show. Can be formatted text. 359 | * @param icon The toast icon to show. 360 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 361 | * or {@link Toast#LENGTH_LONG}. 362 | * 363 | * @return The toast with the supplied parameters. 364 | *
Use {@link Toast#show()} to display the toast. 365 | */ 366 | public static @NonNull Toast make(@NonNull Context context, @Nullable CharSequence text, 367 | @Nullable Drawable icon, int duration) { 368 | return make(context, text, icon, defaultTintColor, defaultBackgroundColor, duration); 369 | } 370 | 371 | /** 372 | * Make a themed toast with icon and the text. 373 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 374 | * 375 | * @param context The context to use. 376 | * @param text The text to show. Can be formatted text. 377 | * @param tintColor The toast tint color based on the toast background. 378 | *
It will automatically check for the contrast to provide the 379 | * best visibility. 380 | * @param backgroundColor The toast background color. 381 | * 382 | * @return The toast with the supplied parameters. 383 | *
Use {@link Toast#show()} to display the toast. 384 | */ 385 | public static @NonNull Toast make(@NonNull Context context, @Nullable CharSequence text, 386 | @Nullable @ColorInt Integer tintColor, @Nullable @ColorInt Integer backgroundColor) { 387 | return make(context, text, null, tintColor, backgroundColor, Toast.LENGTH_SHORT); 388 | } 389 | 390 | /** 391 | * Make a themed toast with text, background and the tint color. 392 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 393 | * 394 | * @param context The context to use. 395 | * @param text The text to show. Can be formatted text. 396 | * @param tintColor The toast tint color based on the toast background. 397 | *
It will automatically check for the contrast to provide the 398 | * best visibility. 399 | * @param backgroundColor The toast background color. 400 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 401 | * or {@link Toast#LENGTH_LONG}. 402 | * 403 | * @return The toast with the supplied parameters. 404 | *
Use {@link Toast#show()} to display the toast. 405 | */ 406 | public static @NonNull Toast make(@NonNull Context context, 407 | @Nullable CharSequence text, @Nullable @ColorInt Integer tintColor, 408 | @Nullable @ColorInt Integer backgroundColor, int duration) { 409 | return make(context, text, null, tintColor, backgroundColor, duration); 410 | } 411 | 412 | /** 413 | * Make a themed toast with text, icon, background and the tint color. 414 | *
The toast duration will be {@link Toast#LENGTH_SHORT}. 415 | * 416 | * @param context The context to use. 417 | * @param text The text to show. Can be formatted text. 418 | * @param icon The toast icon to show. 419 | * @param tintColor The toast tint color based on the toast background. 420 | *
It will automatically check for the contrast to provide the 421 | * best visibility. 422 | * @param backgroundColor The toast background color. 423 | * 424 | * @return The toast with the supplied parameters. 425 | *
Use {@link Toast#show()} to display the toast. 426 | */ 427 | public static @NonNull Toast make(@NonNull Context context, @Nullable CharSequence text, 428 | @Nullable Drawable icon, @Nullable @ColorInt Integer tintColor, 429 | @Nullable @ColorInt Integer backgroundColor) { 430 | return make(context, text, icon, tintColor, backgroundColor, Toast.LENGTH_SHORT); 431 | } 432 | 433 | /** 434 | * Make a themed toast with text, icon, background and the tint color. 435 | * 436 | * @param context The context to use. 437 | * @param text The text to show. Can be formatted text. 438 | * @param icon The toast icon to show. 439 | * @param tintColor The toast tint color based on the toast background. 440 | *
It will automatically check for the contrast to provide the 441 | * best visibility. 442 | * @param backgroundColor The toast background color. 443 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT} 444 | * or {@link Toast#LENGTH_LONG}. 445 | * 446 | * @return The toast with the supplied parameters. 447 | *
Use {@link Toast#show()} to display the toast. 448 | */ 449 | public static @NonNull Toast make(@NonNull Context context, @Nullable CharSequence text, 450 | @Nullable Drawable icon, @Nullable @ColorInt Integer tintColor, 451 | @Nullable @ColorInt Integer backgroundColor, int duration) { 452 | if (context instanceof Activity && ((Activity) context).isFinishing()) { 453 | context = context.getApplicationContext(); 454 | } 455 | 456 | @Nullable @ColorInt Integer toastTintColor = tintColor; 457 | if (tintColor != null && backgroundColor != null) { 458 | toastTintColor = DynamicColorUtils.getContrastColor(toastTintColor, backgroundColor); 459 | } 460 | 461 | ToastCompat toast = new ToastCompat(context, new Toast(context)); 462 | View toastLayout = LayoutInflater.from(context).inflate( 463 | R.layout.adt_layout_toast, new LinearLayout(context), false); 464 | ImageView toastIcon = toastLayout.findViewById(R.id.adt_toast_icon); 465 | TextView toastText = toastLayout.findViewById(R.id.adt_toast_text); 466 | 467 | if (!disableIcon && icon != null) { 468 | if (iconSize != ADT_DEFAULT_ICON_SIZE) { 469 | toastIcon.getLayoutParams().width = iconSize; 470 | toastIcon.getLayoutParams().height = iconSize; 471 | toastIcon.requestLayout(); 472 | } 473 | 474 | if (tintIcon && toastTintColor != null) { 475 | toastIcon.setColorFilter(toastTintColor); 476 | } else { 477 | toastIcon.clearColorFilter(); 478 | } 479 | toastIcon.setImageDrawable(icon); 480 | } else { 481 | toastIcon.setVisibility(View.GONE); 482 | } 483 | 484 | if (textTypeface != null) { 485 | toastText.setTypeface(textTypeface); 486 | } 487 | if (textSize != ADT_DEFAULT_TEXT_SIZE) { 488 | toastText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); 489 | } 490 | 491 | if (toastTintColor != null) { 492 | toastText.setTextColor(toastTintColor); 493 | } 494 | toastText.setText(text); 495 | 496 | if (toastBackground != null) { 497 | DynamicDrawableUtils.setBackground(toastLayout, backgroundColor != null 498 | ? DynamicDrawableUtils.colorizeDrawable(toastBackground, 499 | backgroundColor, PorterDuff.Mode.MULTIPLY) : toastBackground); 500 | } else { 501 | DynamicDrawableUtils.setBackground(toastLayout, backgroundColor != null 502 | ? DynamicDrawableUtils.colorizeDrawable(ContextCompat.getDrawable(context, 503 | R.drawable.adt_toast_background), backgroundColor, PorterDuff.Mode.MULTIPLY) 504 | : ContextCompat.getDrawable(context, R.drawable.adt_toast_background)); 505 | } 506 | 507 | toast.setDuration(duration); 508 | toast.setView(toastLayout); 509 | 510 | return toast; 511 | } 512 | 513 | /** 514 | * Configuration class to customise the {@link DynamicToast} attributes. 515 | */ 516 | public static class Config { 517 | 518 | /** 519 | * Singleton instance of {@link Config}. 520 | */ 521 | private static Config sInstance; 522 | 523 | /** 524 | * Background color for the default toast. 525 | */ 526 | private @ColorInt Integer defaultBackgroundColor = DynamicToast.defaultBackgroundColor; 527 | 528 | /** 529 | * Tint color for the default toast. 530 | */ 531 | private @ColorInt Integer defaultTintColor = DynamicToast.defaultTintColor; 532 | 533 | /** 534 | * Background color for the error toast. 535 | */ 536 | private @ColorInt Integer errorBackgroundColor = DynamicToast.errorBackgroundColor; 537 | 538 | /** 539 | * Background color for the success toast. 540 | */ 541 | private @ColorInt Integer successBackgroundColor = DynamicToast.successBackgroundColor; 542 | 543 | /** 544 | * Background color for the warning toast. 545 | */ 546 | private @ColorInt Integer warningBackgroundColor = DynamicToast.warningBackgroundColor; 547 | 548 | /** 549 | * Custom icon for the error toast. 550 | */ 551 | private Drawable errorIcon = DynamicToast.errorIcon; 552 | 553 | /** 554 | * Custom icon for the success toast. 555 | */ 556 | private Drawable successIcon = DynamicToast.successIcon; 557 | 558 | /** 559 | * Custom icon for the warning toast. 560 | */ 561 | private Drawable warningIcon = DynamicToast.warningIcon; 562 | 563 | /** 564 | * {@code true} to disable icon for all the toasts. 565 | */ 566 | private boolean disableIcon = DynamicToast.disableIcon; 567 | 568 | /** 569 | * {@code true} to tint icon for all the toasts. 570 | */ 571 | private boolean tintIcon = DynamicToast.tintIcon; 572 | 573 | /** 574 | * Icon size for the toast in pixels. 575 | */ 576 | private int iconSize = DynamicToast.iconSize; 577 | 578 | /** 579 | * Text size for the toast in SP. 580 | * 581 | * @see TypedValue#COMPLEX_UNIT_SP; 582 | */ 583 | private @ColorInt int textSize = DynamicToast.textSize; 584 | 585 | /** 586 | * Custom text typeface used by the toast. 587 | */ 588 | private Typeface textTypeface = null; 589 | 590 | /** 591 | * Custom background used by the toast. 592 | */ 593 | private Drawable toastBackground = null; 594 | 595 | /** 596 | * Making default constructor private to avoid instantiation. 597 | */ 598 | private Config() { } 599 | 600 | /** 601 | * Get instance to access public methods. Must be called before accessing methods. 602 | * 603 | * @return The singleton instance of this class. 604 | */ 605 | public static @NonNull Config getInstance() { 606 | if (sInstance == null) { 607 | sInstance = new Config(); 608 | } 609 | 610 | return sInstance; 611 | } 612 | 613 | /** 614 | * Set the default background color. 615 | * 616 | * @param defaultBackgroundColor The background color to be set. 617 | * 618 | * @return The {@link Config} object to allow for chaining of calls to set methods. 619 | */ 620 | public @NonNull Config setDefaultBackgroundColor( 621 | @Nullable @ColorInt Integer defaultBackgroundColor) { 622 | this.defaultBackgroundColor = defaultBackgroundColor; 623 | 624 | return this; 625 | } 626 | 627 | /** 628 | * Set the default tint color. 629 | * 630 | * @param defaultTintColor The tint color to be set. 631 | * 632 | * @return The {@link Config} object to allow for chaining of calls to set methods. 633 | */ 634 | public @NonNull Config setDefaultTintColor(@Nullable @ColorInt Integer defaultTintColor) { 635 | this.defaultTintColor = defaultTintColor; 636 | 637 | return this; 638 | } 639 | 640 | /** 641 | * Set the error background color. 642 | * 643 | * @param errorBackgroundColor The error background color to be set. 644 | * 645 | * @return The {@link Config} object to allow for chaining of calls to set methods. 646 | */ 647 | public @NonNull Config setErrorBackgroundColor( 648 | @Nullable @ColorInt Integer errorBackgroundColor) { 649 | this.errorBackgroundColor = errorBackgroundColor; 650 | 651 | return this; 652 | } 653 | 654 | /** 655 | * Set the success background color. 656 | * 657 | * @param successBackgroundColor The success background color 658 | * to be set. 659 | * 660 | * @return The {@link Config} object to allow for chaining of calls to set methods. 661 | */ 662 | public @NonNull Config setSuccessBackgroundColor( 663 | @Nullable @ColorInt Integer successBackgroundColor) { 664 | this.successBackgroundColor = successBackgroundColor; 665 | 666 | return this; 667 | } 668 | 669 | /** 670 | * Set the warning background color. 671 | * 672 | * @param warningBackgroundColor The warning background color to be set. 673 | * 674 | * @return The {@link Config} object to allow for chaining of calls to set methods. 675 | */ 676 | public @NonNull Config setWarningBackgroundColor( 677 | @Nullable @ColorInt Integer warningBackgroundColor) { 678 | this.warningBackgroundColor = warningBackgroundColor; 679 | 680 | return this; 681 | } 682 | 683 | /** 684 | * Set the error icon. 685 | *
Pass {@code null} to use the default icon. 686 | * 687 | * @param errorIcon The error icon to be set. 688 | * 689 | * @return The {@link Config} object to allow for chaining of calls to set methods. 690 | */ 691 | public @NonNull Config setErrorIcon(@Nullable Drawable errorIcon) { 692 | this.errorIcon = errorIcon; 693 | 694 | return this; 695 | } 696 | 697 | /** 698 | * Set the success icon. 699 | *
Pass {@code null} to use the default icon. 700 | * 701 | * @param successIcon The success icon to be set. 702 | * 703 | * @return The {@link Config} object to allow for chaining of calls to set methods. 704 | */ 705 | public @NonNull Config setSuccessIcon(@Nullable Drawable successIcon) { 706 | this.successIcon = successIcon; 707 | 708 | return this; 709 | } 710 | 711 | /** 712 | * Set the warning icon. 713 | *
Pass {@code null} to use the default icon. 714 | * 715 | * @param warningIcon The warning icon to be set. 716 | * 717 | * @return The {@link Config} object to allow for chaining of calls to set methods. 718 | */ 719 | public @NonNull Config setWarningIcon(@Nullable Drawable warningIcon) { 720 | this.warningIcon = warningIcon; 721 | 722 | return this; 723 | } 724 | 725 | /** 726 | * Set the icon visibility. 727 | * 728 | * @param disableIcon {@code true} to disable icon for all the toasts. 729 | * 730 | * @return The {@link Config} object to allow for chaining of calls to set methods. 731 | */ 732 | public @NonNull Config setDisableIcon(boolean disableIcon) { 733 | this.disableIcon = disableIcon; 734 | 735 | return this; 736 | } 737 | 738 | /** 739 | * Set whether to tint the icon. 740 | * 741 | * @param tintIcon {@code true} to tint icon for all the toasts. 742 | * 743 | * @return The {@link Config} object to allow for chaining of calls to set methods. 744 | */ 745 | public @NonNull Config setTintIcon(boolean tintIcon) { 746 | this.tintIcon = tintIcon; 747 | 748 | return this; 749 | } 750 | 751 | /** 752 | * Set the icon size. 753 | * 754 | * @param iconSize The icon size to be set in {@code pixels}. 755 | * 756 | * @return The {@link Config} object to allow for chaining of calls to set methods. 757 | */ 758 | public @NonNull Config setIconSize(int iconSize) { 759 | this.iconSize = iconSize; 760 | 761 | return this; 762 | } 763 | 764 | /** 765 | * Set the text size. 766 | * 767 | * @param textSize The text size to be set in {@code sp}. 768 | * 769 | * @return The {@link Config} object to allow for chaining of calls to set methods. 770 | */ 771 | public @NonNull Config setTextSize(int textSize) { 772 | this.textSize = textSize; 773 | 774 | return this; 775 | } 776 | 777 | /** 778 | * Set the text typeface. 779 | *
Pass {@code null} to use the default typeface. 780 | * 781 | * @param textTypeface The text typeface to be set. 782 | * 783 | * @return The {@link Config} object to allow for chaining of calls to set methods. 784 | */ 785 | public @NonNull Config setTextTypeface(@Nullable Typeface textTypeface) { 786 | this.textTypeface = textTypeface; 787 | 788 | return this; 789 | } 790 | 791 | /** 792 | * Set the toast background. 793 | *
Pass {@code null} to use the default background.
794 | *
795 | * @param toastBackground The toast background to be set.
796 | *
797 | * @return The {@link Config} object to allow for chaining of calls to set methods.
798 | */
799 | public @NonNull Config setToastBackground(@Nullable Drawable toastBackground) {
800 | this.toastBackground = toastBackground;
801 |
802 | return this;
803 | }
804 |
805 | /**
806 | * Apply customisations.
807 | */
808 | public void apply() {
809 | DynamicToast.defaultBackgroundColor = defaultBackgroundColor;
810 | DynamicToast.defaultTintColor = defaultTintColor;
811 | DynamicToast.errorBackgroundColor = errorBackgroundColor;
812 | DynamicToast.successBackgroundColor = successBackgroundColor;
813 | DynamicToast.warningBackgroundColor = warningBackgroundColor;
814 | DynamicToast.errorIcon = errorIcon;
815 | DynamicToast.successIcon = successIcon;
816 | DynamicToast.warningIcon = warningIcon;
817 | DynamicToast.disableIcon = disableIcon;
818 | DynamicToast.tintIcon = tintIcon;
819 | DynamicToast.iconSize = iconSize;
820 | DynamicToast.textSize = textSize;
821 | DynamicToast.textTypeface = textTypeface;
822 | DynamicToast.toastBackground = toastBackground;
823 |
824 | sInstance = null;
825 | }
826 |
827 | /**
828 | * Reset customisations.
829 | */
830 | public void reset() {
831 | DynamicToast.defaultBackgroundColor = ADT_DEFAULT_BG_COLOR;
832 | DynamicToast.defaultTintColor = ADT_DEFAULT_TINT_COLOR;
833 | DynamicToast.errorBackgroundColor = ADT_DEFAULT_ERROR_BG_COLOR;
834 | DynamicToast.successBackgroundColor = ADT_DEFAULT_SUCCESS_BG_COLOR;
835 | DynamicToast.warningBackgroundColor = ADT_DEFAULT_WARNING_BG_COLOR;
836 | DynamicToast.errorIcon = null;
837 | DynamicToast.successIcon = null;
838 | DynamicToast.warningIcon = null;
839 | DynamicToast.disableIcon = ADT_DEFAULT_DISABLE_ICON;
840 | DynamicToast.tintIcon = ADT_DEFAULT_TINT_ICON;
841 | DynamicToast.iconSize = ADT_DEFAULT_ICON_SIZE;
842 | DynamicToast.textSize = ADT_DEFAULT_TEXT_SIZE;
843 | DynamicToast.textTypeface = null;
844 | DynamicToast.toastBackground = null;
845 |
846 | sInstance = null;
847 | }
848 | }
849 | }
850 |
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/java/com/pranavpandey/android/dynamic/toasts/internal/ToastCompat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2022 Pranav Pandey
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.pranavpandey.android.dynamic.toasts.internal;
18 |
19 | import android.annotation.SuppressLint;
20 | import android.content.Context;
21 | import android.content.res.Resources;
22 | import android.view.View;
23 | import android.widget.Toast;
24 |
25 | import androidx.annotation.NonNull;
26 | import androidx.annotation.Nullable;
27 | import androidx.annotation.StringRes;
28 |
29 | import com.pranavpandey.android.dynamic.util.DynamicSdkUtils;
30 |
31 | import java.lang.reflect.Field;
32 |
33 | /**
34 | * A Toast to fix the bad token exception on API 25.
35 | */
36 | @SuppressWarnings("deprecation")
37 | public final class ToastCompat extends Toast {
38 |
39 | /**
40 | * Base toast used by this toast compat.
41 | */
42 | private final @NonNull Toast mToast;
43 |
44 | public ToastCompat(Context context, @NonNull Toast base) {
45 | super(context);
46 |
47 | this.mToast = base;
48 | }
49 |
50 | /**
51 | * Make a standard toast that just contains a text view.
52 | *
53 | * @param context The context to use.
54 | * @param text The text to show. Can be formatted text.
55 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT}
56 | * or {@link Toast#LENGTH_LONG}.
57 | *
58 | * @return The standard toast that just contains a text view.
59 | */
60 | @SuppressLint("ShowToast")
61 | public static ToastCompat makeText(@NonNull Context context,
62 | @Nullable CharSequence text, int duration) {
63 | Toast toast = Toast.makeText(context, text, duration);
64 | setToastContext(toast.getView(), new ToastContext(context, toast));
65 | return new ToastCompat(context, toast);
66 | }
67 |
68 | /**
69 | * Make a standard toast that just contains a text view.
70 | *
71 | * @param context The context to use.
72 | * @param resId The resource id of the string resource to use. Can be formatted text.
73 | * @param duration The duration for the toast, either {@link Toast#LENGTH_SHORT}
74 | * or {@link Toast#LENGTH_LONG}.
75 | *
76 | * @return The standard toast that just contains a text view.
77 | */
78 | public static Toast makeText(@NonNull Context context, @StringRes int resId, int duration)
79 | throws Resources.NotFoundException {
80 | return makeText(context, context.getResources().getText(resId), duration);
81 | }
82 |
83 | /**
84 | * Sets the toast context to fix bad token exception.
85 | *
86 | * @param view The view used by the toast
87 | * @param context The context used by the toast.
88 | */
89 | @SuppressLint("DiscouragedPrivateApi")
90 | private static void setToastContext(@Nullable View view, @NonNull Context context) {
91 | if (view != null && DynamicSdkUtils.is25()) {
92 | try {
93 | Field field = View.class.getDeclaredField("mContext");
94 | field.setAccessible(true);
95 | field.set(view, context);
96 | } catch (Throwable throwable) {
97 | throwable.printStackTrace();
98 | }
99 | }
100 | }
101 |
102 | @Override
103 | public void show() {
104 | mToast.show();
105 | }
106 |
107 | @Override
108 | public void setDuration(int duration) {
109 | mToast.setDuration(duration);
110 | }
111 |
112 | @Override
113 | public void setGravity(int gravity, int xOffset, int yOffset) {
114 | mToast.setGravity(gravity, xOffset, yOffset);
115 | }
116 |
117 | @Override
118 | public void setMargin(float horizontalMargin, float verticalMargin) {
119 | mToast.setMargin(horizontalMargin, verticalMargin);
120 | }
121 |
122 | @Override
123 | public void setText(int resId) {
124 | mToast.setText(resId);
125 | }
126 |
127 | @Override
128 | public void setText(CharSequence s) {
129 | mToast.setText(s);
130 | }
131 |
132 | @Override
133 | public void setView(View view) {
134 | mToast.setView(view);
135 | setToastContext(view, new ToastContext(view.getContext(), this));
136 | }
137 |
138 | @Override
139 | public float getHorizontalMargin() {
140 | return mToast.getHorizontalMargin();
141 | }
142 |
143 | @Override
144 | public float getVerticalMargin() {
145 | return mToast.getVerticalMargin();
146 | }
147 |
148 | @Override
149 | public int getDuration() {
150 | return mToast.getDuration();
151 | }
152 |
153 | @Override
154 | public int getGravity() {
155 | return mToast.getGravity();
156 | }
157 |
158 | @Override
159 | public int getXOffset() {
160 | return mToast.getXOffset();
161 | }
162 |
163 | @Override
164 | public int getYOffset() {
165 | return mToast.getYOffset();
166 | }
167 |
168 | @Override
169 | public @Nullable View getView() {
170 | return mToast.getView();
171 | }
172 |
173 | public @NonNull Toast getToast() {
174 | return mToast;
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/java/com/pranavpandey/android/dynamic/toasts/internal/ToastContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2022 Pranav Pandey
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.pranavpandey.android.dynamic.toasts.internal;
18 |
19 | import android.content.Context;
20 | import android.content.ContextWrapper;
21 | import android.view.Display;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.view.WindowManager;
25 | import android.widget.Toast;
26 |
27 | import androidx.annotation.NonNull;
28 | import androidx.annotation.Nullable;
29 |
30 | /**
31 | * A ContextWrapper to fix bad token exception.
32 | */
33 | public final class ToastContext extends ContextWrapper {
34 |
35 | /**
36 | * Boast used by this context wrapper.
37 | */
38 | private @NonNull Toast toast;
39 |
40 | /**
41 | * Constructor to initialize an object of this class.
42 | *
43 | * @param base The base context for this wrapper.
44 | * @param toast The toast for this wrapper.
45 | */
46 | public ToastContext(@NonNull Context base, @NonNull Toast toast) {
47 | super(base);
48 |
49 | this.toast = toast;
50 | }
51 |
52 | @Override
53 | public Context getApplicationContext() {
54 | return new ApplicationContextWrapper(getBaseContext().getApplicationContext());
55 | }
56 |
57 | /**
58 | * A ContextWrapper to initialize window manager service.
59 | */
60 | static final class ApplicationContextWrapper extends ContextWrapper {
61 |
62 | /**
63 | * Constructor to initialize an object of this class.
64 | *
65 | * @param base The base context for this wrapper.
66 | */
67 | private ApplicationContextWrapper(@NonNull Context base) {
68 | super(base);
69 | }
70 |
71 | @Override
72 | public Object getSystemService(@NonNull String name) {
73 | @Nullable Object service = null;
74 | if (Context.WINDOW_SERVICE.equals(name)) {
75 | service = getBaseContext().getSystemService(name);
76 | }
77 |
78 | if (service != null) {
79 | return new WindowManagerWrapper((WindowManager) service);
80 | }
81 |
82 | return super.getSystemService(name);
83 | }
84 | }
85 |
86 | /**
87 | * A WindowManager to fix the bad token exception.
88 | */
89 | @SuppressWarnings("deprecation")
90 | static final class WindowManagerWrapper implements WindowManager {
91 |
92 | /**
93 | * The base window manager used by this wrapper.
94 | */
95 | private final @NonNull WindowManager base;
96 |
97 | /**
98 | * Constructor to initialize an object of this class.
99 | *
100 | * @param base The base window manager for this wrapper.
101 | */
102 | private WindowManagerWrapper(@NonNull WindowManager base) {
103 | this.base = base;
104 | }
105 |
106 | @Override
107 | public Display getDefaultDisplay() {
108 | return base.getDefaultDisplay();
109 | }
110 |
111 | @Override
112 | public void removeViewImmediate(View view) {
113 | base.removeViewImmediate(view);
114 | }
115 |
116 | @Override
117 | public void addView(View view, ViewGroup.LayoutParams params) {
118 | try {
119 | base.addView(view, params);
120 | } catch (BadTokenException e) {
121 | e.printStackTrace();
122 | } catch (Throwable throwable) {
123 | throwable.printStackTrace();
124 | }
125 | }
126 |
127 | @Override
128 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
129 | base.updateViewLayout(view, params);
130 | }
131 |
132 | @Override
133 | public void removeView(View view) {
134 | base.removeView(view);
135 | }
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_error.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_success.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-hdpi/adt_ic_warning.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_error.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_success.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-mdpi/adt_ic_warning.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_error.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_success.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xhdpi/adt_ic_warning.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_error.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_success.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxhdpi/adt_ic_warning.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_error.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_success.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_warning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pranavpandey/dynamic-toasts/06483b06973e9ca627637c5b30011d5ed53e98d5/dynamic-toasts/src/main/res/drawable-xxxhdpi/adt_ic_warning.png
--------------------------------------------------------------------------------
/dynamic-toasts/src/main/res/drawable/adt_hint_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
29 | A simple library to display themed toasts with icon and text on Android.
30 | © Pranav Pandey
31 | Website
32 | GitHub
33 | LinkedIn
34 | Please download my other apps via Google Play to support the development.
35 | ]]>