Chapter 21pages 569 to 694 | 694 - 569 = 25 pages | 13 is the mid way point for this chapter | Current Page 594 - 569 = 25 | Comments: 0 more pages till the end | reserve
571 Android apps are Java programs that use an application framework, a set of classes and files that make the job easier as long as you follow all the rules.
You've already used a framework, Swing, to create graphical user interfaces.
The Android SDK provides a framework that lays down a set of rules for how apps must be structured to run properly on Android devices.
571 Before you can write apps, you must install and configure three things:
the Android SDK,
Android Studio,
and Android Plug-in for Eclipse.
571 Android Studio will set up the SDK and Plug-in during installation.
To get Android Studio, visit the website developer.android.com/sdk and click the Download button (or link) on this page.
The software requires 1.1GB of disk space.
571 You will be asked what components to install along with Android Studio.
Options include the Android SDK and Android Virtual Device, a simulator that acts like a phone or tablet running the mobile OS.
Install both of these components and any others the installation wizard recommends.
572 After the installation wizard downloads the Android SDK and other components, you are asked to choose a theme for the Android Studio user interface (Figure 21.1)
Figure 21.1 - Choosing the user interface theme in Android Studio - goes here
574 In the Project pane, expand folders to familiarize yourself with the files and folders that a starting project contains.
A new app such as Palindrone has these starting components:
/javafolder --- the app's Java source code that you create.
/java/com.java21days.palindrome/PalindromeActivity --- The activity class created along with the project.
/res --- Application resources, which include animation, graphics, layout files, numbers, and strings.
Subfolders layout, values, drawable, menu, and midmap hold specific resource types.
These folders contain 10 resource files: four versions of ic_launcher.png, two versions of dimens.xml, activity_palindrome.xml, menu_palindrome.xml, strings.xml, and styles.xml.
/manifests/AndroidManifest.xml --- The app's configuration file.
Figure 21.3 - Viewing the components of an Android project. -- goes here
577 To run your first app, choose the menu command Run, Run App.
The Android emulator loads in its own window, which may take a minute or more, depending on how fast your computer is.
The first thing that loads is a screen that displays an animated Android logo while the emulator continues to load the OS.
577 The emulator displays "Palindrome" in the app's title bar and one line of text on the app's screen, "Sit on a Potato Pan, Otis!," as shown in Figure 21.6.
Controls to the right of the screen let you use the emulator like a phone with a mouse.
You also can click the screen, although in this app there's nothing to click.
577 Click the Back button to close the Palindome app and try out your new fake Android phone.
Figure 21.6 - Running an app in an emulator. - goes here
578 An emulator can simulate many things, such as connecting to the Internet over the computer's current connection and receiving phone calls and SMS messages.
578 If the emulator fails when you attempt to run the app, there's an error that occurs on some Windows and Mac OS computers.
The following error message is displayed in Android Studio:
Output
Error: x86 emulation currently requires hardware acceleration:
Please ensure Intel HAXM is properly installed and usable. CPU
acceleration status: HAX kernel module is not installed
578 This error occurs on computers with an Intel processor when a program called the Intel Hardware Accelerated Execution Manager (HAXM) has not been installed.
This program can be downloaded as part of the Android SDK.
Before you go any further today, read Appendix C< "Fixing a Problem with the Android Studio Emulator."
578 Android apps can make use of SMS messaging, location-based services, touch screen input, and the rest of the device's functionality.
For a final programming project, you'll create an app that can make a phone call, visit a website with the browser, and load a location using Google Maps.
579 After closing the Palindrome project with the menu command File, Close Project, create a new project in Android Studio by following these steps:
On the quick start menu, select Start a New Android Studio Project. The Create New Project wizard appears.
In the Application Name field, enter Santa.
Accept the other defaults also and click Next. A Target Android Device pane appears.
Accept these defaults also and click Next. An activity pane appears.
Choose Blank Activity, and then click Next. You will be given a chance to customize the activity.
In the Activity Name field, enter SantaActivity.
Click Finish.
579 The project opens in the main Android Studio user interface.
579 Although Android apps are Java programs, a lot of work required to create them is done in the Android Studio interface.
You can accomplish many things in an Android app without writing Java code.
579 One thing you accomplish without programming is creating resources the app needs.
579 As shown earlier today, every new Android project begins with several folders and subfolders that contain resources.
To examine these folders, expand the Project pane, and then expand /res and all its subfolders, as shown in Figure 21.7.
579 The project's starting resources are several graphic files, strings in strings.xml, and graphical user interface layout files that also are in XML format.
Graphics must be in PNG, JPG, or GIF format.
Two additional XML files that often are added to apps are styles.xml, which defines fonts, colors, and other visuals used in the app,
and dimens.xml, which holds dimensional measurements for text and other things that can be displayed in the app.
579 The /res/mipmap folder contains four versions of the file ic_launcher.png.
This is the app's icon, the small graphic that represents it in application menus on an Android device.
Each folder is for graphics of different screen resolutions.
A new graphics file, santa.png will be brought into the project and designed as its icon in AndroidManifest.xml, the file that holds the app's configuration settings.
Figure 21.7 - Examining an app's resources. - goes here
580 CAUTION: Resource filenames can contain only lowercase letters, numbers, the underscore character _ .
Android apps identify resources using their filenames with the extension removed.
This becomes the resource's ID, which is how it will be referred to in code.
The files dragged into this project have the IDs browser, maps, northpole, phone, and santa.
No two resources can have the same ID, except for versions of the same graphics file in the four ic_launcher.png folders, which are treated as a single resource.
An app can't be compiled in Android Studio if two resources have the same ID.
581 The graphical user interface for an Android app does not use Swing, because Android has its own library of user interface widgets.
An app's graphical user interface is created as a collection of layouts, which are containers that hold text fields, buttons, graphics, and other widgets.
582 Each screen displayed to a user can have a single layout or multiple layouts.
There are layouts that organize widgets into a table, stack them vertically or horizontally, and arrange them in other ways.
582 The Santa app has a single screen with buttons to contact Santa.
582 An app could be presented as multiple screens:
A splash screen displays while the app loads.
A menu screen contains buttons to access the other screens.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
getMenuInflater().inflate(R.menu.menu_santa, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
587 Three intents are created in the processClick() method, each in a case section of the switch conditional:
Below the last line of the onCreate() method on line 14, add these statements:
action = new Intent(Intent.ACTION_DIAL, Uri.parse(
"tel:877-446-6723"));
action = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://www.noradsanta.org"));
action = new Intent(Intent.ACTION_VIEW, Uri.parse(
"geo:0,0?q=101 Saint Nicholas Dr., North Pole, AK"));
587 An Intent() constructor takes two arguments:
The action to take, selected by a class variable
The data associated with the action
587 The three Intents tell the Android device to set up an outgoing phone call to Santa's NORAD hotline at 877-446-6723,
visit the website www.noradsanta.org, and load Google Maps with an address at the North Pole.
587 The startActivity(Intent) statement turns an intent into action:
public void processClicks(View display) {
Intent action = null;
int id = display.getId();
switch (id) {
case (R.id.imageButton):
action = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:877-446-6723"));
break;
case (R.id.imageButton2):
action = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.noradsanta.org"));
break;
case (R.id.imageButton3):
action = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=101 Saint Nicholas Dr., North Pole, AK"));
break;
default:
break;
}
startActivity(action);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
getMenuInflater().inflate(R.menu.menu_santa, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
There are links for each of the manufacturers of Android phones that go to their driver download pages.
591 Follow the manufacturer's instructions for how to install the driver, making sure to uninstall an older driver version if you're asked to do that first.
592 For the past three weeks, you've had a chance to work with the syntax and the core classes that make up the Java language and the Java Class Library.
You've ventured into sophisticated topics such as JDBC, Internet networking, and data structures,
and you've explored class libraries such as the XML Object Model library and Android.
592 Now you are ready to tackle the biggest challenge yet:
Turning an empty source code file into a robust and reliable program
implemented as a set of Java classes using object-oriented programming.
592 This book has an official website at www.java21days.com with answers to frequently asked questions,
source code for the entire book, error corrections, and supplementary material.
592 Now get to work on the next billion-dollar tech startup.
In your IPO, don't forget the author who was there when you taught yourself Java.
593 The following question is the kind of thing you could expect to be asked on a Java programming certification test. Answer it without looking at today's material or using the Java compiler to test the code.
The answer is available on the book's website at www.java21days.com
Given:
public class CharCase {
public static void main(String[] arguments) {
float x = 9;
float y = 5;
char c = '1';
switch (c) {
case 1;
x = x + 2;
case 2;
x = x + 3;
default;
x = x + 1;
}
System.out.println("Value of x: " + x);
}
}
594 What will be the value of x when it is displayed ?
9.0
10.0
11.0
The program will not compile.
Exercise
Top | page 594 | To extend your knowledge of the subjects covered today, try the following exercises:
Exercise solutions are offered on the book's website at www.java21days.com
Modify the Palindrome app to display a different palindrome and show a graphic as the screen's background.
Modify the Santa app with the phone number, website address, and map location of another famous person.