Android Demo, Objective-C




CS175

Chris Pollett

Sep 3, 2014

Outline

Finish Android Demo

An Android Project

  • To make an Android project in Eclipse, we went to File - New - Android Project and filled out the form asking for name of the project, target version of Android, etc.
  • In the left hand pane of Eclipse, the file tree for our project looks like:
    Image of folders of file tree of our first project
  • The src folder should contain our Java files, gen folder is auto-generated for us based on id's in the xml files of our project, Android 4.4W are the jar files for the version of Android we are using, the assets folder will be things like images, sound files, etc (don't have yet), the bin folder will contain the compiled version of our project, the res folder has layouts, icons, string, etc used in the project, and finally we have three text files AndroidManifest.xml, proguard.cfg, and project.properties.
  • Looking in the res folder, we can see three sub-folders: drawable-hdpi, drawable-mdpi, drawable-ldpi. Each has in it an icon ic-launcher.png. These are 72px x72px, 36px x 36px, and 16px x 16px.
  • The Graphic Layout tool

    AndroidManifest.xml

    Running App

    AVD Screenshot of Hello World

    The Basics of an Objective-C Program

    Implementation Files

    To implement the functions you use an implementation file and the syntax:

    @implementation classname
    +classMethod {
        // implementation
    }
    -instanceMethod {
        // implementation
    }
    @end
    

    Invoking Methods

    Example Objective-C Program

    // Interface File
    #import <objc/Object.h>
    
    @interface MyHello: Object {
       int myNumber;
       int myOtherNumber;
    }
    -(void)setNumber:(int) aNumber other:(int) bNumber;
    -(void)sayHello;
    @end
    

    Example Program cont'd

    // implementation file
    #import <stdio.h>
    #import "MyHello.h" 
    @implementation MyHello
    -(void)setNumber:(int)aNumber other:(int)bNumber {
        myNumber = aNumber;
        myOtherNumber = bNumber;
    } 
    -(void) sayHello {
       printf("Hello! %d\n", myNumber + myOtherNumber);
    } 
    -(id) init { //self is like this in Java
        self = (id)[super init];
        if (self) { 
            myNumber = 0;
            myOtherNumber = 0; 
        }
        return self;
    }
    @end
    

    Objective-C Example Last Part.

    #import "MyHello.h"
    int main(void) {
       MyHello *hello = [[MyHello alloc] init];
       //set the number to echo
       [hello setNumber:10 other:5];
       [hello sayHello];
       return 0;
    }
    
    /* To compile in gcc could type:
    gcc -arch i386 -x objective-c -Wno-import main.m MyHello.m -lobjc  
    
    To get this to work on 64 bit architecture/llvm you need to switch to NSObject (
    have at least GNUStep available on non-Macs)...
    You can actually build XCode projects from the command line:
    /Developer/usr/bin/xcodebuild -target project
    */
    

    The point of the above is to see the basics of Objective-C without reference to iOS or OSX, next week we'll go back to looking at the language in the specific context of Apple.