Quick Intro to Android Development
-
Upload
jussi-pohjolainen -
Category
Technology
-
view
16.083 -
download
3
Transcript of Quick Intro to Android Development
![Page 1: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/1.jpg)
Quick Intro to Android Development
Jussi Pohjolainen
![Page 2: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/2.jpg)
Android Studio
• Android Studio replaces Eclipse as Google’s primary IDE for native Android Development
• Features–WYSIWYG Editor– Template wizards– Support for Android Wear– Lint tools– Etc
![Page 3: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/3.jpg)
![Page 4: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/4.jpg)
![Page 5: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/5.jpg)
![Page 6: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/6.jpg)
Templatepublic class MainScreen extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_screen, menu);
return true;
}
}
Creates layout from xml-‐file
![Page 7: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/7.jpg)
Modificationimport android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main 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);
}
}
![Page 8: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/8.jpg)
Running
![Page 9: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/9.jpg)
Running
![Page 10: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/10.jpg)
UI: By Coding or XML
• You can code your UI or you can use XML – files
• Using XML – files is the preferredway
• File res/layout/foo.xmlcontains the basic template for your UI
![Page 11: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/11.jpg)
Using the XML filepackage fi.tamk;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}Name of your xml-‐
file(main.xml)
R – class. Generated for
you
Inner class
![Page 12: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/12.jpg)
XML and R -‐ class
• XML file will be compiled to a Java object• Reference to that object is made through R –class
• R – class is generated automatically by the Eclipse plugin
• R – class: gen/some.package/R.java
![Page 13: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/13.jpg)
R -‐ classpackage fi.tamk;
public final class R {public static final class attr {
}
public static final class drawable {public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;}
public static final class string {
public static final int app_name=0x7f040001;public static final int hello=0x7f040000;
}
}
• R – class is an index to all your resources
• Short way of referencing to resources
• Never edit this file by hand!
![Page 14: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/14.jpg)
XML -‐ file<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" >
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/hello_world"tools:context=".MainActivity" />
</RelativeLayout>
![Page 15: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/15.jpg)
XML – file, without Name Spacing
<?xml version="1.0" encoding="utf-8"?>
<TextView layout_width="fill_parent"
layout_height="wrap_content"
text="@string/hello_world"/>
Reference to res/values/strings.xml
![Page 16: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/16.jpg)
res/values/strings.xml<resources>
<string name="app_name">MyHelloWorld</string>
<string name="hello_world">Does this work?</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main_screen">MainScreen</string>
</resources>
![Page 17: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/17.jpg)
Result
![Page 18: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/18.jpg)
Two Widgets and Event Handlingpublic class Main extends Activity implements OnClickListener {
private Button clickMe;
private TextView textView;private LinearLayout layout;
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
clickMe = new Button(this);
clickMe.setText("Click Me!");clickMe.setOnClickListener(this);
textView = new TextView(this);textView.setText("Some Text");
layout = new LinearLayout(this);layout.addView(clickMe);
layout.addView(textView);
setContentView(layout);
}
public void onClick(View v) {
textView.setText("Clicked!");
}}
![Page 19: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/19.jpg)
Two Widgest and Event Handling via XML<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" >
<TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/hello_world"tools:context=".MainScreen" />
<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/textView1"android:layout_centerHorizontal="true"android:text="Change text" />
</RelativeLayout>
![Page 20: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/20.jpg)
![Page 21: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/21.jpg)
R – class updates automaticallypublic final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_action_search=0x7f020000;
public static final int ic_launcher=0x7f020001;
}
public static final class id {
public static final int button1=0x7f070001;
public static final int menu_settings=0x7f070002;
public static final int textView1=0x7f070000;
}
public static final class layout {
public static final int activity_main_screen=0x7f030000;
}
public static final class menu {
public static final int activity_main_screen=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
public static final int menu_settings=0x7f040002;
public static final int title_activity_main_screen=0x7f040003;
}
public static final class style {
public static final int AppTheme=0x7f050000;
}
}
![Page 22: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/22.jpg)
.. And the Codepublic class Main extends Activity implements OnClickListener {
private Button clickMe;private TextView textView;
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);
clickMe = (Button) findViewById(R.id.button1);textView = (TextView) findViewById(R.id.textView1);
clickMe.setOnClickListener(this);}
public void onClick(View v) {textView.setText("Clicked!");
}}
![Page 23: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/23.jpg)
About Delegation Event Model
• Separation between application and UI code• Example:
– somebutton.setOnClickListener(OnClickListener);
• Source: somebutton• Listener: some object that implements OnClickListener
• There is a easier way to do basic event handling..
![Page 24: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/24.jpg)
Listeners in XML<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<Button android:text="Click 1" android:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="click" />
<Button android:text="Click 2" android:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="click" />
</LinearLayout>
![Page 25: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/25.jpg)
And the Java Codepublic class EventHandlingDemo extends Activity {
/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.main);
}
public void click(View source) {switch ( source.getId() ) {
case R.id.button1:
// do something
break;
case R.id.button2:
// do something
break;}
}}
![Page 26: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/26.jpg)
LOGCAT
![Page 27: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/27.jpg)
logcat
• Collecting and viewing system debug output• Command line app– adb logcat
• Can be opened also in Eclipse– Window > Show View > Other… > Logcat
![Page 28: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/28.jpg)
Android Studio and Logcat
![Page 29: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/29.jpg)
Reading and Writing Logs
• Log is a logging class for printing stuff to logcat– Common methods:
• v(String, String) - verbose
• d(String, String) - debug
• i(String, String) - info
• w(String, String) - warning
• e(String, String) – error
– Example• Log.d(“MainScreen”, “Just printing stuff”);
• logging
![Page 30: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/30.jpg)
Logcat outputs everything
![Page 31: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/31.jpg)
Filtering Output
• Log message contains– Tag – short String, example "MainScreen"– Priority– following chars from lowest to highest priority: • v(erbose) – lowest priority
• d(ebug)• i(nfo)
• w(arning)
• e(rror)• f(atal)• s(ilent) – highest priority, nothing is printed
![Page 32: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/32.jpg)
Filtering Output
• Example output– D/MainScreen( 903): User clicked some view object: android.widget.Button@411fe148
• Priority level is D and tag is MainScreen
![Page 33: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/33.jpg)
Restricting output
• To restrict output, use filter expressions• Format– tag : priority
– priority is the minimum level of priority to report
• Example– adb logcat
– tag1:priority1 // show this and..
– tag2:priority2 // this..
– *:S // Set all other tags silent
![Page 34: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/34.jpg)
Real World Usage
• Example– $ adb logcat MainScreen:D *:S
– D/MainScreen( 903): User clicked some view object: android.widget.Button@411fe148
• Display output from MainScreen tag with priority debug or above and restrict everything else
![Page 35: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/35.jpg)
From Eclipse
![Page 36: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/36.jpg)
Good Practice
• Published app should not contain logging code• BuildConfig.DEBUG flag is here to help!• Flag is set automatically to false, when exporting your app
• How?– if(BuildConfig.DEBUG) { Log.e(…); }
![Page 37: Quick Intro to Android Development](https://reader033.fdocuments.in/reader033/viewer/2022042814/5551e0b9b4c90501638b4b3a/html5/thumbnails/37.jpg)
Create a Debug Class!package fi.tamk.tiko.pohjolainen.utilities;
class Debug {
public static void print(String tagName, String msg) {
if(BuildConfig.DEBUG) {
Log.d(tagName, msg);
}
}
}
// And in code
Debug.print("MainScreen", "Something");