Skip to content

Where does one add a functionality in the file for android?

I am currently learning Android Development and am confused about where to add certain functionality in the file.

Some context: I am designing a basic coffee app in Android using Java. I have added a checkbox for toppings in the XML file.

        android:text="Whipped Cream"
        android:textSize="25dp" />

When I click the order button on my app, I should be able to see this Whipped Cream checkbox ticked and displayed in the order summary.

So, where should in general one add the functionality in the .java file?

Here’s the java code that I’ve written so far:

package android.example.updated_coffee_app;


import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    int quantity = 0;

    protected void onCreate(Bundle savedInstanceState) {

    public void increment(View view) {
        quantity = quantity + 1;

    public void decrement(View view) {
        quantity = quantity - 1;

    public void order(View view) {
        CheckBox whippedCream = (CheckBox) findViewById(;
        int price = calculatePrice();
        String priceMessage = createOrderSummary(price);

    private String createOrderSummary(int price) {
        String priceMessage = "Name : Toshali ";
        priceMessage = priceMessage + "nCoffee Quantity: " + quantity;
        priceMessage = priceMessage + "nTotal: $" + price;
        priceMessage = priceMessage + "nThank You!";
        return priceMessage;

    private int calculatePrice() {
        int price = 5 * quantity;
        return price;

    public void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(;

    private void display(int numbers) {
        TextView quantityTextView = (TextView) findViewById(;
        quantityTextView.setText(" " + numbers);


In general, I’m confused about where does one add a particular piece of code? In what method(function)? Or do we make separate methods for all the components?

In the image, I should be able to see ‘Whipped Cream? : True’ in the order summary.

Whipped Cream Checkbox option


Right now you only check the state of your checkbox when the order runs. If you want to isntantly react to the checkbox being checked / unchecked, you need to specify a listener.

Checkboxes allow you to specify a method in the XML, which is called whenever its state changes with “android:onClick=“.

You can change your code like this:

In your xml layout, add this to your CheckBox element:


In your activity, add this new method:

public void onCheckboxClicked(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    if(checked) {
        // TODO: Show that whipped cream is selected
    else {
        // TODO: Show that whipped cream is not selected


Opinion on Android app code structure

You should have separate methods which react to UI events like clicking buttons and toggeling checkboxes. Ideally, those methods should update a UI state object.

Google’s opinionated recommendation is that the state of your UI should be determined by UI state data classes. In your example a UI state class would hold the data for the price, the quantity and the selected extras (whipped cream).

Events like button presses will then update the UI state object. Whenever your UI state changes, you update the UI.

Check out this guide here: