Comments (24)
That seems like it sacrifices type safety for no benefit whatsoever. Why wouldn't you use an overload?
from android-ktx.
from android-ktx.
This may be handy, but apps should really stop using Toast
since they don't show up if notifications are disabled for an app.
https://issuetracker.google.com/issues/36951147
from android-ktx.
And instead of two methods, why not one with a duration
parameter?
from android-ktx.
i think good solution for duration is default fun argument, like this:
fun Context.toast(messageResId: Int, duration: Int = Toast.LENGTH_LONG) =
Toast.makeText(this, messageId, duration).show()
from android-ktx.
We could use also
to be able to show and return the Toast
.
fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG) =
Toast.makeText(this, resId, duration).also { it.show() }
This gives more flexibility to the API users. 😃
from android-ktx.
a single function with a duration arg & a default value is a nice idea 👍 whether it's added here or not, I'll have to incorporate that 😄
from android-ktx.
I always assumed it was better to create a Toast
using the ApplicationContext
(see: https://stackoverflow.com/questions/46730738/toast-maketext-activity-or-application-context).
So I would suggest doing something like this:
fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast =
Toast.makeText(this.applicationContext, text, duration).apply { show() }
This would also answer the proposal of @mitrejcevski
from android-ktx.
I do believe that .apply { show() }
should be part of this extension. Even lint thinks that toast should be created to be shown :)
That's how i use it in projects:
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast =
Toast.makeText(this, message, duration).apply { show() }
toasts are great for visual debugging, not for end user. So i prefer LENGTH_SHORT
as default value. What do you think?
from android-ktx.
@MGaetan89 returning an instance of the Toast
will give an opportunity for the caller to hide the Toast
before time is up by calling toast.cancel()
from android-ktx.
@MGaetan89 to return toast
object as the result. show()
returns Unit, unlike Context.toast()
will return created toast instance.
from android-ktx.
Egor, I'm curious, what is the recommended way? Snackbar?
from android-ktx.
I normally do something similar to what @abbath0767 suggested.
fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_LONG) =
Toast.makeText(this, resId, duration).show()
from android-ktx.
avoids the need to remember to call
show()
Lint guards against this.
from android-ktx.
What about this?
fun Context.toast(content: Any, length: Int = Toast.LENGTH_SHORT): Toast {
val message = when (content) {
is String -> content
is Int -> getString(content)
else -> throw IllegalArgumentException("Content must be a String or a String resource id.")
}
return Toast.makeText(this, message, length).also { it.show() }
}
Here we can pass a String
|String resource id
to the method, also we return the Toast
object back to the caller. 😃
from android-ktx.
You right, type safety for the win.
from android-ktx.
IMO the toast should not be an extension of Context
, but an Activity
. The reason is that the Context
might not be an Activity context, and some devices (i.e. Samsung, Xaomi) would complain. I've seen that happening recently.
from android-ktx.
What's the advantage(s) of using also
/apply
instead of directly show
?
from android-ktx.
@avaytsekhovskiy oh right, I didn't noticed that it became the method's body. Thanks
from android-ktx.
@simonschiller maybe like this:
fun Context.toast(text: CharSequence, long: Boolean = false): Toast =
Toast.makeText(this.applicationContext, text, if (long) Toast.LENGTH_LONG else Toast.LENGTH_SHORT).apply { show() }
from android-ktx.
@illuzor I would argue that a duration parameter is better than a boolean flag, as toast("Lorem Ipsum", Toast.LENGTH_LONG)
is more readable than toast("Lorem Ipsum", true)
from android-ktx.
Was merged.
from android-ktx.
Hi, is toast extension removed, can't import it using version 1.0.0-rc02
?
from android-ktx.
Yes
Why?
from android-ktx.
Related Issues (20)
- Convenience DSL for Media Controller Callback event handling
- androidx.preference.PreferenceFragment not derived from androidx.fragment.app.Fragment HOT 2
- Manifest merger failed HOT 5
- Release of 1.0.0-alpha3 out of sync with this repo HOT 4
- Extension property of bitmap from ImageView HOT 1
- error: resource android:attr/fontVariationSettings not found. HOT 1
- ImageDecoder source and test issues HOT 7
- inline extension properties can't be used in SAM lambda functions due to compiler bug HOT 1
- Add popular libraries extensions in new module HOT 1
- AAPT2 Error HOT 1
- View.post with with this view as a parameter HOT 2
- Resources.Theme.resolveAttribute extension
- Improve View.doOnPreDraw(...) HOT 1
- Features suggestions: Live data transformation HOT 1
- Offer: AsyncTask replacement
- Can't use getString(String) : String HOT 2
- androidx.core.os.BundleKt.bundleOf: Imports for API 21 classes break on older devices HOT 2
- gradle error HOT 3
- Add support for manipulating a color as HSV HOT 1
- Hi everyone!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-ktx.