I am currently learning Android Development and am confused about where to add certain functionality in the MainActivity.java file.
Some context: I am designing a basic coffee app in Android using Java. I have added a checkbox for toppings in the XML file.
<CheckBox
android:id="@ id/checkbox_whipped_cream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Whipped Cream"
android:paddingLeft="20dp"
android:textSize="25dp" />
When I click the order button on my app, I should be able to see this Whipped Cream checkbox ticked and displayed in the order summary.
So, where should in general one add the functionality in the .java file?
Here's the java code that I've written so far:
package android.example.updated_coffee_app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int quantity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increment(View view) {
quantity = quantity 1;
display(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
display(quantity);
}
public void order(View view) {
CheckBox whippedCream = (CheckBox) findViewById(R.id.checkbox_whipped_cream);
int price = calculatePrice();
String priceMessage = createOrderSummary(price);
displayMessage(createOrderSummary(price));
}
private String createOrderSummary(int price) {
String priceMessage = "Name : Toshali ";
priceMessage = priceMessage "\nCoffee Quantity: " quantity;
priceMessage = priceMessage "\nTotal: $" price;
priceMessage = priceMessage "\nThank You!";
return priceMessage;
}
private int calculatePrice() {
int price = 5 * quantity;
return price;
}
public void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
private void display(int numbers) {
TextView quantityTextView = (TextView) findViewById(R.id.quantityOfCoffee);
quantityTextView.setText(" " numbers);
}
}
In general, I'm confused about where does one add a particular piece of code? In what method(function)? Or do we make separate methods for all the components?
In the image, I should be able to see 'Whipped Cream? : True' in the order summary.
CodePudding user response:
Right now you only check the state of your checkbox when the order
runs.
If you want to isntantly react to the checkbox being checked / unchecked, you need to specify a listener.
Checkboxes allow you to specify a method in the XML, which is called whenever its state changes with "android:onClick=
".
You can change your code like this:
In your xml layout, add this to your CheckBox
element:
android:onClick="onCheckboxClicked"
In your activity, add this new method:
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
if(checked) {
// TODO: Show that whipped cream is selected
}
else {
// TODO: Show that whipped cream is not selected
}
}
Reference: https://developer.android.com/guide/topics/ui/controls/checkbox#java
Opinion on Android app code structure
You should have separate methods which react to UI events like clicking buttons and toggeling checkboxes. Ideally, those methods should update a UI state object.
Google's opinionated recommendation is that the state of your UI should be determined by UI state data classes. In your example a UI state class would hold the data for the price, the quantity and the selected extras (whipped cream).
Events like button presses will then update the UI state object. Whenever your UI state changes, you update the UI.
Check out this guide here: https://developer.android.com/jetpack/guide/ui-layer#define-ui-state