Android Toast Application, Toast text on button click.

Android Toast Application, Toast text on button click.

In this Application We will Create an application that designs a layout with a text box and button named  submit. When the submit button is clicked than the text in the text box should be displayed in the toast.

First of All we need to design activity layout file "activity_main.xml" file.

<?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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:id="@+id/editbox"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
    </LinearLayout>
OUTPUT:-

MainActivity.java

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

public class MainActivity extends AppCompatActivity {
    private Button b1;
    private EditText editbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editbox=findViewById(R.id.editbox);
        b1=findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, editbox.getText(),
                 Toast.LENGTH_SHORT).show();
            }
        });
    }
}
    

Comments

Popular posts from this blog

Android Hello World Application

How To Hack Chrome Dinosaur Game

Select, Insert, Delete, and Update using Stored Procedure in ASP.NET