• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 8 comments


SIMPLE TOAST


To display a simple toast, we need to makeText for that toast & then call show() to display it.
To use makeText(), we need to send 3 arguments.
1) Context
2) Text to display in toast
3) Duration How much time its need to be show
At last call show() to display toast.


SOURCE CODE is

package com.ToastExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ToastExample extends Activity
{
                public void onCreate(Bundle savedInstanceState)
                {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                 Toast msg = Toast.makeText(ToastExample.this,
                                                "YOUR TEXT HERE", Toast.LENGTH_LONG);
                                msg.show();

                 }
}



The OUTPUT will be


https://lh4.googleusercontent.com/_xnrF0YSUul8/TVpxrtIzlkI/AAAAAAAAAA0/rIqQmEb4AJ0/simple_toast.png

8 Responses so far.

  1. Vladimir says:

    Thank you, friend,
    Good example. May I ask question?
    This line
    Toast msg = Toast.makeText(ToastExample.this, "YOUR TEXT HERE", Toast.LENGTH_LONG);
    I replaced by (I wanted to see the toast more time - 15 s)
    Toast msg = Toast.makeText(ToastExample.this, "YOUR TEXT HERE", 15000);

    But the toast disappeared very quickly again. What I did wrong?
    Thank you,
    Vladimir

  2. Android says:

    Hi Vladimir..!,

    We can set custom time duration by

    msg.setDuration(INTEGERVALUE);

    But, I don't think that it shows the message for the particular time period.

    We have 2 time duration variables in the class Toast.

    LENGTH_LONG & LENGTH_SHORT.

    If i get any solution, i'll post it.

    Thanks,
    Jack

  3. package com.commonsware.android.skeleton;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Button;
    import android.view.View;
    import android.widget.Toast;
    import android.content.Context;
    import android.view.View.OnClickListener;
    public class App_manishActivity extends Activity {
    /** Called when the activity is first created.
    * @param
    * @param */

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn=(Button)findViewById(R.id.btn);

    Button.OnClickListener listener = new Button.OnClickListener() {




    public void onClick(View v)
    {

    Toast msg = Toast.makeText(App_manish.this,"YOUR TEXT HERE", Toast.LENGTH_LONG);
    msg.show();
    }
    };
    btn.setOnClickListener(listener);
    }
    }



    please help me, I am tired now, I dont know why there is still a error in Toast????
    please help

  4. Jack says:

    Hi Manish,

    You should have use App_manishActivity.this instead of App_manish.this.

  5. Hi Manish,

    I saw all examples about android , all examples are very nice, good job manish , can you please upload the existing database to android examples

    Thaks & Regards
    Chiranjeevi(windows phone 7,8 , windows 8 apps ,Mono for Android Developer)

  6. wasim says:

    Respected Sir,
    I want to know that how to get the toast message when i click the Button .......plz tell me the code.i am waiting form the code.....thanks

  7. Jack says:

    Hi wasim.

    See this link.

    http://android-helper.blogspot.in/2011/04/android-simple-button-click-example.html

  8. Feeling superb to share the link to practice important android interview questions @ http://skillgun.com

Leave a Reply