How to Show Custom Toast on Button Click in App| Android Studio Tutorials for Beginner 2022

How to Show Custom Toast on Button Click in App| Android Studio Tutorials for Beginner 2022




Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.




Show Toast on button click Android Studio





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:id="@+id/showtoastbtn"
        android:text="Show Toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>











MainActivity.java


package com.usmtip.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        Button showToastBtn = findViewById(R.id.showtoastbtn);
        showToastBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "This is Toast...", Toast.LENGTH_LONG).show();
            }
        });

    }
}











All Server Links to Download This File:



Previous Post
Next Post

post written by:

0 Comments: