Android App Basics Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
A First Example: Advent Devotions
UML Class Diagram Generated by Android External Activity
Two Activities in Advent Devotions AdventDevos displays the calendar of dates Devo displays a single devotion Intent myIntent = new Intent(AdventDevos.this, Devo.class); myIntent.putExtra("ButtonNum", "" + index); startActivity(myIntent);
Two Activities in Advent Devotions AdventDevos displays the calendar of dates Devo displays a single devotion Bundle extras = getIntent().getExtras(); String value = extras.getString("ButtonNum"); Integer buttonNum = Integer.valueOf(value);
Launching an Intent you didn’t write Devos has button to URL Browser launched Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(" passage +"&version=NIV")); startActivity(i);
Android Activity “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).” Activity.html#ActivityLifecycle
AndroidManifest.xml <manifest xmlns:android=" package="com.simexusa.adventdevotions" android:versionCode="2" android:versionName="1.0"> <activity android:name=".AdventDevos" 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
Look around the files
Layouts and Resources See main.xml and devo.xml –Activity associates with layout xml file using setContentView(R.layout.main); or setContentView(R.layout.devo); in onCreate() –Note TableLayout and TableRow similar to and in html –Note use of dimen (see values/dimens.xml) and color (see values/colors.xml) –Also see strings.xml
main.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="Nov. 29" android:layout_width="wrap_content" android:layout_height="wrap_content" <Button android:text="Nov. 30" android:layout_width="wrap_content" android:layout_height="wrap_content" <Button android:text="Dec. 1" android:layout_width="wrap_content" android:layout_height="wrap_content" <Button android:text="Dec. 2" android:layout_width="wrap_content" android:layout_height="wrap_content" …
devo.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" <TextView android:text="Date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="italic" android:typeface="serif" <TextView android:text="Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="bold" android:typeface="serif" <Button android:text="Read Scripture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" <ScrollView android:layout_height="fill_parent" android:layout_width="fill_parent"> <TextView android:text="Body" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:typeface="serif"
dimens.xml 17sp 20sp Hello World, AdventDevos! Advent Devotions #AAFFFF99 #FF colors.xml strings.xml