Issue
Hello I am working on a project on Android Studio which is for a coffee shop. I am having trouble with my radio buttons. What I need is that after a user clicks a choice "Hot,Cold,Blended". The quantity(Which will be 1) and price will show automatically.
But what I have right now is that the user has to click on the quantity button first to show the price and quantity count. I would like for it to be automatically.
I have tried setting the quantity to 1 and setting the price to its base price but is not working.
Here is my code
public class menu1_item1 extends AppCompatActivity {
ImageView imageView;
RadioGroup radioG;
ImageButton plusquantity, minusquantity;
TextView quantitynumber, drinnkName, coffeePrice;
CheckBox addToppings, addExtraCream;
Button addtoCart;
int quantity;
public Uri mCurrentCartUri;
boolean hasAllRequiredValues = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu1_item1);
imageView = findViewById(R.id.imageViewInfo);
plusquantity = findViewById(R.id.addquantity);
minusquantity = findViewById(R.id.subquantity);
quantitynumber = findViewById(R.id.quantity);
drinnkName = findViewById(R.id.drinkNameinInfo);
coffeePrice = findViewById(R.id.coffeePrice);
addToppings = findViewById(R.id.addToppings);
addtoCart = findViewById(R.id.addtocart);
addExtraCream = findViewById(R.id.addCream);
radioG = findViewById(R.id.radioG);
drinnkName.setText("Iced Coffee");
plusquantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioID = radioG.getCheckedRadioButtonId();
if(radioID == R.id.hotradioButton){
int basePrice = 100;
quantity++;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}else if(radioID == R.id.coldRadioButton){
int basePrice = 140;
quantity++;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}else if(radioID == R.id.blendRadioButton){
int basePrice = 150;
quantity++;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}
}
private void displayQuantity() {
quantitynumber.setText(String.valueOf(quantity));
}
});
minusquantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioID = radioG.getCheckedRadioButtonId();
if (radioID == R.id.hotradioButton) {
int basePrice = 100;
if (quantity == 0) {
Toast.makeText(menu1_item1.this, "Cannot Decrease Quantity", Toast.LENGTH_SHORT).show();
} else {
quantity--;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}
}else if (radioID == R.id.coldRadioButton){
int basePrice = 140;
if (quantity == 0) {
Toast.makeText(menu1_item1.this, "Cannot Decrease Quantity", Toast.LENGTH_SHORT).show();
} else {
quantity--;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}
}else if(radioID == R.id.blendRadioButton){
int basePrice = 150;
if (quantity == 0) {
Toast.makeText(menu1_item1.this, "Cannot Decrease Quantity", Toast.LENGTH_SHORT).show();
} else {
quantity--;
displayQuantity();
int coffePrice = basePrice * quantity;
String setnewPrice = String.valueOf(coffePrice);
coffeePrice.setText(setnewPrice);
}
}
}
private void displayQuantity() {
quantitynumber.setText(String.valueOf(quantity));
}
});
}
}
Here is a visual on my layout design: https://ibb.co/cYL0BV6
Any help is appreciated!!
Solution
The reason the price and quantity are not initially showing is because you have the logic within the setOnClickListener
method. That logic will only be executed when the user clicks one of the radio buttons, and not when the activity is first launched.
You need to move the logic that sets the price and quantity up higher in your code (outside of setOnClickListener
), such as by where you set drinnkName.setText("Iced Coffee");
. Then, it will execute when the activity is launched.
Answered By - shagberg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.