• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
21 android2 updated
 

21 android2 updated

on

  • 3,731 views

Presentation given by Dr Nathan Amanquah at Ashesi during GhanaGTUG Android warm-up event

Presentation given by Dr Nathan Amanquah at Ashesi during GhanaGTUG Android warm-up event

Statistics

Views

Total Views
3,731
Views on SlideShare
3,731
Embed Views
0

Actions

Likes
1
Downloads
307
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • @+id/foo means you are creating an id named foo in the namespace of your application. You can refer to it using @id/foo. @android:id/foo means you are referring to an id defined in the android namespace. This namespace is the namespace of the framework The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).
  • Place radio group inside say a LinearLayout

21 android2 updated 21 android2 updated Presentation Transcript

  • Android N Amanquah
  • Installation:
    • Install jdk
    • Install android sdk starter pack
      • (this also installs sdk tools)
    • Install platform tools
      • (eg unzip platform-tools_r03-windows, rename to “platform tools”, directly under android-sdk/)
    • Install sdkPlatform
      • (eg unzip android-2.2_r02-windows into “platforms”)
    • Create an AVD using android sdk and avd mgr
    • Netbeans: Install nbandriod into netbeans
    • Eclipse: install ADT
    • add SDK's tools/ and platform-tools to your PATH
    • http://developer.android.com/sdk/installing.html
  • Steps
    • Design interface
      • Code it
      • Use xml
    • Create objects in code
    • Provide implementation /events in code
  • Building Blocks
    • There are four building blocks to an Android application:
    • Activity - composed of Views, responds to events
    • Intent Receiver
    • Service – long lived running code eg media plyr
    • Content Provider eg store in SQLite db
    • AndroidManifest.xml.
      • declare the components of your application
      • capabilities and requirements of your components
  • Intents
    • Intent
      • what an application wants done- a request to do smth.
      • Use Intent to move from screen to screen
      • Two main components of intent data structure: the action and the data to act upon
      • Typical values for action are MAIN (entry point: can be started from icon), VIEW, PICK, EDIT, etc. The data is expressed as a Uniform Resource Indicator (URI)
      • Eg to view a website:
        • new Intent(android.content.Intent. VIEW_ACTION , ContentURI. create ("http://www.ashesi.edu.gh"));
  • Intent Filters
    • Intent Filter
      • a description of what intents an activity (or intent receiver) is capable of handling
      • Activities publish their IntentFilters in the AndroidManifest.xml file.
    • Navigating from screen to screen is accomplished by resolving intents.
    • To navigate forward, an activity calls startActivity(myIntent).  systems looks for matching intent filters
    • Advantages:
      • reuse of existing functionality
      • Activities can be replaced/subsituted.
  • Intent Receivers
    • execute in reaction to an external event
    • App need not be running (eg to a phone call)
    • Apps can broadcast intents
      • Context.broadcastIntent().
  • Hello world: programmatically
    • package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         TextView tv = new TextView(this);        tv.setText("Hello, Android");        setContentView(tv);    } }
    • Note: copying and pasting this code results in additional invisible characters
    • Past in Notepad first
  • Hello world: by XML
    • package com.example.helloandroid;
    • import android.app.Activity;
    • import android.os.Bundle;
    • public class HelloAndroid extends Activity {
    • /** Called when the activity is first created. */
    • @Override
    • public void onCreate(Bundle savedInstanceState) {
    • super.onCreate(savedInstanceState); setContentView(R.layout.main);
    • }
    • }
    Configure the xml file to indicate what is loaded in the activity
  • XML Layout- a hierarchy of views
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;>
      • <TextView
        • android:layout_width=&quot;wrap_content&quot;
        • android:layout_height=&quot;wrap_content“
        • android:text=&quot;Hello World&quot;/>
    • </LinearLayout>
    • View class is base for all widgets
    • ViewGroup
  • Views
    • views in a window are arranged in a single tree
    • can add views from code or from tree of views in XML layout files
    • After creating view, do some of the ff
      • Set properties eg setText
      • SetFocus
      • Set up listeners
    • Set visibility
  • View Hierarchy- view w button
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    • android:layout_width=&quot;fill_parent&quot;
    • android:layout_height=&quot;fill_parent&quot;
    • android:orientation=&quot;vertical&quot; >
    • <TextView android:id=&quot;@+id/text&quot;
    • android:layout_width=&quot;wrap_content&quot;
    • android:layout_height=&quot;wrap_content&quot;
    • android:text=&quot;Hello, I am a TextView&quot; />
    • <Button android:id=&quot;@+id/button&quot;
    • android:layout_width=&quot;wrap_content&quot;
    • android:layout_height=&quot;wrap_content&quot;
    • android:text=&quot;Hello, I am a Button&quot; />
    • </LinearLayout>
  • Comparison to Swing
    • Activities i-(J)Frame in Swing.
    • Views -(J)Components in Swing.
    • TextViews -(J)Labels in Swing.
    • EditTexts -(J)TextFields in Swing.
    • Buttons -(J)Buttons in Swing.
    Handling Events is similar: myView.setOnClickListener( new OnClickListener(){ ... // Swing myButton.addActionListener( new ActionListener() {...
  • XML layout
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;   android:id=&quot;@+id/textview&quot;   android:layout_width=&quot;fill_parent&quot;   android:layout_height=&quot;fill_parent&quot;   android:text=&quot;@string/hello&quot;/>
    • These refer to string.xml and also create a new id called textview which can be referred to in code.
    • A compiled object representation of the layout defined in /res/layout/main.xml is R.layout.main
    • Textview can be referred to subsequently as R.id.textview
  • Manifest
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • <manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;org.anddev.android.hello_android&quot;>
    • <application android:icon=&quot;@drawable/icon&quot;>
    • <activity android:name=&quot;.Hello_Android&quot; android:label=&quot;@string/app_name&quot;>
        • <intent-filter>
        • <action android:name=&quot;android.intent.action.MAIN&quot; />
        • <category android:name=&quot;android.intent.category.LAUNCHER&quot; />
        • </intent-filter>
        • </activity>
    • </application>
    • </manifest>
  • References & resources in code
    • '@' prefix to introduce a resource reference
    • android:textColor=&quot;@color/opaque_red&quot;
    • (will be in color.xml)
    • R.java is an auto-generated
    • Xml: android:id=&quot;@+id/my_button“
    • In code:
      • Button myButton = (Button) findViewById ( R.id.my_button );
  • A button (View) and event
    • Views may have an integer id associated with them. –assigned in xml file
    • Eg: define btn and assign an id:
    • <Button      android:id=&quot;@+id/my_button&quot;      android:layout_width=&quot;wrap_content&quot;      android:layout_height=&quot;wrap_content&quot;      android:text=&quot;@string/my_button_text&quot;/>
    • In the onCreate method of activity, find the button:
      • Button myButton = (Button) findViewById(R.id.my_button);
    • Event
    • button.setOnClickListener(new OnClickListener() {     public void onClick(View v) {         // Perform action on clicks         Toast.makeText(HelloFormStuff.this, “Msg displayed in msgbox&quot;, Toast.LENGTH_SHORT).show();     } });
  • Create a form by adding widgets
    • Create a basic linear view eg
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;     android:orientation=&quot;vertical&quot;     android:layout_width=&quot;fill_parent&quot;     android:layout_height=&quot;fill_parent&quot; > </LinearLayout>
    • Add widgets to this layout.
    • Add their corresponding events if any, to onCreate()
  • Radio Button(use radioGroup)
    •   <RadioGroup       android:layout_width=&quot;fill_parent&quot;       android:layout_height=&quot;wrap_content&quot;       android:orientation=&quot;vertical&quot;>       <RadioButton android:id=&quot;@+id/radio_red&quot;           android:layout_width=&quot;wrap_content&quot;           android:layout_height=&quot;wrap_content&quot;           android:text=&quot;Red&quot; />       <RadioButton android:id=&quot;@+id/radio_blue&quot;           android:layout_width=&quot;wrap_content&quot;           android:layout_height=&quot;wrap_content&quot;           android:text=&quot;Blue&quot; />     </RadioGroup>
    • To do something when each RadioButton is selected, you need an View.OnClickListener.
  • Acting when clicked:
    • private OnClickListener radio_listener = new OnClickListener() {     public void onClick(View v) {         // Perform action on clicks         RadioButton rb = (RadioButton) v;         Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();     } };
    • Add ff to onCreate() method:
    •   final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);   final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);   radio_red.setOnClickListener(radio_listener);   radio_blue.setOnClickListener(radio_listener);
  • Layouts
  • Excercise
    • Create a range of layouts. Test them
    • See http://developer.android.com/resources/tutorials/views/index.html