Toast Notification

Toast: In Android, Toast is a notification message that pop up, display a certain amount of time, and automtaically fades in and out, most people just use it for debugging purpose. Or, A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.

For example, when you sent email a message is popup and says email sent and vanishes from the screen.

To show Toast notification you need to call the method makeText as follows:

Toast.makeText(context, text, duration).show();

Parameters:

Context: the application context where you want to show the Toast.

Text: the message you want to display.

Duration: the duration of the Toast.

For Example:

//display in short period of time

Toast.makeText(MainActivity.this, "your msg goes here..", Toast.LENGTH_SHORT).show();

 //display in long period of time

Toast.makeText(MainActivity.this, " your msg goes here..", Toast.LENGTH_LONG).show();


You can even position the Toast on the screen:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);  // toast should appear in the top-left corner

If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.

You can create Toast in two ways:
  1.     .     Normal Toast.
  2.          Custom Toast (click on Custom Toast).


Normal Toast Example:

MainActivity.java:

public class MainActivity extends Activity {
      
       Button btn;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              btn = (Button) findViewById(R.id.button1);
             
              btn.setOnClickListener(new OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {

                           Toast.makeText(MainActivity.this"Oops Button is clicked !", Toast.LENGTH_SHORT).show();
                     }
              });
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="Show me the Toast" />

</RelativeLayout>

Right click the project and run the application







Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment