Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third...

33
Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Transcript of Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third...

Page 1: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Introducing the Sudoku Example

Content taken from book:“Hello, Android” by Ed Burnette

Third Edition

Page 2: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Review of key concepts

• An activity is a user interface screen. • Applications can define one or more activities

to handle different phases of the program.• An intent is a mechanism for describing a

specific action, such as “pick a photo,” “phone home,”

Page 3: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

More key concepts

• A service is a task that runs in the background without the user’s direct interaction.

• A content provider is a set of data wrapped up in a custom API to read and write it.

• A resource is a localized text string, bitmap, or other small piece of noncode information that your program needs.

Page 4: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Safe and Secure

• Every application runs in its own Linux process.

• The hardware forbids one process from accessing another process’s

• Access to certain critical operations is restricted, and you must specifically ask for permission to use them in a file named Android-Manifest.xml

Page 5: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Most common permissions

• INTERNET: Access the Internet.• READ_CONTACTS: Read (but don’t write) the user’s

contacts data.• WRITE_CONTACTS: Write (but don’t read) the user’s

contacts data.• RECEIVE_SMS: Monitor incoming SMS (text) messages.• ACCESS_COARSE_LOCATION: Use a coarse location

provider such as cell towers or wifi.• ACCESS_FINE_LOCATION: Use a more accurate location

provider such as GPS.

Page 6: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Example

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.google.android.app.myapp" ><uses-permission

android:name="android.permission.RECEIVE_SMS" />

</manifest>

Page 7: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Sudoku Game

• You have a grid of eighty-one tiles (nine across and nine down), and you try to fill them in with numbers so that each column, each row, and each of the three-by-three boxes contains the numbers 1 through 9 only once.

• When the game starts, some of the numbers (the givens) are already filled in.

• All the player has to do is supply the rest. • A true Sudoku puzzle has only one unique solution.

Page 8: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

The Sudoku example program

Page 9: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Designing by Declaration

• User interfaces can be designed using one of two methods: procedural and declarative.

• Procedural simply means in code.• Declarative design, on the other hand, does

not involve any code.

Page 10: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Creating the Opening Screen

Page 11: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

First Activity

When you create the Sudoku project, the Androidplug-in makes a single activity for you in Sudoku.java

The call to setContentView( ) fills in the contents of the activity’s screen with an Android view widget.

Page 12: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

First Activity: declarative design

R.layout.main is a resource identifier that refers to the main.xml file in the res/layout directory

main.xml declares the user interface in XML, so that’s the file we need to modify.

Page 13: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Initial resources in the Sudoku project

Page 14: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Linear Layout

<LinearLayoutxmlns:android=http://

schemas.android.com/apk/res/android android:orientation="vertical"

android:layout_width="fill_parent"android:layout_height="fill_parent" ><!-- ... -->

</LinearLayout>

Page 15: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Some parameters in layouts

• xmlns:android="http://schemas.android.com/apk/res/android"

• Defines the XML namespace for Android

• Inside the <LinearLayout> tag you’ll find one child widget:

<TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" />

Page 16: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Modified main.xml file

The @string/resid syntax refers to strings in the res/values/strings.xml file

@+id/about_button defines the ID for the About button

Page 17: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Modified strings.xml file

Page 18: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Modifying the interface

• Let’s make the title text larger and centered, make the buttons smaller, and use a different background color.

Page 19: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Color Definition

• the color definition, which you should put in res/values/colors.xml<?xml version="1.0" encoding="utf-8"?><resources>

<color name="background">#3500ffff</color>

</resources>

Page 20: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

New layout

Page 21: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Using Alternate Resources

Let’s see what happens when we switch the emulator to landscape mode by typing

Ctrl F11(in windows)

Page 22: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Different layout for landscape modeCreate a file called res/layout-land/main.xml (note the -land suffix)

Page 23: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Implementing an About Box

• When the user selects the About button, meaning that either they touch it (if they have a touch screen) or they navigate to it with the D-pad (directional pad) or trackball and press the selection button, we want to pop up a window with some information about Sudoku.

• After scrolling through the text, the user can press the Back button to dismiss the window.

Page 24: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Possible solutions

• Define a new Activity, and start it.• Use the AlertDialog class, and show it.• Subclass Android’s Dialog class, and show that.

Page 25: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Defining new activity• res/layout/about.xml

add strings for the title of the About dialog box and the text itcontains to res/values/strings.xml

Page 26: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Formatting strings

• Note how a string resource can contain simple HTML formatting and can span multiple lines.

• The backslash character (\) in about_text prevents an extra blank from appearing before the first word.

Page 27: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

About activity

• The About activity should be defined in About.java.

• All it needs to do is override onCreate( ) and call setContentView( ).

• To create a new class in Eclipse, use File > New > Class. Specify the following:

Page 28: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

About.javaEdit the class so it looks like this:

Page 29: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

About button in the Sudoku class

In the onCreate( ) method, add code to call findViewById( ) to look up an Android view given its resource ID, and add code to call setOnClickListener() to tell Android which object to tickle when the user touches or clicks the view

Page 30: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

setOnClickListener( ) method

• The setOnClickListener( ) method needs to be passed an object that implements the OnClickListener Java interface.

• OnClickListener has one method in it called onClick( )

Page 31: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Starting activities

• To start an activity in Android, we first need to create an instance of the Intent class.

• There are two kinds of intents: public (named) intents that are registered with the system and can be called from any application and private (anonymous)

Page 32: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Activities and the Manifest

• Every activity needs to be declared in AndroidManifest.xml

<activity android:name=".About"android:label="@string/about_title"

></activity>

Page 33: Introducing the Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.

Clicking on the About button