45
Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Embed Size (px)

Citation preview

Page 1: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Favorite Twitter® Searches AppAndroid How to Program

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 2: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 3: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 4: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 5: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 6: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 7: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 8: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 9: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

A ScrollView can containother views (like a layout)and lets user scroll throughcontent too large todisplay on the screen.

Technologies Overview

Page 10: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Technologies Overview• SharedPreferences▫Used to manipulate a file of key/value pairs, which stores

tags and Twitter search queries▫Going to read in the saved searches in refreshButtons

method called from Activity’s OnCreate method Acceptable because file is small

• Intents▫Typically used to launch activities▫ Indicate an action to be performed and the data on which

it is to be performed▫When button is touched, create a URL containing Twitter

query, load into browser by creating new Intent, then pass Intent to startActivity method to launch browser.

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 11: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Technologies Overview• LayoutInflater▫Programmatically creates GUI components

In out app, each new search, adds another row. •AlertDialog▫Used to display a message to the user

In case one of the EditText’s is empty or to confirm Clear▫While the dialog is displayed, user cannot interact with

app (modal dialog)•AndroidManifest.xml▫Created for you, when you create the app▫Add a new setting to the manifest to prevent the soft

keyboard from displaying when the app first loads.

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 12: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Building the App’s GUI and Resource Files

•Create project•Create Resource Files▫colors.xml, dimen.xml, strings.xml

•Delete and Recreate main.xml•Create GUI: Add TableLayout and Components•Review Key features in main.xml•Create another layout XML file ▫Will define a TableRow that will be programmatically

inflated to create each search Button and corresponding EditButton

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 13: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Creating Resource Files•Right click project name•Select New > Other•Select Android XML file from Android node in the

New dialog•Enter name of the xml file•Under “What type of resource would you like to

create?”, select Values•Click Finish

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 14: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 15: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

User’s search queries areappended to this URL, beforeahe twitter search is displayed.

Page 16: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

From R.color classCan find list online

Stretch horizontallyto fill layout’s width

Remember some of the attributes must be specified directly in the XML.

Enables you to configure options for the current input method, here keyboard will have a a next button. Will change focus to next component that can accept input – tagEditText.

Page 17: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Button for saving a search.

Here keyboard will have a a done button - will hidethe soft keyboard.

Page 18: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 19: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Search buttons will be displayedprogramatically in ScrollView

Page 20: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 21: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

•Create another layout XML file▫Right click layout folder and select New > Other▫In Android node, select Android XML file and click Next▫Enter new_tag_view.xml▫Under “What type of resource…?” select Layout radio

button.▫Select the root element for the new layout – Choose

TableRow.▫Click Finish.▫In Graphical Layout, select Android version and Device

Configuration▫Add two buttons to the layout and configure them.

Text property will be configured programmatically.

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 22: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

new_tag_view.xml©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

So parent’s colorshows through.

Page 23: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Building the App•Package and import statements•Write main (and only) Activity class for app•Override OnCreate•Write programmer defined refreshButton, makeTag,

makeTagGUI, clearButtons methods•Write 4 Inner Classes to implement OnClickListener

for▫ saveButton▫ clearTagsButton▫newTagsButton▫editButton

•Edit AndroidManifest.xml file

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 24: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

The Java Code©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Sort tags.

Access Android services.

Used to manipulate key/value Pairs in files associated with app.

Convert an Internet URL intoproper format.

Deals with soft keyboard.

Dynamically inflate layout

Page 25: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Declare instance variables.

Page 26: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.Read tag/query pairs from “searches” file.MODE_PRIVATE specifies accessibility to file: this file is ONLY accessible to this app.

Page 27: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Get more references.

Call programmer defined method.

Page 28: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Call programmer defined method.

Page 29: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Or modifies an existing query

Page 30: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Android provides a service that enableyou to inflate a layout. To obtain thisservice, you obtain a reference to it.

Page 31: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 32: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 33: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 34: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Argument to the Builder constructor is the context in which the dialog sill be displayed. Must be fully qualified, because we are calling it from within an anonymous inner class.

Page 35: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

to confirm all stored searches to be removed

Define positive button AND its eventhandler

Page 36: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Dialog is cancellable; user cantouch back buttonto dismiss dialog

Page 37: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.Event handler, registered for Each newTagButton as it is created

Gets text of the button clicked

Gets corresponding search query

arg1 – constant to display dataarg2 – uniform resource id todata on which we want to performthe action

Page 38: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 39: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Android Manifest File•When you create a project for an Android app in

Eclipse, the ADT Plugin creates and configures AndroidManifest.xml file▫Describes information about the app▫The manifest element is the root element

package attribute versionCode and versionName attributes icon attribute label attribute (name of the app) uses-sdk specifies target SDK and min SDK version WITHIN application element is the activity element

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 40: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 41: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Activity element•Activity elements are contained within application

element• If the app has more than one activity, each will have

its own activity element•Name attribute – specifies Activity’s class name, if

preceded by a dot (.), class name is auto appended to a package name specified in manifest element

•windowSoftInputMode here is specified NOT to show the soft keyboard when activity is launched▫Can edit xml directly or open the manifest editor by

double-clicking on the AndroidManifest.xml file

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 42: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 43: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Activity element cont.•Within activity element is the intent-filter element▫Specifies types of intents the Activity can respond to▫The intent-filter element must contain one or more

action elements•This contains just one action MAIN and its category

is LAUNCHER▫This indicates that this activity should be listed in the

application launcher with other apps on the device

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 44: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Wrap-up• Introduced ScrollView (ViewGroup) and Button• Learned how to create resource files using New

Android XML File dialog▫color.xml, dimen.xml and new_tag_view.xml

• Learned how to create GUI components dynamically by inflatin an XML layout

• Learned how to store key/value pairs in SharedPreferences and manipulate them

• Learned how to programmatically hide the soft keyboard

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 45: Favorite Twitter® Searches App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Wrap-up• Learned how to load a URI into a device’s web

browser by creating a new Intent and passing it to Context’s startActivity method

• Learned how to use AlertDialog.Builder to configure and create AlertDialogs for displaying message to the user

• Learned how to use Arrays to sort and search • Learned a bit about the AndroidManifest file and

how to configure it

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.