Tutorials

Custom Toast for Android – Example & Tutorial

By on 13 Dicembre 2014

This is a complete example to create a Custom Toast for Android in the style of XBOX360 Achievement (with relative sound).

1.Create the layout of our Custom Toast

In the layout section of our project, create a xml file. My file is called layout_toast.xml

Write this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/toast_layout_root"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@drawable/achievement_unlocked"
        android:textColor="@color/White"
         />

</LinearLayout>

 

Is important write the android:id of the linear layout. In the TextView background we can put our image that rapresent the custom toast. I recommend an image png with trasparency. The image must be in the drawable folder.

Attention !

In this layout i can put what i want. Buttons, TextViews, CheckBox ecc.. Only the useful part of layout will be showed in the final toast. So if i put only a little button the toast will be this button. The white area round the button will be not showed.

 

2.Create the layout of our Main

This layout is very easy. Is only a button.

<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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/button1_name" />

</RelativeLayout>

 

3.Create the MainActivity

In this Activity is important to inflate the layout of Custom toast. Once we have the reference we can modify all the values of that layout.

//I take my custom layout of the toast
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.layout_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));

//Now i can change the properties if i want
tvprova=(TextView)layout.findViewById(R.id.text1);

The line (ViewGroup) findViewById(R.id.toast_layout_root) indeed can be null.

Now to create the custom Toast i use this:

Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.BOTTOM, 0, 100);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

At the end when i click the button i call:

toast.show();

To play the sound we put our mp3 or other in the res/raw folder and we write this:

new Thread(){
    public void run(){
    MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.archsound);  
    mp.setVolume(0.9f, 0.9f);  //Volume sound from 0f to 1f
    mp.start();
    }
     }.start();

Finally this the complete MainActivity:

 

package com.example.customtoast;

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

	TextView tvprova;
	Toast toast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      
        Button btn = (Button) findViewById(R.id.button1);

        LayoutInflater inflater = getLayoutInflater();

        View layout = inflater.inflate(R.layout.layout_toast,
                (ViewGroup) findViewById(R.id.toast_layout_root));

        tvprova=(TextView)layout.findViewById(R.id.text1);

        //I can change the properties if i want
        //tvprova.setBackgroundResource(R.drawable.fiatlogo);

        toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 100);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

toast.show();

new Thread(){
 public void run(){
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.archsound);  
 mp.setVolume(0.9f, 0.9f); 
 mp.start();
                  }
                   }.start();

            }
        });

    }

This is the final result. The Custom Toast for Android:
toast1

toast2

 

TAGS
RELATED POSTS

LEAVE A COMMENT

IL FALCY
Turin, Italy

Hi my name is Falcy and this is my World! I use this blog theme to tell people my story. Through all the places and things I see around the world, there isn't a best way to share my experience! Follow my daily updates and discover with me the essence of traveling!

Visited Places