Programming for Mobile I

Why Look at Mobile Programming in This Course?

  • A great example of a rapidly changing area
  • To get exposure to programming with more hardware constraints (although this is quickly changing as well)
  • It is another example of event-based programming

The Current State of Programming for Mobile

  • iOS
    • Programming was primarily in Objective-C until 2014
    • Apple introduced its own language to replace Objective-C called Swift in 2014
  • Android
    • Programming is primarily done in Java*
    • What we will look at in this course
  • Everything else
    • Windows - C#, C++ and related langauges
    • Blackberry - C, C++ traditionally, have released one Android phone so far...

Android

  • Android is an open source operating system whose development is shepareded by Google
  • The core of the operating system is based on Linux (C and C++)
  • The primary language for development is Java
    • Android also allows code to be written in other languages, and while it is theoretically possible to write an entire application in another language, the intent is to write at least the GUI code in Java

Resources

Java on Android

  • While programming for Android uses Java, and its syntax, there are some notable differences
  • The byte code generated during compilation is different
    • The Dalvik byte code has different instructions then standard Java byte code
    • The architecture of the virtual hardware in the virtual machine is different
  • In the most recent Android versions, the byte code is compiled to phone-dependent native code upon installation

Developing for Android

  • The Google sanctioned enviroment for creating Android applications is Android Studio
    • Free
    • Based on the IntelliJ IDE
    • Includes Android Emulators
  • Generates the file structure of an application for you

Android Program Basics

  • The GUI is defined in an XML file
  • The Java Code is used to define how interactions happen and the logic of the program
  • Sound Familiar?
    • Why use XML?

Android App Layout

  • Can be created through drag and drop in Android Studio or by coding the XML
  • We will be focusing on the XML today
  • A single screen the user sees is called an Activity in Android

Layout

  • In the simplest android applications, the top level element in the XML will be a Layout tag
    • In addition to grouping the widgets on the screen together, the Layout tag tells the application how the widget should be laid out
  • Main Layouts
    • LinearLayout
    • RelativeLayout
    • GridLayout

ID Attribute Syntax

  • Android uses special syntax in the attributes of elements to save typing
  • One of the attributes that uses some shortcuts is android:id
    • Internally the ID is a number, so by prefixing it with an @ sign, we letting the XML parser know this is a string placeholder that needs to essentially be dereferenced
    • To declare a new id, place the + after the @ sybol
    • When referencing, don't use the +
<TextView android:id="@+id/anID" />
<TextView android:layout_below="@id/anID" />

Buttons

  • There are two main types of buttons in Android
    • Button which is used for text buttons and text and image combination buttons
    • ImageButton which is used for a button consisting of only an image
    • The button's main event is onclick
  • For an image button, the src attribute indicates where in the projects res folder the image is found
<Button
                android:text="New Button"
                android:id="@+id/button"/>
<ImageButton
                android:src="@drawable/IMAGE"
            />

EditText

  • The EditText element is used for all types of text input
  • The specific type of input is set with the android:inputType attribute
  • Specifying the type of input is perhaps more important on mobile than the web
    • The user does not have a full keyboard available
    • The input type will help indicate the default type of keyboard for that input
  • By default, there are no events for EditText, but you can add a button to be displayed when the text field is opened

CheckBox

  • CheckBox is used to creat a checkbox
  • A user can select any number of checkboxes
    • In Android they are not formally grouped because of this
  • Each checkbox specifies an onClick handler, but multiple checkboxes can use the same one

    <CheckBox
          android:text="Checkbox Text"
      />
    

RadioButton

  • Radio buttons represent a question where once choice may be selected
  • Because multiple radio buttons represent a choice for the same question, they are grouped together in a RadioGroup
  • The element that holds the radio button itself is RadioButton
  • Just like CheckBox each radio button has its own onClick attribute
<RadioGroup>
    <RadioButton android:text="Radio Button 1"/>
    <RadioButton android:text="Radio Button 2"/>
</RadioGroup>

ToggleButton / Switch

  • ToggleButton ans Switch both represent something that can be on or off
  • A ToggleButton looks like a regular button, but can either be depressed(on) or not depressed(off)
  • In Android 4, Switch was introduced, which uses a slider to show both options at once, and which is currently selected
<ToggleButton
    android:textOff="OFF"
    android:textOn="ON" />

<Switch 
    android:text="Switch Label" />

Spinner (Dropdown)

  • A Spinner is the Android term for a dropdown interface, like select in HTML
  • The Android documentation recomends if you have more choices than can fit on a screen horizonitally, to use a Spinner rather than a CheckBox
  • The entries for a Spinner are defined either programatically or through a seperate XML file with the choices
    <Spinner
    android:entries="@array/ChoicesFromFile" />
    

Images

  • To display an image, the XML is almost idenctical to an ImageButton
  • The element is named ImageView
  • Once again, the location of the image it defined by the android:src attribute
<ImageView
        android:src="@drawable/ImageFile" />