Image in Toast?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Image in Toast?

Postby atrus123 » Wed Oct 15, 2008 3:51 pm

Is it possible to put an image into a Toast?

If not, does anyone know of any way to dismiss a Dialog after a couple of seconds? (Basically tell a dialog to act like a Toast).
atrus123
Developer
Developer
 
Posts: 26
Joined: Thu Oct 09, 2008 2:16 am
Location: Annapolis, MD

Top

Postby MrSnowflake » Wed Oct 15, 2008 5:46 pm

Toast does have a setView, so I suppose you could add a layout to it.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby atrus123 » Wed Oct 15, 2008 8:15 pm

I have been messing with that but it keeps throwing exceptions. I'll see if I can make it work, and if so, post my solution here.
atrus123
Developer
Developer
 
Posts: 26
Joined: Thu Oct 09, 2008 2:16 am
Location: Annapolis, MD

Postby MrSnowflake » Wed Oct 15, 2008 8:21 pm

Very simple Toast Image example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast toast = new Toast(this);
  2.  
  3. ImageView view = new ImageView(this);
  4.  
  5. view.setImageResource(R.drawable.icon);
  6.  
  7. toast.setView(view);
  8.  
  9. toast.show();
  10.  
  11.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


[edit]Another one:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Toast toast = Toast.makeText(this, "lalalal", Toast.LENGTH_LONG);
  3.  
  4. View textView = toast.getView();
  5.  
  6. LinearLayout lay = new LinearLayout(this);
  7.  
  8. lay.setOrientation(LinearLayout.HORIZONTAL);
  9.  
  10. ImageView view = new ImageView(this);
  11.  
  12. view.setImageResource(R.drawable.icon);
  13.  
  14. lay.addView(view);
  15.  
  16. lay.addView(textView);
  17.  
  18. toast.setView(lay);
  19.  
  20. toast.show();
  21.  
  22.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

The image is not on the background the text is on, but if you want to achieve that, you need to extend Toast.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby atrus123 » Thu Oct 16, 2008 12:07 am

Excellent. Thank you for these snippets. I'll try them out.

MrSnowflake wrote:Very simple Toast Image example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast toast = new Toast(this);
  2. ImageView view = new ImageView(this);
  3. view.setImageResource(R.drawable.icon);
  4. toast.setView(view);
  5. toast.show();
  6.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


[edit]Another one:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast toast = Toast.makeText(this, "lalalal", Toast.LENGTH_LONG);
  2. View textView = toast.getView();
  3. LinearLayout lay = new LinearLayout(this);
  4. lay.setOrientation(LinearLayout.HORIZONTAL);
  5. ImageView view = new ImageView(this);
  6. view.setImageResource(R.drawable.icon);
  7. lay.addView(view);
  8. lay.addView(textView);
  9. toast.setView(lay);
  10. toast.show();
  11.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

The image is not on the background the text is on, but if you want to achieve that, you need to extend Toast.
atrus123
Developer
Developer
 
Posts: 26
Joined: Thu Oct 09, 2008 2:16 am
Location: Annapolis, MD

Postby plusminus » Thu Oct 16, 2008 4:34 am

:arrow: I think this is worth a real snippet entry :idea:
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby ninor » Thu Oct 16, 2008 5:46 am

MrSnowFlake, I created the snippet in its section, and credited properly. Post
Thanks!
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby MrSnowflake » Thu Oct 16, 2008 9:04 am

If you try the code, you'll see the image and text aren't on the same background, which is a real drawback, I'd say.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby atrus123 » Thu Oct 16, 2008 6:31 pm

They served my purposes brilliantly. Thanks again.
atrus123
Developer
Developer
 
Posts: 26
Joined: Thu Oct 09, 2008 2:16 am
Location: Annapolis, MD

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests