app basic

13
Android App Basics

Upload: training-guide

Post on 19-May-2015

281 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: App basic

Android App Basics

Page 2: App basic

l A First Example: Advent Devotions

Page 3: App basic

l UML Class Diagram

Page 4: App basic

l Two Activities in Advent Devotions

l AdventDevos displays the calendar of dates

l Devo displays a single devotion

• Intent myIntent = new Intent(AdventDevos.this, Devo.class);• myIntent.putExtra("ButtonNum", "" + index); • startActivity(myIntent);

Page 5: App basic

l Two Activities in Advent Devotions

l AdventDevos displays the calendar of dates

l Devo displays a single devotion

• Bundle extras = getIntent().getExtras();• String value = extras.getString("ButtonNum");• Integer buttonNum = Integer.valueOf(value);

Page 6: App basic

l Launching an Intent you didn’t write

l Devos has button to URL

l Browser launched

l Intent i = new Intent(Intent.ACTION_VIEW, • Uri.parse("http://www.biblegateway.com/passage/?search="+ • passage +"&version=NIV"));• startActivity(i);

Page 7: App basic

l Android Activity

l “An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).”

• http://developer.android.com/reference/android/app/• Activity.html#ActivityLifecycle

Page 8: App basic

l AndroidManifest.xml

• <?xml version="1.0" encoding="utf-8"?>• <manifest xmlns:android="http://schemas.android.com/apk/res/android"• package="com.simexusa.adventdevotions"• android:versionCode="2"• android:versionName="1.0">• <application android:icon="@drawable/star" android:label="@string/app_name" android:debuggable="false">• <activity android:name=".AdventDevos"• android:label="@string/app_name">• <intent-filter>• <action android:name="android.intent.action.MAIN" />• <category android:name="android.intent.category.LAUNCHER" />• </intent-filter>• </activity>• <activity android:name=".Devo"/>• </application>• <uses-sdk android:minSdkVersion="3" />• <uses-permission android:name="android.permission.INTERNET" />• </manifest>

• Specifies icon for launching app

• Specifies icon for launching app

• Specifies activity to be launched at startup

• Each upload to Market requires versionCode increment

• Security permissions requested from user on install

Page 9: App basic

l Look around the files

Page 10: App basic

l Layouts and Resources

l See main.xml and devo.xmll Activity associates with layout xml file using

setContentView(R.layout.main); or setContentView(R.layout.devo); in onCreate()

l Note TableLayout and TableRow similar to <table> and <tr> in html

l Note use of dimen (see values/dimens.xml) and color (see values/colors.xml)

l Also see strings.xml

Page 11: App basic

l main.xml

l <?xml version="1.0" encoding="utf-8"?>l <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"l android:orientation="vertical" android:layout_width="fill_parent"l android:layout_height="fill_parent" android:background="@color/background">l <TableLayout android:layout_width="wrap_content"l android:id="@+id/TableLayout01" android:layout_height="wrap_content">l <TableRow android:paddingTop="8px">l <Button android:text="Nov. 29" android:id="@+id/Button01"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:textSize="@dimen/button_width"></Button>l <Button android:text="Nov. 30" android:id="@+id/Button02"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:textSize="@dimen/button_width"></Button>l <Button android:text="Dec. 1" android:id="@+id/Button03"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:textSize="@dimen/button_width"></Button>l <Button android:text="Dec. 2" android:id="@+id/Button04"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:textSize="@dimen/button_width"></Button>l </TableRow> …

Page 12: App basic

l devo.xmll <?xml version="1.0" encoding="utf-8"?>l <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"l android:orientation="vertical" android:layout_width="fill_parent"l android:layout_height="fill_parent" android:gravity="center_horizontal"l android:background="@color/background">l <TextView android:text="Date" android:id="@+id/Date"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:gravity="center_horizontal" android:textStyle="italic"l android:textSize="@dimen/reference_width" android:typeface="serif"l android:textColor="@color/text"></TextView>l <TextView android:text="Title" android:id="@+id/Title"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:gravity="center_horizontal" android:textStyle="bold"l android:textSize="@dimen/reference_width" android:typeface="serif"l android:textColor="@color/text"></TextView>l <Button android:text="Read Scripture" android:id="@+id/ButtonScripture"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:gravity="center_horizontal" android:textSize="@dimen/reference_width"></Button>l <ScrollView android:id="@+id/ScrollView01"l android:layout_height="fill_parent" android:layout_width="fill_parent">l <TextView android:text="Body" android:id="@+id/Body"l android:layout_width="wrap_content" android:layout_height="wrap_content"l android:gravity="left" android:textSize="@dimen/reference_width"l android:typeface="serif" android:textColor="@color/text"></TextView>l </ScrollView>• </LinearLayout>

Page 13: App basic

l dimens.xmll <?xml version="1.0" encoding="utf-8"?>l <resources>l <dimen name="button_width">17sp</dimen>l <dimen name="reference_width">20sp</dimen>l </resources>

l <?xml version="1.0" encoding="utf-8"?>l <resources>l <string name="hello">Hello World, AdventDevos!</string>l <string name="app_name">Advent Devotions</string>l </resources>

l <?xml version="1.0" encoding="utf-8"?>l <resources>l <color name="background">#AAFFFF99</color>l <color name="text">#FF000000</color>l </resources>

• colors.xml

• strings.xml