Android N Amanquah
Installation: <ul><li>Install jdk </li></ul><ul><li>Install android sdk starter pack </li></ul><ul><ul><li>(this also inst...
Steps <ul><li>Design interface </li></ul><ul><ul><li>Code it </li></ul></ul><ul><ul><li>Use xml </li></ul></ul><ul><li>Cre...
Building Blocks <ul><li>There are four building blocks to an Android application:  </li></ul><ul><li>Activity - composed o...
Intents <ul><li>Intent </li></ul><ul><ul><li>what an application wants done- a request to do smth.  </li></ul></ul><ul><ul...
Intent Filters <ul><li>Intent Filter </li></ul><ul><ul><li>a description of what intents an activity (or intent receiver) ...
Intent Receivers <ul><li>execute in reaction to an external event </li></ul><ul><li>App need not be running (eg to a phone...
Hello world: programmatically <ul><li>package com.example.helloandroid; import android.app.Activity; import android.os.Bun...
Hello world: by XML <ul><li>package com.example.helloandroid; </li></ul><ul><li>import android.app.Activity; </li></ul><ul...
XML Layout- a hierarchy of views <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  </li></ul><ul><li><Li...
Views <ul><li>views in a window are arranged in a single tree  </li></ul><ul><li>can add views from code or from tree of v...
View Hierarchy- view w button <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><Linear...
Comparison to Swing <ul><li>Activities i-(J)Frame in Swing. </li></ul><ul><li>Views -(J)Components in Swing. </li></ul><ul...
XML layout <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <TextView xmlns:android=&quot;http://schemas...
Manifest <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  </li></ul><ul><li><manifest xmlns:android=&qu...
References & resources in code <ul><li>'@' prefix to introduce a resource reference </li></ul><ul><li>android:textColor=&q...
A button (View) and event <ul><li>Views may have an integer id associated with them. –assigned in xml file </li></ul><ul><...
Create a form by adding widgets <ul><li>Create a basic linear view eg </li></ul><ul><li><?xml version=&quot;1.0&quot; enco...
Radio Button(use radioGroup) <ul><li>   <RadioGroup       android:layout_width=&quot;fill_parent&quot;       android:layou...
Acting when clicked: <ul><li>private OnClickListener radio_listener = new OnClickListener() {     public void onClick(View...
Layouts
Excercise <ul><li>Create a range of layouts. Test them </li></ul><ul><li>See  http://developer.android.com/resources/tutor...
Upcoming SlideShare
Loading in...5
×

21 android2 updated

4,080

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,080
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
332
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • @+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

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

      Clipping is a handy way to collect important slides you want to go back to later.

    ×