Good day!

I created Toast and pasted a picture there (via LinearLayout). But for some reason, after adding a picture, Toast stretched vertically to the maximum. Ugly. :( I did not find the parameter setting the maximum height. How to be?

Thanks for the answer.

Toast creation code:

Toast toast = Toast.makeText(getApplicationContext(), R.string.short_toast_message, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout ShortToastContainer = (LinearLayout) toast.getView(); ImageView ShortToastImageView = new ImageView(getApplicationContext()); ShortToastImageView.setImageResource(R.drawable.message); ShortToastContainer.addView(ShortToastImageView, 0); toast.show(); 

    2 answers 2

    As far as I know, this can not be done. And the reasons seem to be obvious - it will be possible to show, for example, advertisements on top of all applications and whatnot with a toast.

    But there are libraries that allow you to show the similarity of toast, fully customizable.

    In general, the toast is categorically not intended to push pictures into it (maximum, perhaps, an icon?). Write why you want to do this, and I am sure you will be offered a better solution ...

    • The idea was to show a message once a day with the window open. My level slightly exceeds "Hello, world!". Therefore, looking for lessons for my ideas and doing them. The picture was initially not needed, but did the developer.alexanderklimov.ru/android/toast.php lesson and the girl nature got the better of it and wanted to be "prettier". And the author of the lesson Toast does not spread across the screen. - Alemarika
    • if you need to show something while your application is on the screen, it is better to use, for example, PopupWindow - Vladyslav Matviienko
    • No, the point is that it is necessary to show a message regardless of whether it is on the screen or not there ... - Alemarika
    • @Alemarika, I think that, to put it mildly, users will be resentful that your toasts are closing content on the screen. This is bad. - Vladyslav Matviienko
    • Add a message in the notification area - anber

    Well ... Try to manually set the height:

     LinearLayout ShortToastContainer = (LinearLayout) toast.getView(); LinearLayout.LayoutParams params=(LinearLayout.LayoutParams) ShortToastContainer.getLayoutParams(); params.height=LinearLayout.LayoutParams.WRAP_CONTENT;//или число в пикселях. 100, например ShortToastContainer.setLayoutParams(params); 

    And you don't need to write variables with a capital letter. They were taken with a small one, and the name of the class - with a large one.

    • Thank. While it does not work, it crashes with the words: "Unfortunately, your application is stopped," I will dig in the logs in the evening. Regarding the design you are undoubtedly right. Maybe you will advise a resource where there is a brief about the correct design? I would be very grateful. - Alemarika