Answer the question
In order to leave comments, you need to log in
Android/Java - how to output multiple variables per line?
I'm making a simple coffee ordering app based on one course from Udacity , just adding my own features, because they leave a lot of "holes".
In general, the user there clicked on the required number of cups of coffee, added cream and / or chocolate as desired, and pressed the "order" button.
Depending on what he clicked there, the strings.xml file stored lines like:
<string name="total_coffee_cost">You ordered %1$d cups of coffee. \nPrepare to pay $%2$d. \nThank you!</string>
<string name="total_coffee_cost_zero">You have not ordered any cup of coffee. \nTry to choose again. \nThank you!</string>
private TextView createOrderSummary(View view){
CheckBox whippedCreamCheckBox = findViewById(R.id.whipped_cream_checkbox);
hasWhippedCream = whippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = findViewById(R.id.chocolate_checkbox);
hasChocolate = chocolateCheckBox.isChecked();
TextView priceMessage = findViewById(R.id.price_text_view);
if (numberOfCoffees == 0)
priceMessage.setText(getString(R.string.total_coffee_cost_zero));
....
else if ((numberOfCoffees > 1) && (!hasWhippedCream) && (!hasChocolate))
priceMessage.setText(getString(R.string.total_coffee_cost, numberOfCoffees, numberOfCoffees * priceOfCup));
....
return priceMessage;
}
<string name="total_coffee_cost">Hi, %1$s! You ordered %2$d cups of coffee. \nPrepare to pay $%3$d. \nThank you!</string>
Answer the question
In order to leave comments, you need to log in
What do the parameters of the setText() method have to do with it? You are passing arguments to getString() . It can have any number of formatArgs arguments. Read about varargs.
I'm guessing you're just a little confused. For convenience, declare a variable for the text:
String textString = getString(R.string.total_coffee_cost, numberOfCoffees, numberOfCoffees * priceOfCup, param3, param4);
priceMessage.setText(textString);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question