Showing posts with label notification. Show all posts
Showing posts with label notification. Show all posts

April 14, 2011

How To display a Custom Toast Notification in Android

     If you need to display a simple Toast use this tutorial but if you want to customize your Toast Notification read the post below.

  1. Create your own layout for the Toast. Add it to the res/layout directory ( file - customizedtoast.xml).
  2. 
        
        
    
    
  3. Create a new LayourInflater, then call the inflate() method passing the Layout of the custom Toast and the root id (id of the linearLayour in our case) .
  4. LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.customizedtoast, 
      (ViewGroup) findViewById(R.id.customToastLayout));
    
  5. Create new Toast and pass the root of the layout to the setView() method of the Toast. Call the show() method.
  6. Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setView(layout);
    toast.show();
    

Example:


Complete code :

main.java
package com.blog.customizedtoastnotification;

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

public class main extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {

    LayoutInflater li = getLayoutInflater();
    View layout = li.inflate(R.layout.customizedtoast,
      (ViewGroup) findViewById(R.id.customToastLayout));

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setView(layout);
    toast.show();
   }
  });
 }
}
main.xml

 
 



April 13, 2011

How To display a simple Toast Notificatin in Android

To create a simple Toast Notification instantiate a Toast object passing the context, the text and the duration to its makeText() method, then call the show() method. The duration can be : Toast.LENGTH_LONG or Toast.LENGTH_SHORT.
Example:
Context context = getApplicationContext();
CharSequence text = "Write the toast message here";
int duration = Toast.LENGTH_SHORT;
Toast toastNodification = Toast.makeText(context, text, duration);
toastNodification.show();

You can also use the short (chained) version :
Toast.makeText(getApplicationContext(), "short version", Toast.LENGTH_SHORT).show();

Don't forget to add imports:
import android.content.Context;
import android.widget.Toast;

The result:


Complete code :

main.java

package com.blog.simpletoastnotification;

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

public class main extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button simpleBtn = (Button) findViewById(R.id.button1);
  simpleBtn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Context context = getApplicationContext();
    CharSequence text = "Write the toast message here";
    int duration = Toast.LENGTH_SHORT;
    Toast toastNodification = Toast.makeText(context, text, duration);
    toastNodification.show();
    
    // short version
   // Toast.makeText(getApplicationContext(),
"short version Toast", Toast.LENGTH_SHORT).show();
   }
  });

 }
}

main.xml