0% found this document useful (0 votes)
3 views2 pages

MAD (Ex 13)

The document contains the source code for an Android application that displays a restaurant menu. It includes a MainActivity class that sets up the user interface and handles menu item selections, showing a toast message with the dish name and price. The layout and menu resources are defined in XML files, providing a simple user interface with a welcome message and a list of dishes.
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)
3 views2 pages

MAD (Ex 13)

The document contains the source code for an Android application that displays a restaurant menu. It includes a MainActivity class that sets up the user interface and handles menu item selections, showing a toast message with the dish name and price. The layout and menu resources are defined in XML files, providing a simple user interface with a welcome message and a list of dishes.
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/ 2

SOURCE CODE

MainActivity.java

package com.example.restaurantmenu;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_items, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dish1:
showToast("Spaghetti Carbonara - $12");
return true;
case R.id.dish2:
showToast("Grilled Chicken Salad - $10");
return true;
case R.id.dish3:
showToast("Margherita Pizza - $15");
return true;
case R.id.dish4:
showToast("Beef Burger - $13");
return true;
case R.id.dish5:
showToast("Chicken Tacos - $11");
return true;
case R.id.dish6:
showToast("Chocolate Lava Cake - $8");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void showToast(String message) {


Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Our Restaurant!"
android:textSize="24sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>

</RelativeLayout>

menu_items.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/dish1"
android:title="Spaghetti Carbonara"/>
<item
android:id="@+id/dish2"
android:title="Grilled Chicken Salad"/>
<item
android:id="@+id/dish3"
android:title="Margherita Pizza"/>
<item
android:id="@+id/dish4"
android:title="Beef Burger"/>
<item
android:id="@+id/dish5"
android:title="Chicken Tacos"/>
<item
android:id="@+id/dish6"
android:title="Chocolate Lava Cake"/>
</menu>

You might also like