How to Display Toast message in android studio

How to display toast when clicking on the button.  This video shows you how to display a toast message in android. We will display to toast message long length and a short length. 



activity_main.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:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toast"
    android:text="Display"/>

</LinearLayout>


MainActivity.java
package com.example.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 toastdisplay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        toastdisplay=findViewById ( R.id.toast );
        toastdisplay.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View v) {
               // Toast.makeText ( getApplicationContext (),"this is toast",Toast.LENGTH_SHORT ).show ();

                Toast.makeText ( MainActivity.this, "this is second toast", Toast.LENGTH_LONG ).show ();

            }
        } );
    }
}

watch video 
 https://youtu.be/vS2Ztk512iE 

SUBSCRIBE to download

Download Source code
Post a Comment (0)
Previous Post Next Post