Coder Social home page Coder Social logo

Comments (24)

JakeWharton avatar JakeWharton commented on July 24, 2024 8

That seems like it sacrifices type safety for no benefit whatsoever. Why wouldn't you use an overload?

from android-ktx.

JakeWharton avatar JakeWharton commented on July 24, 2024 6

from android-ktx.

egor-n avatar egor-n commented on July 24, 2024 5

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.

MGaetan89 avatar MGaetan89 commented on July 24, 2024 5

And instead of two methods, why not one with a duration parameter?

from android-ktx.

abbath0767 avatar abbath0767 commented on July 24, 2024 5

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.

frederickcxa avatar frederickcxa commented on July 24, 2024 5

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.

n8ebel avatar n8ebel commented on July 24, 2024 3

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.

simonschiller avatar simonschiller commented on July 24, 2024 3

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.

avaytsekhovskiy avatar avaytsekhovskiy commented on July 24, 2024 2

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.

jshvarts avatar jshvarts commented on July 24, 2024 2

@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.

avaytsekhovskiy avatar avaytsekhovskiy commented on July 24, 2024 1

@MGaetan89 to return toast object as the result. show() returns Unit, unlike Context.toast() will return created toast instance.

from android-ktx.

bernaferrari avatar bernaferrari commented on July 24, 2024

Egor, I'm curious, what is the recommended way? Snackbar?

from android-ktx.

jshvarts avatar jshvarts commented on July 24, 2024

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.

JakeWharton avatar JakeWharton commented on July 24, 2024

avoids the need to remember to call show()

Lint guards against this.

from android-ktx.

frederickcxa avatar frederickcxa commented on July 24, 2024

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.

frederickcxa avatar frederickcxa commented on July 24, 2024

You right, type safety for the win.

from android-ktx.

mitrejcevski avatar mitrejcevski commented on July 24, 2024

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.

MGaetan89 avatar MGaetan89 commented on July 24, 2024

What's the advantage(s) of using also/apply instead of directly show?

from android-ktx.

MGaetan89 avatar MGaetan89 commented on July 24, 2024

@avaytsekhovskiy oh right, I didn't noticed that it became the method's body. Thanks

from android-ktx.

illuzor avatar illuzor commented on July 24, 2024

@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.

simonschiller avatar simonschiller commented on July 24, 2024

@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.

JakeWharton avatar JakeWharton commented on July 24, 2024

Was merged.

from android-ktx.

jemshit avatar jemshit commented on July 24, 2024

Hi, is toast extension removed, can't import it using version 1.0.0-rc02?

from android-ktx.

hacker1024 avatar hacker1024 commented on July 24, 2024

Yes

Why?

from android-ktx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.