Android Programming Tutorials

(Romina) #1
Menus and Messages

void populateFrom(Restaurant r) {
name.setText(r.getName());
address.setText(r.getAddress());

if (r.getType().equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);
}
else if (r.getType().equals("take_out")) {
icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}
}
}
}

Extra Credit.................................................................................................


Here are some things you can try beyond those step-by-step instructions:



  • Try using an AlertDialog instead of a Toast to display the message.

  • Try adding a menu option to switch you between tabs. In particular,


change the text and icon on the menu option to reflect the other tab
(i.e., on the List tab, the menu should show "Details" and the details

tab icon; on the Details tab, the menu should show "List" and the
List tab icon).


  • Try creating an ErrorDialog designed to display exceptions in a
    "pleasant" format to the end user. The ErrorDialog should also log
    the exceptions via android.util.Log. Use some sort of runtime


exception (e.g., division by zero) for generating exceptions to pass
to the dialog.

Further Reading..........................................................................................


You can learn more about menus – both option menus and context menus



  • in the "Applying Menus" chapter of The Busy Coder's Guide to Android


Development. The use of a Toast is covered in the "Showing Pop-Up


Messages" chapter of the same book.


67
Free download pdf