Toast example in android



                   Toast in android is quite similar to a popup or a dialogbox that displays message to the user. But it has various pros comparing popup. Example of toast is whenever you edit a message and exit inbox without sending, toast will be displayed saying  "Message saved as Draft".

Pros
  • Toast disappear after a certain time, without a need for user interaction.
  • When toast is displayed, the responsible application still remains accessible by the user.
  • Toast can be hided anytime by touching the screen.
Lets go ahead with an example of android toast and see how it works. Its a simple example. 
Three parameters are required to display a toast application context, text to display, duration.
Duration can take only two parameters - Toast.LENGTH_SHORT (displays message for short time), Toast.LENGTH_LONG(displays message for long time.)

Toast toast = Toast.makeText(getApplicationContext(), text, duration);

Toast can be positioned anywhere in the screen as shown below,

toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);

setGravity takes three parameters, first parameters positions the toast somewhere in the screen, I have placed it at bottom-center, in the same way it can be positioned at top-left, top-right etc., Next two parameters  takes care of fine tuning the position from the location indicated through first parameter. 
Finally display the toast as shown below,

toast.show();
Android Toast
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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:text="Click me!" />

</RelativeLayout>


MainActivity.java

Download eclipse project here


package com.example.androidtoast;

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

public class MainActivity extends Activity {
 private Button button;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.buttonToast);
  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    CharSequence text = "Welcome to CTE.com !";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(getApplicationContext(), text,
      duration);
    toast.setGravity(Gravity.CENTER | Gravity.CENTER, 10, 0);
    toast.show();
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
}

 



Reactions:

0 comments :

Post a Comment