0% found this document useful (0 votes)
30 views17 pages

Mobile Application Develpment: Android Activities

The document describes how to build a "Bigger Number" game mobile application using Android. It involves designing the user interface with widgets in XML layout files, setting event listeners on the widgets to handle click events in Java code, and updating scores and displaying messages in response to correct or incorrect guesses. The application involves generating random numbers for the buttons on each roll, and checking the player's guess by comparing the numbers to update their points and display a toast message.

Uploaded by

naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

Mobile Application Develpment: Android Activities

The document describes how to build a "Bigger Number" game mobile application using Android. It involves designing the user interface with widgets in XML layout files, setting event listeners on the widgets to handle click events in Java code, and updating scores and displaying messages in response to correct or incorrect guesses. The application involves generating random numbers for the buttons on each roll, and checking the player's guess by comparing the numbers to update their points and display a toast message.

Uploaded by

naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Mobile Application

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

● event: An external stimulus your program can respond to.


● Common kinds of events include:
– Mouse motion / tapping, Keys pressed,
– Timers expiring, Network data available

● event-driven programming: Overall


execution of your program is largely dictated by user events.
– Commonly used in graphical programs.

● To respond to events in a program, you must:


– Write methods to handle each kind of event ("listener" methods).
– Attach those methods to particular GUI widgets.
Setting an event listener
 select the widget in the Design view
 •scroll down its Properties until you find onClick
 type the name of a method you'll write to handle the click
 •switch to the Text view and find the XML for that button
 •click the "Light Bulb" and choose to "Create" the method
Event listener Java code
AndroidManifest.xml
activity_main.xml
activity_main.xml continues
MainActivity.java
import android.app.*;
import android.support.v7.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.util.*;

public class MainActivity extends Activity {


private int num1; // the numbers on the left and right buttons
private int num2;
private int points; // player's point total; initially 0
/*
* Called when the player clicks the left number button.
*/
public void clickButton1(View view) {
check(num1, num2);
}

/*
* 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();
}

TextView pointsView = (TextView) findViewById(R.id.pointsTextView);


pointsView.setText("Points: " + points);
roll();
}
/*
* Chooses new random integers to appear on the two buttons.
*/
private void roll() {
// pick two random numbers
Random r = new Random();
num1 = r.nextInt(9);
num2 = r.nextInt(9);
while (num2 == num1) {
num2 = r.nextInt(9);
}
// set the buttons to display the random numbers
Button left = (Button) findViewById(R.id.buttonLeft);
left.setText("" + num1); // "" + int -> converts int to String

Button right = (Button) findViewById(R.id.buttonRight);


right.setText("" + num2);
}
/*************************************************************************
* BELOW THIS POINT IS CODE THAT WAS GENERATED BY
ANDROID STUDIO THAT WE *
* DID NOT MODIFY, EXCEPT FOR ONE LINE THAT IS MARKED
BELOW. *
*************************************************************************/

/*
* 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();

– where duration is Toast.LENGTH_SHORT or LENGTH_LONG

● A "Toast" is a pop-up message that appears for a short time.


● Useful for displaying short updates in response to events.
● Should not be relied upon extensively for important info.
References
 Activity class
 https://developer.android.com/reference/android/app/Activity.html
#Activity

You might also like