Develop An Application To Displ
Develop An Application To Displ
Develop an application to display three checkboxes and one button named “Order
“as shown below. Once you click on button it should toast different selected
checkboxes along with items individual and total price.
<CheckBox
android:id="@+id/checkbox_pizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza - 100 Rs" />
<CheckBox
android:id="@+id/checkbox_coffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee - 50 Rs" />
<CheckBox
android:id="@+id/checkbox_burger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger - 120 Rs" />
<Button
android:id="@+id/button_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place Order"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/text_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:layout_marginTop="20dp" />
</LinearLayout>
package com.example.q12;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkboxPizza = findViewById(R.id.checkbox_pizza);
checkboxCoffee = findViewById(R.id.checkbox_coffee);
checkboxBurger = findViewById(R.id.checkbox_burger);
buttonOrder = findViewById(R.id.button_order);
textOutput = findViewById(R.id.text_output);
buttonOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int totalAmount = 0;
StringBuilder result = new StringBuilder("Selected Items:");
if (checkboxPizza.isChecked()) {
result.append("\nPizza - 100 Rs");
totalAmount += 100;
}
if (checkboxCoffee.isChecked()) {
result.append("\nCoffee - 50 Rs");
totalAmount += 50;
}
if (checkboxBurger.isChecked()) {
result.append("\nBurger - 120 Rs");
totalAmount += 120;
}
textOutput.setText(result.toString());
Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}