intro to android app development a hobbyist's perspective

37
 Intro to Android App Development A Hobbyist's Perspective presented by Steve Wiley Compare iPhone vs Android Getting started with Android SimpleCalc walk through

Upload: peterbuck

Post on 05-Dec-2014

3.978 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Intro to Android App Development A Hobbyist's Perspective

  

Intro to Android App DevelopmentA Hobbyist's Perspective

presented by Steve Wiley

● Compare iPhone vs Android● Getting started with Android● SimpleCalc walk through

Page 2: Intro to Android App Development A Hobbyist's Perspective

  

Language Comparison

● iPhone   Objective­C→

● Android   Java→

Objective­C JavaHeader files No header files

Object reference counting Automatic garbage collection

Class extension None

Messages to objects with [] Dot notation

Page 3: Intro to Android App Development A Hobbyist's Perspective

  

Language Comparion

Objective­C

[instance setWidth:5andHeight:[tgt getHeight]];

Java

instance.setWidthAndHeight(    5, tgt.getHeight()    );

Page 4: Intro to Android App Development A Hobbyist's Perspective

  

IDE Comparison

● iPhone   XCode→

– Intellisense

– Integrated debugging● Often force to resort to ”printf” debugging due to ”out 

of scope” errors.

● Android   Eclipse with Android Plug­in→– Very full­featured

– I've bairly scratched the surface

Page 5: Intro to Android App Development A Hobbyist's Perspective

  

Cost Comparison

iPhone 

● Mac required

● Tools   free→

● Device installation   →$100/year

● App­store submittal   free→

● Apple's take   30%→

Android

● Mac, Linux, Windows

● Tools   free→● Device installation 

 free→● App­store submittal 

 $25 one­time→● Google's take   →

30%

Page 6: Intro to Android App Development A Hobbyist's Perspective

  

Getting Started

● Install Eclipse– http://www.eclipse.org

● Install the ADT plug­in for Eclipse– http://www.android.com/sdk/index.html

● Add a browser bookmark for the documentation– <install­dir>/docs/guide/samples/index.html

Page 7: Intro to Android App Development A Hobbyist's Perspective

  

Create a New Project

● Start Eclipse● Click ”Workbench”● Window \ Android SDK and AVD Manager

‒ Available Packages‒ https://dl­ssl.google.com/android/repository/repository.xml

‒ Virtual Devices

● File \ New \ Android Project‒ Specify lowest possible SDK

Page 8: Intro to Android App Development A Hobbyist's Perspective

  

What's Created for You?

● Activityin DefaultActivity.java

● res/layout/main.xml

● res/values/strings.xml

Page 9: Intro to Android App Development A Hobbyist's Perspective

  

… and

● AndroidManifest.xml

Page 10: Intro to Android App Development A Hobbyist's Perspective

  

Simple Calc

RPN Simple Calculator● 0 – 9 keys● Add, Subtract, 

Multiply Divide● Change Sign● Backspace● Decimal Point● Exit

Page 11: Intro to Android App Development A Hobbyist's Perspective

  

First... Create the Layouts

● Portrait & Landscape‒ res/layout­port/main.xml‒ res/layout­land/main.xml

● Contents:Layout

Controls...● Common Layout Choices:

‒ AbsoluteLayout, FrameLayout, LinearLayout, RelativeLayout, TableLayout

Page 12: Intro to Android App Development A Hobbyist's Perspective

  

Nested TableLayouts

Outer Table

Inner Table

TableRows

Page 13: Intro to Android App Development A Hobbyist's Perspective

  

Orientation<FrameLayout

android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="0dip">android:orientation="vertical"<TableLayout...

<FrameLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="0dip">android:orientation="horizontal"<TableLayout...

Page 14: Intro to Android App Development A Hobbyist's Perspective

  

Results TextView<TableRow>

<TextViewandroid:id="@+id/resultsView"android:layout_span="3"android:typeface="sans"android:gravity="right|fill_vertical"android:text="@string/hello"android:clickable="false"android:cursorVisible="false"android:focusable="false"android:focusableInTouchMode="false"android:lines="1"android:layout_margin="2sp"android:background="@drawable/yellowbox"android:textColor="@color/black"android:textStyle="bold"android:textSize="40dip"/>

</TableRow>

● Use ”+id” notation to provide lookup of control in code.

● @string resources are pulled from res/values/string.xml.

● @drawable resources come out of res/drawable/*.png

● @color resources come out of res/values/colors,xml.

Page 15: Intro to Android App Development A Hobbyist's Perspective

  

Enter Button

<TableRow><Button

android:text="@string/Enter"android:id="@+id/enterButton"android:layout_span="2"android:typeface="serif"android:textStyle="bold"android:height="50dip"android:textSize="30dip"android:gravity="center"android:padding="0px"/>

<Button.../></TableRow>

● Layout_span = 2 causes the Enter button to span 2 columns.

Page 16: Intro to Android App Development A Hobbyist's Perspective

  

Next... Add Controls to Code● Declare controls within DefaultActivity class:

private Button changeSignBtn;private Button enterBtn;

● Within DefaultActivity's onCreate() method:‒ this.setContentView( R.layout.main );

for res/layout­*/main.xml

● Assign Controls using findViewById():this.changeSignBtn = (Button)   this.findViewById( R.id.changeSignButton );this.enterBtn = (Button)

this.findViewById( R.id.enterButton );

Page 17: Intro to Android App Development A Hobbyist's Perspective

  

Then... Connect Clicks to Actionsthis.changeSignBtn.setOnClickListener( new

OnClickListener() {public void onClick( View v ) {

DefaultActivity.this.indicateClick();DefaultActivity.this.toggleSignOfScratch();}

} );

this.enterBtn.setOnClickListener( newOnClickListener() {

public void onClick( View v ) {DefaultActivity.this.indicateClick();DefaultActivity.this.onEnterClick();}

} );

Page 18: Intro to Android App Development A Hobbyist's Perspective

  

At This Point

● UI is Layed Out● Controls are Connected to Actions● What next?

‒ Add preferences for: # of digits to display Keystroke feedback:

‒ Clicks (redundant)‒ Vibrations (redundant)‒ Animated Visual

Page 19: Intro to Android App Development A Hobbyist's Perspective

  

Preferences

● Create folder res/xml● Add preferences.xml

‒ Right­click on xml folder‒ New / Other... / Android XML File

Page 20: Intro to Android App Development A Hobbyist's Perspective

  

New Android XML File Dialog

● Name the file ”preferences.xml”

● Check ”Preferene”● No Qualifier needed● Finish

Page 21: Intro to Android App Development A Hobbyist's Perspective

  

Types of Preferences● CheckBoxPreference – checked/unchecked

‒ Click on Key Press (on/off)

‒ Vibrate on Key Press (on/off)

‒ Animation on Key Pres (on/off)

● RingtonePreference – sound selection

● EditTextPreference – editable text

● ListPreference – select a string from a list

● DialogPreference – customization

‒ # of Digits to Display‒ SeekBar from http://android.hlidskialf.com/blog/code/android­seekbar­preference

Page 22: Intro to Android App Development A Hobbyist's Perspective

  

Edit preferences.xml<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<CheckBoxPreferenceandroid:key="AudibleKeyStrokes"android:title="@string/AudibleKeyStrokes"android:defaultValue="true"/>

<CheckBoxPreferenceandroid:key="VibrationOnKeyStroke"android:title="@string/VibrationOnKeyStroke"android:defaultValue="false"/>

<CheckBoxPreferenceandroid:key="AnimateKeyStrokes"android:title="@string/AnimateKeyStrokes"android:defaultValue="false"/>

<com.boisecodecamp.wiley.steve.SeekBarPreferenceandroid:key="digits"android:title="Fraction digits"android:defaultValue="2"android:summary="Number of digits to display = %?"android:dialogMessage="Fraction digits"android:text=" digits"android:max="8"/>

</PreferenceScreen>

Page 23: Intro to Android App Development A Hobbyist's Perspective

  

Create Preferences Activity

● Right­click package

● New / Class...

● Set Superclass to:android.preference.PreferenceActivity

● Add following in onCreate() method:this.addPreferencesFromResource(

R.xml.preferences );

● ”preferences” is from the name of the xml file created earlier

Page 24: Intro to Android App Development A Hobbyist's Perspective

  

Binding Preferences

● Override onCreateOptionsMenu()@Overridepublic boolean onCreateOptionsMenu( Menu menu ) {

menu.add( R.string.DecimalDigitsSetting ).setIcon( android.R.drawable.ic_menu_preferences );

return super.onCreateOptionsMenu( menu );}

● Override onOptionsItemSelected()@Overridepublic boolean onOptionsItemSelected( MenuItem item ) {

super.onOptionsItemSelected( item );Intent optionsItent = new Intent().setClass( this,

SimpleCalcPreferenceActivity.class );this.startActivityForResult( optionsItent, 0 );return true;

}

Page 25: Intro to Android App Development A Hobbyist's Perspective

  

Binding Preferences...

● Override onActivityResult()@Overrideprotected void onActivityResult( int requestCode, 

int resultCode, Intent data ) {super.onActivityResult( requestCode, resultCode, data );this.setPreferences();

}

Page 26: Intro to Android App Development A Hobbyist's Perspective

  

Binding Preferences...private void setPreferences() {

SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences( this );

this.stackManager.setVibrate( sharedPref.getBoolean("VibrationOnKeyStroke", false ) );

this.stackManager.setBeep( sharedPref.getBoolean( "AudibleKeyStrokes", true ) );

this.stackManager.setAnimateKeystrokes( sharedPref.getBoolean("AnimateKeyStrokes", false ) );

this.stackManager.setDigits( sharedPref.getInt( "digits", 2 ) );

}

Page 27: Intro to Android App Development A Hobbyist's Perspective

  

Adding Menus

Override two methods:● onCreateOptionsMenu – invoked when the users presses the Menu 

button.

‒ Create menu items.● OnOptionsItemSelected – invoked when the user selects a 

MenuItem.

‒ Perform the desired action.

Page 28: Intro to Android App Development A Hobbyist's Perspective

  

For Example, Settings Menu@Overridepublic boolean onCreateOptionsMenu( Menu menu ) {

menu.add( R.string.DecimalDigitsSetting ).setIcon( android.R.drawable.ic_menu_preferences );

return super.onCreateOptionsMenu( menu );}

@Overridepublic boolean onOptionsItemSelected( MenuItem item ) {

super.onOptionsItemSelected( item );Intent optionsIntent = new Intent().setClass( 

this, SimpleCalcPreferenceActivity.class );this.startActivityForResult( optionsIntent, 0 );return true;

}

Page 29: Intro to Android App Development A Hobbyist's Perspective

  

Maintaining State

Android moves Activites through states.

Changing orientation will cause a Stop/Create cycle.

Page 30: Intro to Android App Development A Hobbyist's Perspective

  

Saving State

Override at least these methods to save state:@Overrideprotected void onSaveInstanceState( Bundle outState ) {

super.onSaveInstanceState( outState );outState.putParcelable( 

DefaultActivity.stackManagerName,this.stackManager );

}

@Overrideprotected void onPause() {

super.onPause();final ObjectOutputStream out = new ObjectOutputStream( 

this.openFileOutput( SETTINGS_FILENAME,MODE_PRIVATE ) );

out.writeObject( this.stackManager );out.close();

}

Page 31: Intro to Android App Development A Hobbyist's Perspective

  

Restoring Statepublic void onCreate( Bundle savedInstanceState ) {

super.onCreate( savedInstanceState );if( savedInstanceState != null ) {

this.stackManager =savedInstanceState.getParcelable( 

DefaultActivity.stackManagerName );}if( this.stackManager == null ) {

ObjectInputStream in = new ObjectInputStream(this.openFileInput( SETTINGS_FILENAME ) );this.stackManager = (StackManager)

in.readObject();}if( this.stackManager == null ) {

this.stackManager = new StackManager();}

Page 32: Intro to Android App Development A Hobbyist's Perspective

  

Now What do We Have?

● Functional UI● Preferences● Menu to Invoke Preferences● Next:

‒ Run it in the Emulator

Page 33: Intro to Android App Development A Hobbyist's Perspective

  

Creating a Virtual Device

Within Eclipse:  Window / Android SDK and AVD Manager

● New...‒ Name‒ Target

Page 34: Intro to Android App Development A Hobbyist's Perspective

  

Start an Emulator

Can be started from Eclipse or Command Line:‒ Just be sure $PATH is set correctly

● Emulator ­avd <avd­name> ­scale 0.8

‒ Scale as needed for best results

Telnet to the Emulator to help debug.

Favorite keys:

Cntl­F11 Rotate

F6 Trackball

Page 35: Intro to Android App Development A Hobbyist's Perspective

  

Run within Emulator

● Run / Debug...● Android Application / OK● Choose Emulator / OK

Page 36: Intro to Android App Development A Hobbyist's Perspective

  

Run on a Device

● Attached the device via USB● Enable USB debugging on the device● Run or Debug...● Android Application / OK● Choose Device

Page 37: Intro to Android App Development A Hobbyist's Perspective

  

Thank You

● Questions?