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

Practical 15.1

The document contains an Android application code with an XML layout and a Java activity. The layout includes a TextView and a Button that triggers a custom Toast message when clicked. The Toast displays a message at the center of the screen indicating that the user has received mail.

Uploaded by

Pruthesh Upadhye
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)
14 views2 pages

Practical 15.1

The document contains an Android application code with an XML layout and a Java activity. The layout includes a TextView and a Button that triggers a custom Toast message when clicked. The Toast displays a message at the center of the screen indicating that the user has received mail.

Uploaded by

Pruthesh Upadhye
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

15.1 activity_main.

xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Toast Example"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>

<Button
android:id="@+id/btnShowToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"/>
</LinearLayout>

15.1 MainActivity.java

package com.example.practical7;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Button btnShowToast;

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

btnShowToast = findViewById(R.id.btnShowToast);

btnShowToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast();
}
});
}
private void showCustomToast() {
Toast toast = Toast.makeText(getApplicationContext(), "Message for
you:\nYou have got mail!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}

15.1 Output

You might also like