Android Toast Example And Costume Toast Example

July 30, 2017

Android Toast is a like notification message pop up. Basically android toast use for show basic information very short time. Android Toast use for only user satisfaction. You can message show long time and short time and use custom android toast by use android XML. You can set image in custom toast in android. We can show any result by use android toast like click here

 

Now two type android toast

  1. Normal android toast view.

 

Toast.makeText(getApplicationContext(),“Hello! This is Normale Toast”, Toast.LENGTH_SHORT).show();// Toast

android toast

android toast

 

  1. Custom android toast view.

 

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.activity_custome__toast,(ViewGroup) findViewById(R.id.two));

 

ImageView images = (ImageView) layout.findViewById(R.id.images); //Custone toast image view

images.setImageResource(R.drawable.logo_onlinetech24);

 

TextView texts = (TextView) layout.findViewById(R.id.texts); //Custone toast text view

texts.setText(“Hello! This is Custome Toast”);

android toast

android toast

 

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

 

 

Xml

 

<?xml version=“1.0” encoding=“utf-8”?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

  xmlns:app=“http://schemas.android.com/apk/res-auto”

  xmlns:tools=“http://schemas.android.com/tools”

  android:layout_width=“match_parent”

  android:background=“#3b3a3a”

  android:layout_height=“match_parent”

  tools:context=“com.onlinetech24.sm.toast.Custome_Toast”>

 

  <ImageView android:id=“@+id/images”

      android:layout_width=“50dp”

      android:layout_height=“50dp”

      android:layout_marginRight=“10dp” />

 

  <TextView android:id=“@+id/texts”

      android:layout_width=“wrap_content”

      android:layout_height=“wrap_content”

      android:textColor=“#FFF” />

 

</LinearLayout>

 

Full Source

 

1:XML Code

activity_main.xml

 

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”

android:gravity=”center”

android:layout_height=”match_parent”

android:background=”#332c2c”

tools:context=”com.onlinetech24.sm.toast.MainActivity”>

 

<Button

android:id=”@+id/one”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Normal Toast”

android:textColor=”#fff”

android:layout_margin=”5dp”

android:textStyle=”bold”

android:layout_gravity=”center”

android:background=”#04444b”/>

android toast

android toast

<Button

android:id=”@+id/two”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Custome Toast”

android:textColor=”#fff”

android:layout_margin=”5dp”

android:layout_below=”@id/one”

android:textStyle=”bold”

android:layout_gravity=”center”

android:background=”#04490d”/>

 

</RelativeLayout>

 

activity_custome__toast.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:app=”http://schemas.android.com/apk/res-auto”

xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”

android:background=”#3b3a3a”

android:layout_height=”match_parent”

tools:context=”com.onlinetech24.sm.toast.Custome_Toast”>

 

<ImageView android:id=”@+id/images”

android:layout_width=”50dp”

android:layout_height=”50dp”

android:layout_marginRight=”10dp” />

 

<TextView android:id=”@+id/texts”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:textColor=”#FFF” />

 

</LinearLayout>

 

2: Java Class

 

MainActivory.java

 

package com.onlinetech24.sm.toast;

 

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  Button button;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

 

      // Button Onclick method for show Toast

      button=(Button)findViewById(R.id.one);

      button.setOnClickListener(new View.OnClickListener() {

 

          @Override

          public void onClick(View v) {

 

              Toast.makeText(getApplicationContext(),“Hello! This is Normale Toast”, Toast.LENGTH_SHORT).show();// Toast

          }

      });

      // Button Onclick method for show custome Toast another Activty.

      button=(Button)findViewById(R.id.two);

      button.setOnClickListener(new View.OnClickListener() {

          @Override

          public void onClick(View v) {

              Intent i = new Intent(getApplicationContext(),Custome_Toast.class);

              startActivity(i);

          }

      });

  }

}

 

Custome_Toast.java

 

package com.onlinetech24.sm.toast;

 

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class Custome_Toast extends AppCompatActivity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_custome__toast);

 

      LayoutInflater inflater = getLayoutInflater();

      View layout = inflater.inflate(R.layout.activity_custome__toast,(ViewGroup) findViewById(R.id.two));

 

      ImageView images = (ImageView) layout.findViewById(R.id.images); //Custone toast image view

      images.setImageResource(R.drawable.logo_onlinetech24);

 

      TextView texts = (TextView) layout.findViewById(R.id.texts); //Custone toast text view

      texts.setText(“Hello! This is Custome Toast”);

 

      Toast toast = new Toast(getApplicationContext());

      toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

      toast.setDuration(Toast.LENGTH_LONG);

      toast.setView(layout);

      toast.show();

  }

}

 

3: drawable

User “logo_onlinetech24.png” in drawable

 

 

android toast

android toast

No Comments

Leave a Reply

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