0% found this document useful (0 votes)
66 views

PRG 6

The document describes an Android app that calculates discounts on a shopping bill amount. It uses an edit text for entering the bill amount, radio buttons to select a discount percentage of 20%, 25%, or 30%, and displays the calculated discount amount in a text view upon button click.

Uploaded by

Syed Salman
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)
66 views

PRG 6

The document describes an Android app that calculates discounts on a shopping bill amount. It uses an edit text for entering the bill amount, radio buttons to select a discount percentage of 20%, 25%, or 30%, and displays the calculated discount amount in a text view upon button click.

Uploaded by

Syed Salman
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/ 3

Program 6: Create an app that uses radio button group which calculates

discount on shopping bill amount. Use edit-text to enter bill amount and select
one of three radio buttons to determine a discount for 20, 25, or 30 percent. The
discount is calculated upon selection of one of the buttons and displayed in a
textview control.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Enter_bill_amount"
android:id="@+id/bill_amount_message"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:id="@+id/bill_amount_edit_text"
android:layout_below="@id/bill_amount_message"/>

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/discount_radio_group"
android:layout_below="@id/bill_amount_edit_text">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_20"
android:id="@+id/twenty_percent_radio_button"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_25"
android:id="@+id/twenty_five_percent_radio_button"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_30"
android:id="@+id/thirty_percent_radio_button"/>
</RadioGroup>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calculate_discount"
android:id="@+id/calculate_button"
android:layout_below="@id/discount_radio_group"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/discount"
android:id="@+id/discount_text_view"
android:layout_below="@id/calculate_button"/>

</RelativeLayout>

MainActivity.java
package com.example.prg6;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText billAmountEditText;


private RadioButton twentyPercentRadioButton;
private RadioButton twentyFivePercentRadioButton;
private RadioButton thirtyPercentRadioButton;
private TextView discountTextView;

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

billAmountEditText =
findViewById(R.id.bill_amount_edit_text);
RadioGroup discountRadioGroup =
findViewById(R.id.discount_radio_group);
twentyPercentRadioButton =
findViewById(R.id.twenty_percent_radio_button);
twentyFivePercentRadioButton =
findViewById(R.id.twenty_five_percent_radio_button);
thirtyPercentRadioButton =
findViewById(R.id.thirty_percent_radio_button);
Button calculateButton =
findViewById(R.id.calculate_button);
discountTextView = findViewById(R.id.discount_text_view);

calculateButton.setOnClickListener(new
View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
double billAmount =
Double.parseDouble(billAmountEditText.getText().toString());
double discountPercentage = 0.0;
if (twentyPercentRadioButton.isChecked()) {
discountPercentage = 0.2;
} else if (twentyFivePercentRadioButton.isChecked())
{
discountPercentage = 0.25;
} else if (thirtyPercentRadioButton.isChecked()) {
discountPercentage = 0.3;
}
double discountAmount = billAmount *
discountPercentage;
discountTextView.setText("Discount: Rs." +
discountAmount);
}
});
}
}

You might also like