0% found this document useful (0 votes)
49 views4 pages

Toast Message: Activity - Main - XML

The document contains code for displaying a custom toast message in Android. It includes the XML layout files for the main activity and custom toast, and the Java code in the MainActivity class. When the button is clicked, it inflates the custom toast layout, sets its properties, and calls toast.show() to display it on the screen.

Uploaded by

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

Toast Message: Activity - Main - XML

The document contains code for displaying a custom toast message in Android. It includes the XML layout files for the main activity and custom toast, and the Java code in the MainActivity class. When the button is clicked, it inflates the custom toast layout, sets its properties, and calls toast.show() to display it on the screen.

Uploaded by

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

Toast Message

activity_main.xml

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

<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="10dp"

android:layout_y="9dp"

android:text="Hello World, Toast Example"/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="13dp"
android:layout_y="36dp"

android:text="Show Toast" />

</AbsoluteLayout>

Customtoast.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#000"

android:padding="10dp"

android:id="@+id/custtoast">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Message for you :"

android:textSize="30sp"

android:textColor="#fff"/>
<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="You have got mail?"

android:textSize="20sp"

android:textColor="#fff"/>

</LinearLayout>

MainActivity.java

package com.example.customtoast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button bt = (Button)findViewById(R.id.button);

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

LayoutInflater li = getLayoutInflater();

View layout = li.inflate(R.layout.customtoast,

(ViewGroup)findViewById(R.id.custtoast));

Toast toast = new Toast(getApplicationContext());

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.setGravity(Gravity.CENTER_HORIZONTAL|
Gravity.CENTER_VERTICAL,0,0);

toast.show();

} });

}}

You might also like