Android Simple Toast Example - ANDROID - Helper

Tuesday, February 15, 2011

Android Simple Toast Example


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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDoMwJgEnHRsqUYwdolJ2Az4WjldxQYibQG5UtW8TAKRH5aznT6cxjohzGXe-zvMrvN0In1Nppf3hdAqsaTncCgxHcmEhwm5265uJsdg6OHeabfLxl1i5YylAS0Jo4NPT3pE8GuG930Xo/

7 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete
  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

    ReplyDelete
  4. Hi Manish,

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

    ReplyDelete
  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)

    ReplyDelete
  6. 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

    ReplyDelete
  7. Hi wasim.

    See this link.

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

    ReplyDelete