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
Thank you, friend,
ReplyDeleteGood 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
Hi Vladimir..!,
ReplyDeleteWe 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
package com.commonsware.android.skeleton;
ReplyDeleteimport 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
Hi Manish,
ReplyDeleteYou should have use App_manishActivity.this instead of App_manish.this.
Hi Manish,
ReplyDeleteI 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)
Respected Sir,
ReplyDeleteI 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
Hi wasim.
ReplyDeleteSee this link.
http://android-helper.blogspot.in/2011/04/android-simple-button-click-example.html