How to Build an Android Restaurant Bill Splitter App - Hive Programmers

avatar

Greetings to my favorite science community online StemSocial.

It's @skyehi and today presents another chance to share one more episode in my series Android App Development Tutorials for beginners.

Polish_20231221_130754690.jpgOriginal Image Source by Neo from Pexels

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

One big and rather awkward issue friends or family have when they go to the restaurant to eat is who pays the bill. Some people have enough to pay for the whole thing but mostly among casual friends, they have to split the bill evenly amongst themselves.

This usually ends up in some humorous and laughable situations and confusion but great Bill splitting Apps can solve the entire problem.

Well guys for today's tutorial we'll going to be building a basic model of an amazing Bill Splitting App. Since it's a beginner's tutorial, we'll work on the basic model of how the entire App would be built, in more advanced tutorials using Kotlin, we'll build a fuller App guys.

I hope you're ready for today's tutorial, without wasting anymore time let's get started with today's work.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Prerequisites

If you're a newcomer to my series, this part's for you.

Before you can successfully go through with this tutorial, you need to have some primary softwares installed on your PC.

First you need to install Java Development Kit, JDK on your computer. The reason is that we'll be writing the backend code or logic of our Bill Splitting App using Java language and your PC would definitely need JDK installed to be able to execute the Java instructions.

After installing JDK you'll need to download and install Android Studio IDE on your computer. The Android Studio software is the platform on which we'll build all our Android Apps. It's a great tool and very easy to learn how to use guys.

So head over to the download page and download it. If you're having troubles with that, let me know in the comments.

Alright guys at this point I'll assume my readers have successfully installed the necessary softwares. Let's start working on the project now.

Creating a New Android Project

The first thing to do is to open Android studio and click on "Create a new Android Studio project"

There are a few configurations to setup up guys. First of all ensure that " Empty Activity " is the selected template of your project. Click on the "next" button and it will send you to a page to set the App name and package name.

Choose whatever suitable name you want for your App but I'll encourage you to create a catchy and unique name because that will increase your chances of the App getting more downloads on play store.

As usual guys ensure that Java is the selected programming language and not Kotlin. When you're through with the setup click "finish" button and let Android Studio prepare your project.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Design the UI

Like I mentioned earlier, this is a basic model and so we may not include every single needed element but our Bill Splitter App should have very intuitive layouts for entering expenses, adding friends, and displaying the final split amounts.

In my example below I created an EditText layout for the user to input the expenses and a ListView element to display the list of friends.

Here's how that should look like

(html comment removed:  Example XML for expense input )
<EditText
    android:id="@+id/expenseAmount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Expense Amount"
    android:inputType="numberDecimal"/>

(html comment removed:  Example XML for displaying friends )
<ListView
    android:id="@+id/friendsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Data Model

We've been going over this same procedure in most of my recent tutorials. If you provide a list of elements or data, you would always need to create a class model that would include variables which represents each data.

We need to create data structures that would store expenses and friends.

In my example below, I created a new Java class which I called expense.java and in it I created a few variables. We'll have a string variable for description and a double variable to hold the amount to be paid.

Here's how the class code for expenses should look like

// Expense class
public class Expense {
    private String description;
    private double amount;

    public Expense(String description, double amount) {
        this.description = description;
        this.amount = amount;
    }

    // Getters and setters
}

// Friend class
public class Friend {
    private String name;
    private double share;

    public Friend(String name, double share) {
        this.name = name;
        this.share = share;
    }

    // Getters and setters
}

Now guys as you can see above I also created another class file called friend.java class where we'll be creating a string to hold the names of each friend that would be paying a portion of the bill and a double variable to hold the amount that the particular friend will pay.

Very easy and very logical right guys.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Implementing Expense Entry

Now guys remember we created an editText element in our User interface layout design which will be used to input the expenses, well it's time to now write the logic code that would handle expense input from the user.

It's a really easy step where we'll simply initialize the editText and create a newExpense method to handle the calculations.

Here's how your code should look like guys

EditText expenseAmount = findViewById(R.id.expenseAmount);
double amount = Double.parseDouble(expenseAmount.getText().toString());
Expense newExpense = new Expense("Dinner", amount);

Add Friends

I can't stress enough on the fact that this is basically a model example so I'll assume most of the layouts and design will be created by you.

However if you want more detailed work, just let me know in the comments or wait patiently until we get to intermediary level of our series and we'll build more complex Apps guys.

So guys we would need to have a list of friends displayed in our App. This would be the friends that have to split the bill at the restaurant.

Remember we had a ListView element created inside our design layout right, well it's time to initialize it and create a new Array to display the list.

Here's how your code should look like

ListView friendsList = findViewById(R.id.friendsList);
ArrayList<Friend> friends = new ArrayList<>();
// Populate the friends list

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Calculating Bill Split

It's now time for the main logic. Doing the splitting math would be done in this part. To do this we would need to implement a function that can calculate the split amounts based on expenses and friends.

This is merely a model example of how the function would look like but without the math.

public void calculateSplit(ArrayList<Friend> friends, ArrayList<Expense> expenses) {
    // Logic to distribute expenses among friends
}

The logic or math would be based on the price or cost of meals at the restaurant, what the friends ordered that day and how much each person is to contribute. It would definitely require some fractional mathematics. We'll go into details in more advanced tutorials guys.

For the meantime we have successfully built a great model of a Bill Splitter App for restaurants. You would probably need a well designed textview element that would display the final amount to be paid by each friend.

Also the Bill Splitter App can have Payment Integration so that friends could pay their bills through the same app. To make this possible we would probably need to integrate a payment API to facilitate direct transfers.

Thank you so much for taking the time to read today's blog. I hope you enjoyed this particular tutorial. Like I said earlier, this is merely a mdoel you can follow after to build a much more complete App.

If you're having troubles installing the necessary softwares or writing the code, please let me know in the comments.

I would like to thank the StemSocial community for weeks of support and encouragement. We'll be entering a new year and we're about to celebrate Christmas.

I think I'll be making a couple of Christmas Apps from tomorrow all the way to Christmas Day. I hope you'll love to see that guys.

Have a lovely day and catch you next time on StemSocial. Goodbye ❤️


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
3 comments
avatar

Ho Ho Ho! @skyehi, one of your Hive friends wishes you a Merry Christmas and asked us to give you a new badge!

The HiveBuzz team wish you a Merry Christmas!
May you have good health, abundance and everlasting joy in your life.

To find out who wanted you to receive this special gift, click here!

You can view your badges on your board and compare yourself to others in the Ranking

Check out our last posts:

It's the Christmas season: give your friends a gift
0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000
avatar

Congratulations!


You have obtained a vote from CHESS BROTHERS PROJECT

✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪


♟ We invite you to use our hashtag #chessbrothers and learn more about us.

♟♟ You can also reach us on our Discord server and promote your posts there.

♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.

♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.


🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.


Kindly

The CHESS BROTHERS team

0
0
0.000