0% found this document useful (0 votes)
27 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)
27 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

Practical No.

10
Aim: Develop a program to implement Custom Toast Alert.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
d"
xmlns:tools="[Link]
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 [Link].myapplication10;

import static
[Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link].myapplication10.R;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button footballButton = findViewById([Link]);
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button basketballButton =
findViewById([Link]);
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
Button tennisButton = findViewById([Link]);

[Link](new
[Link]() {
@Override
public void onClick(View v) {
showSportSelectedToast("Cricket");
}
});
[Link](new
[Link]() {
@Override
public void onClick(View v) {
showSportSelectedToast("Basketball");
}
});

[Link](new
[Link]() {
@Override
public void onClick(View v) {
showSportSelectedToast("Tennis");
}
});
}
private void showSportSelectedToast(String sport) {
String message = "You selected " + sport;
[Link]([Link], message,
Toast.LENGTH_SHORT).show();
}
}
Output:
1)

2)

You might also like