Search code examples
kotlinandroid-jetpack-composeandroid-toast

How to change toast message's default icon in jetpack compose?


I have a toast message in my code and it shows when user enter empty dictionary name but there is a problem for me toast shows message with a default android icon and I don't want this. How can I change this icon or remove in jetpack compose kotlin ?

here is screen shot. enter image description here

Hear is my code

 fun updateDictionary(context: Context, dictionaryName: String): Boolean {

        if (dictionaryName.isEmpty()) {
            Toast.makeText(context, "Please Enter The Dictionary Name !", Toast.LENGTH_LONG).show()

            return false
        }

        val dictionary = OwnDicEntity(dictionaryName, dicCreationTime ?: "", dicId)
        updateDicUseCase.updateDic(dictionary)

        return true

    }

if I want to update dictionary , I go to the update screen and this function is triggered and I have to write update dictionary name if I enter empty dictionary name. it shows toast message as I wrote above.

I'm returning a boolean value, true and false, so that it doesn't update when I enter a empty value.


Solution

  • Starting with Android 12 you don't get any control over how Toasts look - you get two lines of text and your application icon, so the user knows where the Toast came from. (Android 11 still allows for a custom view, but only for apps in the foreground.)

    If you want to change the icon, you need to add a new one for your app - the one you're seeing is the default ic_launcher drawable for a new project. Some info about that here.

    If you want to remove the icon, you can't! Since you seem to be displaying this as a popup in your own app, you might want to consider a Snackbar instead, or setting the error hint on a text field, or maybe create an area of your UI dedicated to displaying any errors (e.g. if there are multiple components you need to validate, and you want the error state in one place).