Using Toast Widgets in Android

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Using Toast Widgets in Android

Postby vikrant » Fri Jun 12, 2009 7:23 am

Hi I tried with Toast in Android..Here is a small tutorial :)
Package:- android.widget
Documentation:- http://developer.android.com/reference/ ... Toast.html
A Toast is a view containig a quick little message for the user. The Toast class helps you to create and show these messages.
Example:- to display messages like -"your settings have been saved"
Code Snippets:-

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.vikrant.ToastExample;
  2.  
  3. import android.app.Activity;
  4. import android.widget.Button;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Toast;
  8.  
  9. /**
  10.  * When you push the button on this Activity, it creates a Toast object and using the Toast method.
  11.  *Toast#makeText(android.content.Context,java.lang.CharSequence,int)
  12.  *Toast#LENGTH_SHORT
  13.  *Toast#LENGTH_LONG
  14.  */
  15. public class ToastExample extends Activity
  16. {
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState)
  19.     {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.main);
  22.         Button button;
  23.  
  24.         // short notification
  25.         button = (Button) findViewById(R.id.button_short);
  26.         button.setOnClickListener(new Button.OnClickListener()
  27.         {
  28.             public void onClick(View v)
  29.             {
  30.                 // Note that we create the Toast object and call the show() method
  31.                 // on it all on one line.  Most uses look like this, but there
  32.                 // are other methods on Toast that you can call to configure how
  33.                 // it appears.
  34.                 //
  35.                
  36.                 Toast t = new Toast(ToastExample.this);
  37.                 t.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
  38.                 t.show();
  39.             }
  40.         });
  41.  
  42.         // long notification
  43.         // The only difference here is that the notification stays up longer.
  44.         // You might want to use this if there is more text that they're going to read.
  45.         button = (Button) findViewById(R.id.button_long);
  46.         button.setOnClickListener(new Button.OnClickListener()
  47.         {
  48.             public void onClick(View v)
  49.             {
  50.                 Toast.makeText(ToastExample.this," Your settings have been saved",Toast.LENGTH_LONG).show();
  51.             }
  52.         });
  53.     }
  54. }
  55.  
  56.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent" >
  6. <TextView  android:layout_width="fill_parent"
  7.    android:layout_height="wrap_content"
  8.    android:text="@string/hello"  />
  9. <Button  android:id="@+id/button_short"
  10.  android:layout_width="wrap_content"
  11.  android:layout_height="wrap_content"
  12.  android:text="Volume Set"
  13.  android:layout_x="50px"
  14.  android:layout_y="200px"  />
  15.  <Button  android:id="@+id/button_long"
  16.  android:text="Save Settings"
  17.  android:layout_width="wrap_content"
  18.  android:layout_height="wrap_content"
  19.  android:layout_x="150px"
  20.  android:layout_y="200px" />  
  21. </AbsoluteLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Useful Methods:-
void cancel()
int getDuration()
int getGravity();
static Toast makeText(Context c, CharSequnce Text_to_be_shown,int duration)
void show();
void setDuration();
Attachments
Toast2.JPG
Toast2.JPG (18.83 KiB) Viewed 28485 times
Toast1.JPG
Screen Shots
Toast1.JPG (17.3 KiB) Viewed 28485 times
User avatar
vikrant
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 14, 2008 7:26 am
Location: CA,USA

Top

Postby Wiebbe » Thu Jun 18, 2009 10:54 am

Thanks for the code, but something didnt work for me.

The method Maketext is a static method, and it returns a new Toast object.

So when i did it like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Toast t;
  3.  
  4. t = Toast.makeText(onCreate.this, "Volume set to Max", Toast.LENGTH_SHORT);
  5.  
  6. t.show();
  7.  
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it worked :)
Wiebbe
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jun 18, 2009 10:51 am

Thanks

Postby vikrant » Thu Jun 18, 2009 2:19 pm

Hi
Thanks for correcting the code...
Instead of :-

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  Toast t = new Toast(ToastExample.this);
  4.  
  5.  t.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
  6.  
  7.  t.show();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


It should have been:-
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  Toast t
  3.  
  4.  t.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
  5.  
  6.  t.show();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


wanted to modify it, but it slipped :o ...

thnx again :)
User avatar
vikrant
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 14, 2008 7:26 am
Location: CA,USA

Postby WarrenFaith » Fri Jun 19, 2009 8:26 am

Thank you for your tutorial!

One last change and its perfect :)

Please check your warnings for your makeText() method.
You should access a static method always in a static way:
Change
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast t
  2.  
  3.  t.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
  4.  
  5.  t.show();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast t = Toast.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
  2.  
  3.  t.show();
  4.  
  5. // or simplier:
  6.  
  7. Toast.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT).show();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Re: Using Toast Widgets in Android

Postby endhck » Fri Jul 16, 2010 12:42 pm

i tried to use Toast but there is no error in code, however emulator did not worked it.
this is my code for toast:
Code: Select all
Toast.makeText(learn.this,
                  "Done",
                  Toast.LENGTH_SHORT).show();

Will i add anything besides this?
endhck
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Jul 06, 2010 3:20 pm

Re: Using Toast Widgets in Android

Postby endhck » Fri Jul 16, 2010 12:49 pm

I am sorry, it was related another thing.
endhck
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Jul 06, 2010 3:20 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests