How to use Toast on Android

47

Toast is a method, With the help of toast you can display a message that appears on the screen for few times. If you want to use toast on your app is very simple just write few simple lines of code, after writing this code you can use this toast on your android example. If you want to more know about Toast you can read on the official Android developer site. Let’s begin the code.

Use Toast on Android

Below I can write code with help of onClickListner(This works when you press the button on your android), When you press the button then you see a message appears for some time on your android app.

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "You click here", Toast.LENGTH_SHORT).show();
            }
        });

Another way you can write toast code

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(getApplicationContext(), "You clicked here", Toast.LENGTH_SHORT);
                toast.setMargin(500,500);
                toast.show();
            }
        });

Full Example: How to use Toast

Let’s begin the code:

File: MainActivity.java

package com.androidfreeks.toast;

import androidx.appcompat.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 = findViewById(R.id.toast_example);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(getApplicationContext(), "You clicked here", Toast.LENGTH_SHORT);
                toast.setMargin(500,500);
                toast.show();
            }
        });
    }
}

File: 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=".MainActivity">

    <TextView
        android:id="@+id/AndroidFreeks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:gravity="center_horizontal"
        android:text="@string/android_freeks"
        android:textColor="#4CAF50"
        android:textSize="28sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/AndroidFreeks"
        android:fontFamily="monospace"
        android:gravity="center_horizontal"
        android:text="@string/simple_toast_example"
        android:textColor="#F44336"
        android:textSize="45sp"
        android:textStyle="bold" />


    <Button
        android:id="@+id/toast_example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:backgroundTint="#4CAF50"
        android:text="@string/click_here" />


</RelativeLayout>

Output

How to use toast on android

Must Read: How to use Loading Spinner in Android Studio

Hope you like this example, If you have any question-related android you will ask me with the help of a comment. I will reply to you as soon as possible Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here