Custom toast in android

Toast with Custom layout example in android

Toast with Custom layout example in android

We already learn what is Toast in previous tutorial .

Now we are going to learn how make a Toast with custom layout in android , we have taken here an activity class named "CustomToastActivity.java" and related xml file "activity_custom_toast.xml" and custom xml layout for toast

is "toast_layout.xml".

Short code of custom toast.

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

        TextView tv_toast = (TextView) layout.findViewById(R.id.tv_toast);
        tv_toast.setText("Hi, Your custom Toast is also ready.");
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();

Now have a look to the source code.

CustomToastActivity.java


package com.androidfromhome.customtoast;

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

public class CustomToastActivity extends Activity implements OnClickListener {

    private Button btn_toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_toast);
        btn_toast=(Button)findViewById(R.id.btn_toast);
        btn_toast.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_toast:
            getCustomToast();
            break;

        default:
            break;
        }
        
    }
    
    
    public void getCustomToast(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,null);

        TextView tv_toast = (TextView) layout.findViewById(R.id.tv_toast);
        tv_toast.setText("Hi, Your custom Toast is also ready.");
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

    
}


activity_custom_toast.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="Toast Me" />
    </LinearLayout>

</LinearLayout>


 

toast_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_rectangle"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:padding="2dp" >

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_lock_idle_lock" />

        </FrameLayout>

        <TextView
            android:id="@+id/tv_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sorry It&apos;s lock..."
            android:textColor="#ffffff"
            android:textSize="15dp" />

    </LinearLayout>

</LinearLayout>

 


 

Download Source Code

 

 

Related Post:-

Simple Alert Dialog in android

Custom Alert Dialog in android with custom xml layout

ListView in android example

How to create AVD in android

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget