App Development Tutorial : Android Discount Calculator

This tutorial will show how to build a discount calculator using Android Studio. The function of this app will to calculate the total price of an item after it has been discounted. For example, say you wanted to buy an item for $ 100.00 dollars but there was a sale and you could now get that item from 25% off. What would that price after the discount be?

First, we must build the layout of the app.

activity_main.xml

There are Four main views in this app : discount input , price input , saved amount output and discounted price output. Note, both the discount input and price are Edit Text. This is because the user is actually inputting the numbers in. The latter two are outputs, it the function of the app to display the answer.

<EditText
        android:id="@+id/discount"
        android:layout_marginTop="50dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Discount"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/price"
        android:layout_marginTop="9dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Price"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/save_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="You Saved"
        android:textColor="#062AED"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/discounted_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Discount Price"
        android:textColor="#07A60D"
        android:textSize="25sp" />

<!--Here is the button, when user clicks it the app will run-->
    <Button
        android:id="@+id/button"
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

The last object in the layout is the button, when the user clicks the button the app will run and spit out an output.

MainActivity.java

Okay so here is the code for the main function of the app. Iv add comments in the code below to explain what each line is doing.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//Here Variables are called
    private EditText priceTag;
    private EditText discountRate;
    private TextView savedAmount;
    private TextView discountedPrice;
    private Button Button;


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

// Creating Names for our variables 
        Button = findViewById(R.id.button);
        Button.setOnClickListener(this);
        priceTag = findViewById(R.id.price);
        discountRate = findViewById(R.id.discount);
        savedAmount = findViewById(R.id.save_price);
        discountedPrice = findViewById(R.id.discounted_price);

    }

//OnClick Method is created,happens when users clicks button 
    @Override
    public void onClick(View v) {

// Here is the most important part of the code 
//It is the calculating function 
        if (v.getId() == R.id.button) {
            if (!priceTag.getText().toString().equals("") && !discountRate.getText().toString().equals("")) {
                double number = Double.parseDouble(priceTag.getText()
                        .toString());
                double percentage = Double.parseDouble(discountRate.getText()
                        .toString());

                if (percentage >= 0 && percentage <= 100) {
                    double savedprice = (percentage / 100.0) * number;
                    double discountedAmount = number - savedprice;
                    String svdPrice = String.valueOf(savedprice);
                    String disprice = String.valueOf(discountedAmount);

    // Here the output of the equation is pulled to a text
                    savedAmount.setText("Saved : " + svdPrice);
                    discountedPrice.setText("Price After Discount : " + disprice);

                }

            } else {
// Display error if  -1 or 100+ number is chosen 
                Toast.makeText(this, "Discount 0 to 100 %",
                        Toast.LENGTH_LONG).show();
            }
        }
        else {
            Toast.makeText(this, "Only fill in Price and Discount%",
                    Toast.LENGTH_LONG).show();
        }
    }
}

That’s it! Run the app

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s