Upcoming SlideShare
×

# Mobile Application Development: Lecture 12

602 views
557 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
602
On SlideShare
0
From Embeds
0
Number of Embeds
282
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Mobile Application Development: Lecture 12

1. 1. Mobile Application Development Lecture 12 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● Coding Exam 01 Solution ● Shared Preferences ● Persisting States with Shared Preferences ● Inter-Activity Communication with Shared Preferenceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
3. 3. Coding Exam 01http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
4. 4. Solution Steps ● GUI Implementation – Images – Design ● Application Compilation & Installation ● GUI Image Browsing Logic – Up Arrow Button – Down Arrow Button ● ContextMenu – Design – Registration – UI Logichttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
5. 5. GUI Design LinearLayout1 (Vertical) LinearLayout (Horizontal) btn_upper_invisible_left btn_up btn_upper_invisible_right img_view_mathematician LinearLayout (Horizontal) btn_lower_invisible_left btn_down btn_lower_invisible_righthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
6. 6. Image Browsing Logic Button btnUp = null; Button btnDown = null; ImageView mPicture = null; static String[] mBios = null; static String[] mWikiUrls = null; static int[] mImgIds = { R.drawable.bhaskaracharya_img, R.drawable.blaise_pascal_img, R.drawable.euclid_img, R.drawable.isaac_newton_img, R.drawable.muhammad_al_khwarizmi_img, R.drawable.omar_khayyam_img, R.drawable.pingala_img };http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
7. 7. Image Browsing Logic mPicture.setBackgroundDrawable(mRes.getDrawable(mImgIds[mCurId])); btnDown.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mCurId = (mCurId + 1) % 7; mPicture.setBackgroundDrawable(mRes.getDrawable(mImgIds[mCurId])); } });http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
8. 8. Image Browsing Logic btnUp.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mCurId <= 0) mCurId = 6; else mCurId -= 1; mPicture.setBackgroundDrawable(mRes.getDrawable(mImgIds[mCurId])); }});http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
10. 10. ContextMenu UI Logic @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case CONTEXT_MENU_BIO_ITEM: Toast.makeText(this, mBios[mCurId], Toast.LENGTH_LONG).show(); return true; case CONTEXT_MENU_WIKI_ITEM: Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(mWikiUrls[mCurId])); startActivity(i); return true; default: return false; }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
11. 11. ContextMenu Registration // this refers to the main activity object // mPicture is the ImageView this.registerForContextMenu(mPicture);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
13. 13. Data Persistence Techniques ● Local Files ● Shared Preferences ● Content Providers ● Cloud servershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
14. 14. Shared Preferences ● Lightweight key/value pair mechanism for saving primitive application data (Boolean, string, float, long, and integer) ● Commonly used for saving users application preferences, UI states, application settings ● Instances of SharedPreferences class can contain key/value pairs that can be shared among all activities running within the same application contexthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
15. 15. Accessing Shared Preferences ● Shared preferences are key/value pairs are that are persisted against the applications context ● Any application component such as activities, services, and broadcast receivers can access those values by getting the applications contexthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
16. 16. Getting SharedPreferences Object // Use PreferenceManager and the applications context to initialize // a SharedPreferences object inside a component SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
17. 17. Example 01 Implement an application, SharedPrefsApp01, that allows the user the enter strings for two shared preferences via EditTexts and saves those shared preferences. The next time the application is started, the previously entered values are loaded into the EditTexts from the saved shared preferences. The main activity has three buttons: Save, Clear, and Finish. Click on Save reads the two strings entered by the user and saves them as shared preferences under appropriate keys. Clear clears the EditTexts, Finish finishes the activity by calling Activity.finish().http://www.youtube.com/vkedco
18. 18. Example 01: Screen Snapshotshttp://www.youtube.com/vkedco
19. 19. Activity Reference Variables // SharedPreferences, Editor to store and save // shared preferences as key/value pairs SharedPreferences mSharedPrefs = null; Editor mSharedPrefsEditor = null; Resources mRes = null;http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
20. 20. Example 01 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
21. 21. Persisting Shared Preferences // grab the strings entered by the user, put them into the SharedPreferences // Editor under appropirate keys and persist them via commit(). private void saveSharedPrefs() { mSharedPrefsEditor.putString(mRes.getString(R.string.pref_01_key), mEdTxtPrefVal01.getText().toString()); mSharedPrefsEditor.putString(mRes.getString(R.string.pref_02_key), mEdTxtPrefVal02.getText().toString()); mSharedPrefsEditor.commit(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
22. 22. Loading Shared Preferences // Load up the values of the appropriate keys from SharedPreferences and // default to “” if they are not defined.. private void loadSharedPrefs() { this.mEdTxtPrefVal01 .setText(this.mSharedPrefs .getString(this.mRes.getString(R.string.pref_01_key), "")); this.mEdTxtPrefVal02 .setText(this.mSharedPrefs .getString(this.mRes.getString(R.string.pref_02_key), "")); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
23. 23. Inter-Activity Communication ● Components within the same application can use shared preferences to pass information to each other ● A typical use case is for component A to persist several key/value pairs in as shared preferences ● Component B then uses shared preferences to read key/value pairs and do something depending of specific values.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
24. 24. Example 02 Implement an application, SharedPrefsApp02, that allows the user the enter strings for two shared preferences via EditTexts and saves those shared preferences in the activity SharedPreferencesSaverAct. This activity has four buttons: Save, Pass, Clear, and Finish. The logic of Save, Clear, and Finish is the same as in Example 01. Pass button launches another activity, SharedPreferencesReceiverAct. This activity loads the values persisted by the user with shared preferences in SharedPreferencesSaverAct.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
25. 25. Example 02: Screen Snapshotshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
26. 26. Example 02 Code Snippetshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
27. 27. Persisting Shared Preferences in SharedPreferencesSaverAct // grab the strings entered by the user, put them into the SharedPreferences // Editor under appropirate keys and persist them via commit(). private void saveSharedPrefs() { mSharedPrefsEditor.putString(mRes.getString(R.string.pref_01_key), mEdTxtPrefVal01.getText().toString()); mSharedPrefsEditor.putString(mRes.getString(R.string.pref_02_key), mEdTxtPrefVal02.getText().toString()); mSharedPrefsEditor.commit(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
28. 28. Launching SharedPreferncesReceiverAct in SharedPreferencesSaverAct // SharedPreferencesReceiverAct is launched on a click on the Pass button mBtnPass.setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), SharedPrefsReceiverAct.class); startActivity(i); } });http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
29. 29. Loading Shared Preferences in SharedPreferncesReceiverAct // Look up appropriate values and default to empty strings if they are not // defined. private void loadSharedPrefs() { mEdTxtPrefVal01.setText(this.mSharedPrefs.getString(mRes.getString(R.string.pref_01_key), "")); mEdTxtPrefVal02.setText(this.mSharedPrefs.getString(mRes.getString(R.string.pref_02_key), "")); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
30. 30. References ● http://developer.android.com/reference/android/content/SharedPreferences.html ● Source code available at http://vkedco.blogspot.com/2012/10/mobappdev-lecture-12.htmlhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
31. 31. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com