0% found this document useful (0 votes)
2 views

pr15

The document outlines two practical exercises for developing Android applications. The first exercise involves creating a custom toast message using XML layout files and Java code, while the second exercise focuses on implementing checkboxes for selecting items and displaying the total cost in a toast message. Both exercises include detailed code snippets for the layout and activity files.

Uploaded by

Shivraj Chougule
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)
2 views

pr15

The document outlines two practical exercises for developing Android applications. The first exercise involves creating a custom toast message using XML layout files and Java code, while the second exercise focuses on implementing checkboxes for selecting items and displaying the total cost in a toast message. Both exercises include detailed code snippets for the layout and activity files.

Uploaded by

Shivraj Chougule
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/ 10

Name: Shivraj Prakash Chougule Enrolment No.

: 2215770083

Batch: T1 Roll No.: 12

Practical No. 15: Develop a program to implement Custom Toast.

X. Exercise

1. Write a program to display following toast message.

Program (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">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello
World, Toast Example" android:textColor="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content
"
android:layout_height="wrap_conten
t"
android:layout_marginTop="12dp"
android:text="Show Toast"
app:layout_constraintEnd_toEndOf="
parent"
app:layout_constraintHorizontal_bias=
"0.034"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toBottomOf
="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

Program (custom_layout.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"
android:background="#000" android:id="@+id/root_layout">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message For You : "
android:textColor="#fff" android:textSize="25dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="120dp"
tools:layout_editor_absoluteY="117dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have got a mail!"
android:textSize="18dp" android:textColor="#fff"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="120dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Program (MainActivity.java) –

package com.example.androidapplication_pat15_1; 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
btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override public
void onClick(View v) {
showCustomToast();
}
});
}
public void showCustomToast(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)
findViewById(R.id.root_layout));
Toast tst = new Toast(getApplicationContext());
tst.setGravity(Gravity.CENTER,0,0);
tst.setDuration(Toast.LENGTH_LONG);
tst.setView(layout); tst.show();
}}
Outp
ut –
2. Write a program to display three checkboxes and one button named “order “ as
shown below.

Program (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">

<CheckBox android:id="@+id/checkBox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="84dp"

android:text="Pizza"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<CheckBox android:id="@+id/checkBox2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="168dp"

android:text="Coffee"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<CheckBox

android:id="@+id/checkBox3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="256dp"
android:text="Burger"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498

"

app:layout_constraintStart_toStartOf="parent

"

app:layout_constraintTop_toTopOf="parent"

/>

<Button android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="368dp"

android:text="Order"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Program (custom_layout.xml) – <?xml

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

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e3e3e3" android:id="@+id/root_id">
<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView"

tools:layout_editor_absoluteX="wrap_cont

ent"

tools:layout_editor_absoluteY="wrap_cont

ent" android:maxLines="6"

android:textColor="#000"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Program (MainActivity.java) – package

com.example.androidapplication_pat15_2; import

androidx.appcompat.app.AppCompatActivity; import

android.os.Bundle; import

android.view.LayoutInflater; import

android.view.View; import android.view.ViewGroup;

import android.widget.Button; import

android.widget.CheckBox; import

android.widget.TextView; import

android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button btn;

CheckBox c1,c2,c3; String

bill = "Selected items: "; int

total = 0;
@Override protected void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = findViewById(R.id.button); c1

= findViewById(R.id.checkBox); c2

= findViewById(R.id.checkBox2);

c3 =

findViewById(R.id.checkBox3);

String[] amount = new String[]{"200","50","120"};

btn.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {

if(c1.isChecked()){ bill += "\n" +

c1.getText()+ ": " + amount[0]; total+=

Integer.parseInt(amount[0]);

if(c2.isChecked()){ bill += "\n" +

c2.getText() + ": " + amount[1]; total+=

Integer.parseInt(amount[1]);

if(c3.isChecked()){ bill += "\n" +

c3.getText()+ ": " + amount[2]; total+=

Integer.parseInt(amount[2]);

bill += "\nTotal: " + Integer.toString(total);

LayoutInflater inflater = getLayoutInflater();


View layout = inflater.inflate(R.layout.custom_layout,(ViewGroup)
findViewById(R.id.root_id));

TextView txt = layout.findViewById(R.id.textView);

txt.setText(bill);

Toast tst = new Toast(getApplicationContext());

tst.setDuration(Toast.LENGTH_LONG); tst.setView(layout);

tst.show();

bill = "Selected items: ";

total = 0;

});

}
Output –

You might also like