image1 image2 image3

HELLO I'M NITENDRA THAKUR|WELCOME TO MY PERSONAL BLOG|I LOVE TO DO CREATIVE THINGS|I'M PROFESSIONAL ANDROID APPLICATION DEVELOPER

What is toast in android programming?

What is Toasts?


Toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out, most people just use it for debugging purpose.It only fills the amount of space required for the message and the current activity remains visible and interactive.Toast is used to display information for short period of time.


Toast class is used to display the short duration message without creating the hindrance for the user interaction.It doesn't block the running activities.


Toast class extend java.lang.Object class i.e. android.widget.Toast class is the subclass of java.lang.Object class.




Types of Toast


1. Default Toast(comes with predefined parameters ) 

2.Custom Toast(Developer customize the Toast )



Toast constants



  • LENGTH_LONG - show the notification for long period of time(3.5sec).
  • LENGTH_SHORT - show the notification for short period of time(2.0sec).

Toast's Methods


  • makeText(Context context, int resId, int duration)


Make a standard toast that just contains a text view with the text from a resource.


Parameters
contextThe context to use. Usually your Application or Activity object.
resIdThe resource id of the string resource to use. Can be formatted text.
durationHow long to display the message. Either LENGTH_SHORT or LENGTH_LONG


  • makeText(Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.

Parameters
contextThe context to use. Usually your Application or Activity object.
textThe text to show. Can be formatted text.
durationHow long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Code to Implement Toast in Button


package com.example.buttondemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"I am Clicked", Toast.LENGTH_LONG).show();
}
});
}

}

ButtonDemo with Toast : Download ButtonDemo.zip



In Android, a toast message is a simple visual message that appears on the screen momentarily. Toast messages are managed by the application framework, but they are triggered by you, the developer. You can use toast messages as an actual part of a live app, but you can also use them for debugging. To show something that's happening at run time. I'm working in a project called ToastMessages. And in this first demonstration I'll show a ToastMessage on the screen as the app comes to the screen.

The first step is to declare an instance of the Toast class. I'll type the name of the class. And I'll press Ctrl+Space to be sure I've imported it, and I'll name the object simply message. Now to create a toast message object, use a method of the toast class called make text. There are two versions of the method, one that accepts a character sequence or a string. And one that receives a resource ID for string resources. I'll use a literal string.

The first argument is the context. And I'll use this. Meaning that this toast message object is being created in the scope of the current activity. Then I'll pass in a literal string of activity created. Finally, pass in the duration. Most commonly, developers use a constant of the Toast class. There are two available called LENGTH_LONG and LENGTH_SHORT. I'll use LENGTH_LONG. When a Toast message appears, it'll show up for a certain duration, and then go away automatically.

Now that I've created the message object, I can show it by calling a method of the object called show. I'll save my changes and run the app in the emulator. When the app appears, the toast message appears automatically. And then it goes away after a moment. To see that message again, I'll touch the back button, that unloads the application, then I'll go to my app list, and I'll locate my app, which is now named Toast, and I'll run it again.

And I see that the activity is being created anew. Toast messages can be used for this sort of debugging at app start-up. But they can also be used to track user events, events that are dispatched to tell you that the user has done something. One of the most common events happens when the user selects an item from the Options menu. For example, I'll go to my Options menu and choose Settings. Right now in this app, that doesn't do anything. But I'd like to find out when the user does it and if my code is correct for handling the event.

In Android when the user selects an option from the menu, you get an event. And this will be the first demonstration of creating an event handler. In the activity class I'll place the cursor below the existing methods. I'll type onopt, and then I'll press Ctrl+Space. And I'll choose an override of a method called onOptionsItemSelected. I'll press Enter, and Eclipse adds an override of that method. I'm now overriding a method of the Activity class.

And whatever code I add where the to do comment appears will be executed when the user chooses something from that menu. I'll get rid of that comment and then for the return statement I'll create a Toast message and display it. This time I'll use a slightly abbreviated syntax. I'll create the message and immediately show it. So I'm doing it all in a single statement instead of two statements. The code will be toast.maketext. Once again, I'll pass in this for the context, and the literal string will look like this.
You select it, and then I'll append a property of this item argument. This is an instance of the class MenuItem, and I'll call a method named getTitle, which returns a character sequence.Once again, I'll use a duration of LENGTH_LONG, and then once I've actually created the toast object, I'll immediately display it with .show at the end of the statement. And so now, every time the user selects an item from the Options menu, this code will be executed.

I'll run the app. And once again, as the app comes to the screen, I get the toast message, Activity Created. But then I'll go to the Options menu and choose Settings, and I see the message, You Selected Settings. When the message goes away, I'll try it again, and see that every time I choose an item from the menu, the message is displayed. So again, toast messages can be a really useful part of an actual Android app, but they're also used for debugging simple events.


To find out when something has happened, without having to dig through the Logcat view.




Share this:

CONVERSATION

0 comments:

Post a Comment