Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Android Application Model I
CSE 5236: Mobile Application Development
Instructor: Adam C. Champion
Course Coordinator: Dr. Rajiv Ramnath
1
Framework Support (e.g. Android)
2
Framework Capabilities and Add-Ons
• Tailored to mobile apps
• Built-in services:
–
–
–
–
–
–
–
–
GUI
OS services (file I/O, threads, device management)
Graphics
Device access (GPS, camera, music and video players, sensors)
Web services
Networking
XML processing
Standard language libraries
• Add-ons:
– Maps
– Database support (SQLite)
– WebKit
3
IDE Support
• Eclipse, Android Studio (Beta)
• Testing tools (test management, unit
tests)
• Performance profiling tools
• SCM integration (Git, SVN, CVS)
• Software emulators
• Sensor injection (GPS, other sensors)
4
Android Studio Project Components
5
Types of Android Programs
• Applications
– Home app, Home screen
– Take over screen
• Services
– Run in the background without UI
• App widgets and home screen widgets
– View-only interface to a service
– Home screen widget if on Home screen
• All apps are composed of Activities – a “cohesive
step” in an Android application
6
Activities in
Tic-Tac-Toe
7
Specifying Activities – AndroidManifest.xml <activity android:name=".SplashScreen" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-‐filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER”/> </intent-‐filter> </activity> <activity android:name=".Login" android:label="@string/app_name" android:launchMode="singleInstance" android:screenOrientation="portrait"> <intent-‐filter> <action android:name="com.wiley.fordummies.androidsdk.Login"/> <category android:name="android.intent.category.DEFAULT"/> </intent-‐filter> </activity> 8
Implementing Activities
public class Login extends Activity implements OnClickListener { ... ... } ... public class GameSession extends Activity { ... ... 9
} Activity UI
• Widgets
• View and ViewGroup • Package android.view • Specified declaratively in layout
files
10
Sample Layout: Login Activity
<?xml version="1.0" encoding="utf-‐8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android” android:background="@color/background" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dip"> <LinearLayout android:orientation="vertical … <TextView android:text="@string/login_title” … /> <TextView … /> <EditText …"/> <TextView … /> <EditText … "/> <Button … "/> <Button … "/> <Button … "/> </LinearLayout> </ScrollView> 11
Views, ViewGroups, Layouts, Widgets
• Many types of views, layouts and widgets:
– ScrollView, HorizontalScrollView – LinearLayout, AbsoluteLayout, FrameLayout,
RelativeLayout – TextView, EditText, Button, DatePicker, Spinner
• Nested nature of layout
– ViewGroup base class for composite elements
– View base class for terminal UI components.
• Layout files “compiled” into resource R class in res
subtree.
12
Implementing UI Logic – Listener Objects
public class Login extends Activity implements OnClickListener { ... private EditText userNameEditableField; private EditText passwordEditableField; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); userNameEditableField = (EditText) findViewById(R.id.username_text); passwordEditableField = (EditText) findViewById(R.id.password_text); View btnLogin = (Button) findViewById(R.id.login_button); btnLogin.setOnClickListener(this); View btnCancel = (Button) findViewById(R.id.cancel_button); btnCancel.setOnClickListener(this); View btnNewUser = (Button) findViewById(R.id.new_user_button); btnNewUser.setOnClickListener(this); } ... } 13
The OnClick Handler
public void onClick(View v) { switch (v.getId()) { case R.id.login_button: checkLogin(); break; case R.id.cancel_button: finish(); break; case R.id.new_user_button: startActivity(new Intent(this, Account.class)); break; } } 14
Embedding a View – GameSession
Activity
public class GameSession extends Activity { ... } <?xml version="1.0" encoding="utf-‐8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/
res/android” …"> <LinearLayout ... <com.wiley.fordummies.androidsdk.Board android:id="@+id/board" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> <TextView ... "/> <TextView ... "/> </LinearLayout> 15
Embedding a View – Board Class
public class Board extends View { ... } public boolean onTouchEvent(MotionEvent event) { ... if(game.state==STATE.END) return false; ... case MotionEvent.ACTION_DOWN: ... // calculate which square was clicked ... // Attempt to make a legal move in the game game.onTouch(posX, posY); break; } return super.onTouchEvent(event); } 16
Handling UI in the Activity
• Activity also a View
• Can handle UI without any widgets. Why?
– Handle non-widget-specific events (touch)
– Handle user interaction outside the boundaries of
any UI components
– See OnTouchEvent method in SplashScreen
class.
17
Menus (Option menus) and Action Bars
• Declare the menu items
• Define onCreateOptionsMenu() and/or the
onCreateContextMenu() callback methods
in Activity. Automatically called to create the
menu (what if it doesn’t exist?).
• Implement onOptionsItemSelected()
and/or onContextItemSelected() in
activity.
18
Menu Layout File
<?xml version="1.0" encoding="utf-‐8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Settings" android:id="@+id/menu_settings" android:icon="@android:drawable/ic_menu_preferences" /> <item android:title="Help" android:id="@+id/menu_help" android:icon="@android:drawable/ic_menu_info_details" /> <item android:title="Exit" android:id="@+id/menu_exit" android:icon="@android:drawable/ic_menu_close_clear_cancel" /> <item android:title="Contacts" android:id="@+id/menu_contacts" android:icon="@android:drawable/ic_menu_view" /> </menu> Action Bar: Declare menu item with additional attribute android:showAsAction ="ifRoom", "never", "withText" or "always".
19
Menu Creation
public class GameOptions extends Activity implements OnClickListener { ... public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } ... } 20
Menu Handlers
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_settings: startActivity(new Intent(this, Settings.class)); return true; case R.id.menu_help: startActivity(new Intent(this, Help.class)); return true; case R.id.menu_exit: quitApplication(); return true; } return false; } 21
UI for Larger Screens - Fragments
• In Android 3.0 and up with compatibility library
(ACL) for earlier versions
• Further decouples UI interactions from activity
lifecycle
– Standard concept in frameworks
• Allows reuse of UI components
• Specialized activity class – FragmentActivity • We will cover it in a later class
22
Example Use
of Fragments
23
Special Types of Activities – Preferences (1)
public class Settings extends PreferenceActivity { ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction(). replace(android.R.id.content, new SettingsFragment()).commit(); } ... public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } } 24
Special Types of Activities – Preferences (2)
... public static String getName(Context context) { return PreferenceManager.getDefaultSharedPreferences(context) .getString(OPT_NAME, OPT_NAME_DEF); } public static boolean doesHumanPlayFirst(Context context) { return PreferenceManager.getDefaultSharedPreferences(context) .getBoolean(OPT_PLAY_FIRST, OPT_PLAY_FIRST_DEF); } } 25
Layout File for a Preferences Activity
<?xml version="1.0" encoding="utf-‐8"?> <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/
android android:title="Settings" android:background="@color/background"> <EditTextPreference android:key="name" android:title="Player Info" android:summary="Select your name" android:defaultValue="Player 1"/> <CheckBoxPreference android:key="human_starts" android:title="Human Plays First" android:summary="Check box to play first" android:defaultValue="true" /> </PreferenceScreen> 26
Thank You
Questions and comments?
27