Refactored Menu - New 'Event Handling'
If you recall from Step 01, our Base class has 3 particular methods that we haven't made use of - yet
public void settings(MenuItem item)
{
Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show();
}
public void report(MenuItem item)
{
startActivity (new Intent(this, Report.class));
}
public void donate(MenuItem item)
{
startActivity (new Intent(this, Donate.class));
}
}
We will now use these methods, and using the onClick attribute in our MenuItems, we will 'bind' these methods to our menu options.
Firstly, edit your menu_donate.xml and ensure it now looks as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Donate">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:onClick="settings"/>
<item
android:id="@+id/menuReport"
android:orderInCategory="100"
android:title="@string/menuReport"
app:showAsAction="never"
android:onClick="report"/>
<item
android:id="@+id/menuDonate"
android:orderInCategory="100"
android:title="@string/menuDonate"
app:showAsAction="never"
android:onClick="donate"/>
</menu>
note the added onClick attributes for each MenuItem, it directly corresponds to the method names in our Base class
We can now refactor our Donate & Report classes and Remove the menu inflation and event handling code in both classes.
So, both classes should look as follows:
Donate
public class Donate extends Base {
private Button donateButton;
private RadioGroup paymentMethod;
private ProgressBar progressBar;
private NumberPicker amountPicker;
private EditText amountText;
private TextView amountTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
donateButton = (Button) findViewById(R.id.donateButton);
paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
amountText = (EditText) findViewById(R.id.paymentAmount);
amountTotal = (TextView) findViewById(R.id.totalSoFar);
amountPicker.setMinValue(0);
amountPicker.setMaxValue(1000);
progressBar.setMax(10000);
amountTotal.setText("$0");
}
public void donateButtonPressed (View view)
{
String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
int donatedAmount = amountPicker.getValue();
if (donatedAmount == 0)
{
String text = amountText.getText().toString();
if (!text.equals(""))
donatedAmount = Integer.parseInt(text);
}
if (donatedAmount > 0)
{
newDonation(new Donation(donatedAmount, method));
progressBar.setProgress(totalDonated);
String totalDonatedStr = "$" + totalDonated;
amountTotal.setText(totalDonatedStr);
}
}
}
Report
public class Report extends Base
{
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
listView = (ListView) findViewById(R.id.reportList);
DonationAdapter adapter = new DonationAdapter(this, donations);
listView.setAdapter(adapter);
}
}
Run your app once more, to confirm the changes and that your menu still works as it should.