If you want to show any message to the user then you need to show toast in android. This toast is just like a snack bar. You can also make a custom toast.
To display a message for a little information in your android application we use Toast class in android. An android toast displays a small message to the users.
We can say it's just a little information for the user for a few seconds and disappears after some time. Normally, the android toast is displayed at the bottom of the screen but you can also change display position and layout.
Android toast vs snackbar
Sometimes we need to show a piece of information to the user as a popup, but I can't use the bootstrap modal for that. So that time we need to create a Snackbar or we can say that a Toast message.
How To Create a Snackbar / Toast Bootstrap
First of all you require to add Toast class in your activity:
import android.widget.Toast;
Android toast duration
Generally, Toast is a popup for showing a piece of small information to the user, this is also using in the android application.
We can customize our Toast layout according to our requirements. It will be shown on the screen when you call it and It will disappear after a few seconds, whatever you have set.
There are two constants of Toast class which are given below for time duration.
Length Long
If you want to show any message to the user for a long duration, then you need to implement this on your toast setting.
Default duration for length long is 3.5 seconds.
LENGTH_LONG
you can use this just like:
Toast.LENGTH_LONG
Length Short
If you want to show any message to the user for a short duration, then you need to implement this on your toast setting.
Default duration for length long is 2 seconds.
LENGTH_SHORT
you can use this just like:
Toast.LENGTH_SHORT
Android Toast Example
Example for simple toast message:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.legendblogs.demo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Example of Toast"
android:id="@+id/textView"
android:layout_marginTop="91dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to get Toast"
android:id="@+id/button"
android:layout_marginTop="48dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity.java:
package com.legendblogs.demo;
import android.content.Context;
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 {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
Button button_toast = (Button) findViewById(R.id.button);
button_toast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Hello Legend Blogs User",Toast.LENGTH_SHORT).show();
}
});
}
}
You can create your custom toast in android. By using XML layouts. So, just create an XML layout and attach layout with your Toast class object.
cheers,
Write a comment