[go: up one dir, main page]

0% found this document useful (0 votes)
26 views7 pages

Android 10

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)
26 views7 pages

Android 10

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/ 7

Practical No.

10
Aim: Develop a program to implement Custom Toast Alert.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">

<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Your Favorite Sport"
android:textSize="24sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/footballButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket"
android:layout_below="@id/titleTextView"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/basketballButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basketball"
android:layout_below="@id/footballButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/tennisButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tennis"
android:layout_below="@id/basketballButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

Java Code:
package com.example.myapplication10;

import static
com.example.myapplication10.R.id.basketballButton;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication10.R;
public class MainActivity extends AppCompatActivity {

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

@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button footballButton = findViewById(R.id.footballButton);
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button basketballButton =
findViewById(R.id.basketballButton);
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button tennisButton = findViewById(R.id.tennisButton);

footballButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showSportSelectedToast("Cricket");
}
});
basketballButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showSportSelectedToast("Basketball");
}
});

tennisButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showSportSelectedToast("Tennis");
}
});
}
private void showSportSelectedToast(String sport) {
String message = "You selected " + sport;
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_SHORT).show();
}
}
Output:
1)

2)

You might also like