0% found this document useful (0 votes)
7 views6 pages

Practical 15 - Krishna

The document provides two Android application examples. The first example demonstrates how to display a toast message when a button is clicked, while the second example showcases a user interface with checkboxes and a button that, when clicked, displays a toast with the selected items. Both examples include XML layout files and corresponding Java code for the MainActivity class.
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)
7 views6 pages

Practical 15 - Krishna

The document provides two Android application examples. The first example demonstrates how to display a toast message when a button is clicked, while the second example showcases a user interface with checkboxes and a button that, when clicked, displays a toast with the selected items. Both examples include XML layout files and corresponding Java code for the MainActivity class.
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/ 6

15_1 Write a program to display following toast message.

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world,Toast Example"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="22dp"
android:id="@+id/txt"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_below="@+id/txt"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:id="@+id/btn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~Performed by Krishna Israni - 36"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="@+id/btn"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
MainActivity.java:
package com.example.15_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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);
//Linking
Button btn;
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Name:- Anish Dharnidhar\nRollNo:- 07",
Toast.LENGTH_SHORT).show();
}
});
}
}

Output:
15_2 Write a program to display three checkboxes and one button named “Order “as shown
below. Once you clicked on button it should toast different selected checkboxes along with
items individual and total price.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/apple"
android:text="Apple"
android:textSize="30dp"
android:textStyle="italic"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Banana"
android:id="@+id/banana"
android:textSize="30dp"
android:textStyle="italic"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mango"
android:id="@+id/mango"
android:textSize="30dp"
android:textStyle="italic"
android:layout_centerHorizontal="true"
android:layout_marginTop="220dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:id="@+id/order"
android:textStyle="italic"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="280dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Performed by Krishna Israni 36"
android:layout_marginTop="550dp"
android:layout_marginLeft="130dp"
/>
</RelativeLayout>

MainActivity.java
package com.example.a15_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox c1,c2,c3;
Button btn;
String order="Selected Items in Busket are: ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.order);
c1=findViewById(R.id.apple);
c2=findViewById(R.id.banana);
c3=findViewById(R.id.mango);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (c1.isChecked()) {
order = order + "\nApple";
}
if (c2.isChecked()) {
order = order + "\nBanana";
}
if (c3.isChecked()) {
order = order + "\nMango";
}

Toast.makeText(MainActivity.this, "" + order, Toast.LENGTH_LONG).show();


order = "Selected Items in Basket are: "; // Resetting the order string for the next click

}
});
}
}

Output:

You might also like