Mobile Application Develpment: Android Activities
Mobile Application Develpment: Android Activities
Develpment
Android Activities
Top-down design
Let's start from a design of an app that we want to create and
then learn the necessary skills to build that app.
● "Bigger Number" game
– user is shown two numbers
– must choose which one is bigger by
clicking on the appropriate button
– game pops up brief "correct" / "incorrect"
message after each guess
– get points for each correct answer
(lose points for incorrect answers)
Creating a new project
Designing a user interface
open XML file for your layout (e.g. activity_main.xml)
● drag widgets from left Palette to the preview image
● set their properties in lower-right Properties panel
Events
/*
* Called when the player clicks the right number button.
*/
public void clickButton2(View view) {
check(num2, num1);
}
/*
* Updates the player's score based on whether they guessed correctly.
* Also shows a 'toast' which is a brief popup message.
*/
private void check(int a, int b) {
if (a > b) {
points++;
Toast.makeText(this, "Correct!",
Toast.LENGTH_SHORT).show();
} else {
points--;
Toast.makeText(this, "You are Wrong.",
Toast.LENGTH_SHORT).show();
}
/*
* This method is called by Android when our activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll(); // <-- we added this line to set initial button random numbers
}
}
Displaying Toasts
Toast.makeText(this, "message", duration).show();