0% found this document useful (0 votes)
6 views2 pages

Experiment No4

The document outlines the creation of a Toast Message application in Android Studio, where users can input a string and display it as a toast message upon clicking a button. It includes the source code for the MainActivity and the XML layout for the user interface. The expected result is a successful execution of the code, demonstrating the functionality of toast messages in the app.

Uploaded by

Hairish Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Experiment No4

The document outlines the creation of a Toast Message application in Android Studio, where users can input a string and display it as a toast message upon clicking a button. It includes the source code for the MainActivity and the XML layout for the user interface. The expected result is a successful execution of the code, demonstrating the functionality of toast messages in the app.

Uploaded by

Hairish Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXPERIMENT NO:4 DATE:

CREATE A TOAST MESSAGE APPLICATION

Aim:

To create an “Toast Message” application in android studio.

Problem Statement:

Create a simple Android application that demonstrates the use of “Toast messages”.
The app should allow the user to interact with buttons, and when a button is clicked, a toast
message should be displayed.

The app should have a main screen with a Plain Text and button

1.PlainText: input string

2.Button:”click” toast the input string.

Source Code

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.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText text;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(EditText) findViewById(R.id.t1);
b=(Button) findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg=text.getText().toString();
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();

}
});

}
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<EditText
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="enter your message"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.157" />

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

RESULT

The code will be executed successfully and output will be verified.

You might also like