Monday, August 17, 2015

How to create a custom Toast layout in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#7cb0ca"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Toast View"
        />
</RelativeLayout>
toast_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textStyle="italic"
    android:fontFamily="sans-serif-medium"
    android:text="Custom Toast View"
    android:background="@drawable/toast_bg"
    android:elevation="5dp"
    android:translationZ="5dp"
    />
res/drawable/toast_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#fff564"/>
            <stroke
                android:color="#ff3041"
                android:width="2dp"
                />
        </shape>
    </item>
</selector>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference of widgets from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the application context
                Context context = getApplicationContext();

                // Initializing an empty Toast
                Toast toast = new Toast(context);

                // Set the Toast duration
                toast.setDuration(Toast.LENGTH_SHORT);

                // Show the Toast on center
                toast.setGravity(Gravity.CENTER,0,0);

                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_custom_view,null);

                // Set the Toast custom layout
                toast.setView(layout);

                // Show the Toast on app interface
                toast.show();
            }
        });
  }
}
More android examples