The goal of this project is to create an Android mobile application that enables users to book a seat on a bus for a tour. The application will allow users to select their preferred seat from a visual representation of the bus seating arrangement. This document outlines the code necessary to implement this functionality and provides an explanation of how to package the application for installation on Android devices.
using Android.App;
using Android.OS;
using Android.Widget;
using System.Collections.Generic;
namespace BusSeatBookingApp
{
[Activity(Label = "Bus Seat Booking", MainLauncher = true)]
public class MainActivity : Activity
{
private List<Button> seatButtons;
private List<bool> seatAvailability;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
seatButtons = new List<Button>
{
FindViewById<Button>(Resource.Id.seat1),
FindViewById<Button>(Resource.Id.seat2),
FindViewById<Button>(Resource.Id.seat3),
FindViewById<Button>(Resource.Id.seat4),
FindViewById<Button>(Resource.Id.seat5)
};
seatAvailability = new List<bool> { true, true, true, true, true };
for (int i = 0; i < seatButtons.Count; i++)
{
int index = i; // Capture the current index
seatButtons[i].Click += (sender, e) => OnSeatButtonClick(index);
}
}
private void OnSeatButtonClick(int index)
{
if (seatAvailability[index])
{
seatButtons[index].SetBackgroundColor(Android.Graphics.Color.Green);
seatAvailability[index] = false;
Toast.MakeText(this, $"Seat {index + 1} booked!", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, $"Seat {index + 1} is already booked.", ToastLength.Short).Show();
}
}
}
}
The provided C# code is designed for an Android application that allows users to book bus seats. Here’s a breakdown of the key components:
- Namespace and Class Declaration: The application is encapsulated within the
BusSeatBookingApp
namespace and theMainActivity
class, which inherits fromActivity
. - UI Elements: The
seatButtons
list holds references to the buttons representing each seat, whileseatAvailability
keeps track of whether each seat is available (true) or booked (false). - OnCreate Method: This method initializes the activity. It sets the content view to a layout resource (not shown here) and populates the
seatButtons
list with button references. It also initializes theseatAvailability
list to indicate that all seats are available. - Event Handling: A loop attaches a click event handler to each seat button. When a button is clicked, it calls the
OnSeatButtonClick
method with the index of the clicked seat. - Booking Logic: The
OnSeatButtonClick
method checks if the selected seat is available. If it is, the seat button’s background color changes to green, indicating that it has been booked, and a toast message confirms the booking. If the seat is already booked, a different toast message informs the user.
Making the App Installable
To convert this code into an installable mobile app, follow these steps:
- Set Up Android Studio: Download and install Android Studio, which provides the necessary tools for Android app development.
- Create a New Project: Start a new project in Android Studio and select the “Empty Activity” template. This will generate the basic structure for your app.
- Add Layout Resources: Create a layout XML file (e.g.,
activity_main.xml
) that defines the UI elements, including buttons for each seat. Ensure that the IDs match those referenced in the code. - Build the App: Use the “Build” menu in Android Studio to compile the application. This process will generate an APK (Android Package) file.
- Test the App: You can test the app on an Android emulator or a physical device connected to your computer.
- Distribute the App: Once testing is complete, you can distribute the APK file for installation on other devices or publish it on the Google Play Store for wider accessibility.
By following these steps, you can successfully create and deploy a functional Android bus seat booking application.
Happy Coding.
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com