Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences

Size: px
Start display at page:

Download "Mobile Programming Lecture 7. Dialogs, Menus, and SharedPreferences"

Transcription

1 Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences

2 Agenda Dialogs Menus SharedPreferences

3 Android Application Components 1. Activity 2. Broadcast Receiver 3. Content Provider 4. Service

4 Dialogs A Dialog is a small window that appears in front of the current Activity It causes the Activity to lose focus Used for ProgressBars, Alerts, etc

5 Dialogs oncreatedialog called the first time the showdialog(int) method is called onpreparedialog called every time it s opened and as such will allow you to override your dialog before it is shown Without this, it will remain the same as the first time it was opened Note: As of API Level 13 oncreatedialog and onpreparedialog were deprecated in favor of DialogFragment

6 Dialogs - AlertDialog An AlertDialog is an extension of the Dialog class It is capable of constructing most Dialog user interfaces and is the suggested Dialog type

7 Dialogs - AlertDialog You should use it for Dialogs that use any of the following features A title A text message One, two, or three buttons A list of selectable items (with optional checkboxes or radio buttons)

8 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog( int id) { Dialog dialog = null; switch (id) { case DIALOG_EXIT_ID : AlertDialog.Builder builder = new AlertDialog.Builder( this); builder.setmessage( "Do you want to exit?" ); builder.setcancelable( true); builder.setpositivebutton( "Yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

9 Nothing special here, just an Dialogs int I use - to Creating identify the dialog, an AlertDialog because we can have more than one public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

10 Dialogs - Creating an AlertDialog Override this method of Activity, which is called public class MyDialogs extends Activity { static final when int DIALOG_EXIT_ID you want to show = 1; any dialog of the Activity protected Dialog oncreatedialog(int id) { Dialog dialog = null; switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

11 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; Switch because we can have more than one dialog, meaning we need protected Dialog oncreatedialog(int id) { to check the dialog id Dialog dialog = null; switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

12 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; If you want an AlertDialog, you need to build one first switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

13 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; Give the user a message switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

14 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; If true, then the user can press the back button to dismiss the dialog. switch(id) false { would force the user to make an case DIALOG_EXIT_ID: action on the dialog. AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

15 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; Add a button, AND set a listener for switch(id) { when the button is pressed. This case DIALOG_EXIT_ID: should be the "positive" button, e.g. AlertDialog.Builder builder = new AlertDialog.Builder(this); "Yes", builder.setmessage("do "Absolutely!" you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

16 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; switch(id) { case This DIALOG_EXIT_ID: is the listener for when the "positive" AlertDialog.Builder button is pressed, builder so = you new AlertDialog.Builder(this); should builder.setmessage("do take some kind of action. you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

17 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { The Dialog isn't public created void onclick(dialoginterface dialog, int id) { until you call create() AlertDialogExample.this.finish(); ); dialog = builder.create(); break; return dialog;

18 Dialogs - Creating an AlertDialog public class MyDialogs extends Activity { static final int DIALOG_EXIT_ID = 1; protected Dialog oncreatedialog(int id) { Dialog dialog = null; switch(id) { case DIALOG_EXIT_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setmessage("do you want to exit?"); builder.setcancelable(true); builder.setpositivebutton("yes", new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int id) { AlertDialogExample.this.finish(); ); dialog = builder.create(); break; The type of this method is Dialog, so here we return... the Dialog! return dialog;

19 Dialogs - AlertDialog You can add up to 3 buttons on the AlertDialog by calling 1. dialog.setpositivebutton() We just used this one in the previous slides 2. dialog.setnegativebutton() "No", "Cancel" 3. dialog.setneutralbutton() "Remind me Later"

20 Dialogs - Showing a Dialog To show a Dialog on the screen, simply call showdialog(int) from within your Activity. It takes the ID of the dialog as a parameter. You can also call it from within an anonymous inner class Make sure you override the oncreatedialog() method in that Activity!

21 Dialogs - Showing a Dialog See AlertDialogExample

22 Dialogs - Dismissing a Dialog You don't want to show the Dialog to the user forever! You have to allow the user to close the Dialog somehow, even if it's not cancelable You can dismiss the Dialog by calling dismissdialog(int) from within the controlling Activity where the argument is the Dialog ID dismiss() on the Dialog object e.g. dialog.dismiss()

23 Dialogs - Showing a Dialog See DismissDialogExample

24 Dialogs - AlertDialog with a List Not only buttons! You can also add a list to your AlertDialog

25 Dialogs - AlertDialog with a List String[] countries = new String[] { "Bahamas", "Barbados", "Jamaica", "St. Martin"; public Dialog oncreatedialog(int id) { Builder builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

26 We will use this Dialogs - AlertDialog with a List String array for our list String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; public Dialog oncreatedialog(int id) { Builder builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

27 Dialogs - AlertDialog with a List This is a method in an Activity class, as String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; in the previous example public Dialog oncreatedialog(int id) { Builder builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

28 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; We're still using an AlertDialog Builder public Dialog oncreatedialog(int id) { Builder builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

29 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; public Dialog This oncreatedialog(int time we call id) { Builder setitems()! builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

30 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; First argument public Dialog oncreatedialog(int id) { should be your list Builder builder = of new items AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

31 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; There is more than public Dialog oncreatedialog(int id) { one Builder builder = new AlertDialog.Builder(this); OnClickListener class! builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

32 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; If you want to use both View.OnclickListener (for buttons) and DialogInterface.OnClickListener (for public Dialog oncreatedialog(int id) Dialogs), { then you need to be more specific Builder builder = new AlertDialog.Builder(this); here builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

33 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; public Dialog oncreatedialog(int id) { Builder When builder an item = in new the AlertDialog.Builder(this); list is clicked, builder.settitle("select you are Android kindly provides a Country"); you with the Dialog itself, as well builder.setitems(countries, new DialogInterface.OnClickListener() { as the index of the clicked item public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); ); return builder.create();

34 Dialogs - AlertDialog with a List String[] countries = new String[]{"Bahamas", "Barbados", "Jamaica", "St. Martin"; public Dialog oncreatedialog(int id) { Builder builder = new AlertDialog.Builder(this); builder.settitle("select a Country"); builder.setitems(countries, new DialogInterface.OnClickListener() { public void onclick(dialoginterface dialog, int index) { Toast.makeText(getApplicationContext(), "You selected " + countries[index], Toast.LENGTH_LONG).show(); Let's not forget to create the Dialog and return it ); return builder.create();

35 Dialogs - AlertDialog with a List See AlertDialogListExample

36 Dialogs - AlertDialog Custom View If you wish to display just a simple message to your user the setmessage() method will suffice To display a more complex view, first get a handle on the default FrameLayout custom FrameLayout custom = (FrameLayout) findviewbyid(android.r.id.custom); Add your custom View by calling addview() on the FrameLayout custom.addview(complexview, new LayoutParams(MATCH_PARENT, MATCH_PARENT);

37 Dialogs - Date/TimePicker Dialogs See DatePickerDialogExample

38 Dialogs - Custom Dialogs - SeekBar You can create your own Dialog if the standard Android Dialogs are not suitable for your needs For example, there is no SeekBar Dialog, but you can create your own See CustomDialogExample

39 Dialogs - DialogFragment Creating Dialogs by using the oncreatedialog() method of an Activity is old school Creating Dialogs by using a DialogFragment is new school

40 Dialogs - DialogFragment DialogFragment also has an oncreatedialog() callback method! i.e., both an Activity and a DialogFragment have an oncreatedialog() callback method

41 Dialogs - DialogFragment DialogFragment is slightly different than the other Fragments we've seen so far We don't need to add it to the XML Nor do we need to add it to the UI using a FragmentTransaction

42 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout. main); Button button = (Button) findviewbyid(r.id. button1); button.setonclicklistener( new OnClickListener() { public void onclick(view v) { MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); ); public void dopositiveclick() { Toast.makeText( this, "dopositiveclick()", Toast. LENGTH_LONG).show();

43 Dialogs - DialogFragment Let's create our Activity first... public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

44 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); Let's show the Dialog when this Button is clicked! setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

45 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); The Button was just clicked at this point, so let's create a new instance of MyDialogFragment, which we will see in a few slides button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

46 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); Just call.show() on the Fragment! This time we didn't need to use the FragmentTransaction to add the Fragment button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

47 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); Just pass the FragmentManager and it will take care of adding and removing the Fragment for you button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

48 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { ); public void onclick(view v) { Second argument is the tag that you want to assign to the Fragment MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

49 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); We will get back to this! public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

50 Dialogs - DialogFragment Let's take a look at our DialogFragment

51 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act = (MyActivity) getactivity(); act.dopositiveclick(); ); return builder.create();

52 Dialogs - DialogFragment! public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); ); return builder.create();

53 Dialogs - DialogFragment Dialog also has an oncreatedialog(), public class MyDialogFragment the proof is in the extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); ); return builder.create();

54 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { This is how we get the Context from within a Fragment, remember!? public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); ); return builder.create();

55 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act= (MyActivity) getactivity(); We need to act.dopositiveclick(); return a Dialog! ); return builder.create();

56 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment You've seen { this other stuff before! public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { public void onclick(dialoginterface dialog, int which) { MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); ); return builder.create();

57 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { The button in the Dialog has now been clicked! What do we do next? public void onclick(dialoginterface dialog, int which) { ); MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); return builder.create();

58 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { Let's get a handle on the Activity containing this Fragment public void onclick(dialoginterface dialog, int which) { ); MyActivity act= (MyActivity) getactivity(); act.dopositiveclick(); return builder.create();

59 Dialogs - DialogFragment public class MyDialogFragment extends DialogFragment { public Dialog oncreatedialog(bundle savedinstancestate) { Builder builder = new AlertDialog.Builder(getActivity()); builder.settitle("this is a DialogFragment!"); builder.setpositivebutton("ok", new OnClickListener() { Let's call our own custom method, public void dopositiveclick() onclick(dialoginterface on dialog, int which) { the Activity MyActivity act= (MyActivity) getactivity(); ); act.dopositiveclick(); return builder.create();

60 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); Which takes us back here, back to our Activity. public void dopositiveclick() { Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

61 Dialogs - DialogFragment public class MyActivity extends Activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Button button = (Button) findviewbyid(r.id.button1); button.setonclicklistener(new OnClickListener() { public void onclick(view v) { ); public void dopositiveclick() { MyDialogFragment f = new MyDialogFragment(); f.show(getfragmentmanager(), "dialog"); Simply make a Toast as evidence that we were successful Toast.makeText(this, "dopositiveclick()", Toast.LENGTH_LONG).show();

62 Dialogs - DialogFragment Note that we don't need to override oncreateview() or onactivitycreated() methods of a DialogFragment You may choose to override oncreateview() and return a View if you want to create some custom Dialog without using oncreatedialog() I'll leave it up to the Android developer's website to explain it if anyone is interested in doing so

63 Dialogs - DialogFragment See FragmentDialogExample

64 Menu Options In Android 2.3.x and below, clicking on the dedicated Menu button allows the user to reveal menu options In Android 3.0 and above, the options menu is presented by way of an action bar The dedicated Menu button is deprecated and some devices just do not have one

65 Menu Options - Creating one <= 2.3.x Right click on your project Select New > Android Resource File In the Resource Type drop down list, select Menu Enter a Filename and click Finish Within the <menu> </menu> tags, create a new <item> tag Edit the android:id and android:title attributes as appropriate Repeat to add additional menu options

66 Menu Options - Creating one <= 2.3.x We will use this Menu XML file, main_menu.xml, for our example <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android=" <item android:id="@+id/set_text" android:title="@string/set_text_opt"/> <item android:id="@+id/close" android:title="@string/close_opt" /> </menu>

67 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

68 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Override this Activity method public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

69 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); Similar to a LayoutInflater, but for Menus instead public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

70 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); public Show boolean the Menu now oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

71 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); This is the Menu XML file that we created previously public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

72 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); This is given as argument to oncreateoptionsmenu, so use it as argument to inflate the menu public boolean oncreateoptionsmenu(menu menu) { MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu, menu); return true;

73 Menu Options - Creating one <= 2.3.x public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); public boolean oncreateoptionsmenu(menu menu) { Return true if you want the MenuInflater menu to be displayed, inflater false = getmenuinflater(); if you don't inflater.inflate(r.menu.main_menu, menu); return true;

74 Menu Options - Creating one <= 2.3.x This is enough for the Menu to be displayed on the screen when the user presses the Menu button If you want to take action after an option is selected, then you need to override the onoptionsitemselected() method of Activity

75 Menu Options - Creating one <= 2.3.x public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case R.id.set_text: TextView tv = (TextView) findviewbyid(r.id. textview1); tv.settext("first Option Selected!" ); break; case R.id.close: Toast.makeText( this, "Goodbye!", Toast.LENGTH_LONG).show(); finish(); break; return true;

76 Menu Options - Creating one <= 2.3.x public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { We override this method of Activity case R.id.set_text: TextView tv = (TextView) findviewbyid(r.id.textview1); tv.settext("first Option Selected!"); break; case R.id.close: Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show(); finish(); break; return true;

77 Menu Options - Creating one <= 2.3.x Which menu item was selected? public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case R.id.set_text: TextView tv = (TextView) findviewbyid(r.id.textview1); tv.settext("first Option Selected!"); break; case R.id.close: Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show(); finish(); break; return true;

78 Menu Options - Creating one <= 2.3.x Here we just changed the text of a public boolean onoptionsitemselected(menuitem item) { TextView when the item switch (item.getitemid()) { R.id.set_text is selected case R.id.set_text: TextView tv = (TextView) findviewbyid(r.id.textview1); tv.settext("first Option Selected!"); break; case R.id.close: Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show(); finish(); break; return true;

79 Menu Options - Creating one <= 2.3.x public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case R.id.set_text: TextView tv = (TextView) findviewbyid(r.id.textview1); tv.settext("first Option Selected!"); Here we break; close our app when item R.id.close is selected case R.id.close: Toast.makeText(this, "Goodbye!", Toast.LENGTH_LONG).show(); finish(); break; return true;

80 Menu Options - Creating one <= 2.3.x See MenuOptionsExample

81 Menu Options - Creating one <= 2.3.x You can change the menu options that show up at runtime

82 Menu Options - Creating one >= 3.0 For Android 3.0 and higher... We ll take a closer look at this when we talk about Material Design

83 Context Menu You can provide a context menu for any View, but they are most often used for items in a ListView GridView Other View collections in which the user can perform direct actions on each item.

84 Context Menu - Creating one When creating a Context Menu, you can create a Menu XML file in the same way you do for an Options Menu

85 Context Menu - Creating one We will use the following XML file for our example <?xml version= "1.0" encoding="utf-8"?> <menu xmlns:android= " > <item android:id="@+id/edit_option" android:title= "Edit"></item> <item android:id="@+id/share_option" android:title= "Share"></item> <item android:id="@+id/delete_option" android:title= "Delete"></item> </menu> All of the callback methods in this example are declared within our ListActivity

86 Context Menu - Creating one As a reminder, A ListActivity extends Activity It already has a ListView, so you don't need to add one Instead of getting a handle on the ListView by calling findviewbyid(r.id.list_view_id) You simply call getlistview() instead

87 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior", "George", "Dan" ; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setlistadapter( new ArrayAdapter<String>( this, android.r.layout. simple_list_item_1, entries)); registerforcontextmenu(getlistview());

88 Context Menu - Creating one We will use this String array to populate our List public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior", "George","Dan"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setlistadapter(new ArrayAdapter<String>(this, android.r.layout.simple_list_item_1, entries)); registerforcontextmenu(getlistview());

89 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] {"Martin", "Anderson", "Junior", "George", "Dan"; Populate the ListView public here void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setlistadapter(new ArrayAdapter<String>(this, android.r.layout.simple_list_item_1, entries)); registerforcontextmenu(getlistview());

90 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] {"Martin", "Anderson", "Junior", "George", "Dan"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); Register the ListView with a this.setlistadapter(new Context Menu, now the menu ArrayAdapter<String>(this, will show up when android.r.layout.simple_list_item_1, long entries)); press on the ListView registerforcontextmenu(getlistview());

91 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior", oncreate() from previous "George", slide is here "Dan";... public void oncreatecontextmenu(contextmenu menu, View v, ContextMenuInfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.context_menu, menu);...

92 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior",... "George", "Dan" ; public void oncreatecontextmenu(contextmenu menu, View v, Override this method. It is called when the Context Menu for View v is being built super.oncreatecontextmenu(menu, v, menuinfo); MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.context_menu, menu); ContextMenuInfo menuinfo) {...

93 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior", "George", "Dan" ;... public You've void seen oncreatecontextmenu(contextmenu this menu, View v, before ContextMenuInfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); MenuInflater inflater = getmenuinflater(); inflater.inflate(r.menu.context_menu, menu);...

94 Context Menu - Creating one public class ContextMenuExample extends ListActivity { String[] entries = new String[] { "Martin", "Anderson", "Junior", "George", "Dan" ;... public void oncreatecontextmenu(contextmenu menu, View v, ContextMenuInfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); MenuInflater inflater = getmenuinflater(); oncontextitemselected() on inflater.inflate(r.menu.context_menu, menu); the next slide goes here...

95 Override this method Context Menu - Creating one of Activity public boolean oncontextitemselected(menuitem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); TextView tv = (TextView) getlistview().getadapter().getview(info.position, null, null); switch(item.getitemid()) { case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: /* Edit option selected */ break; return true;

96 This Object has information about the Context Menu, NOT the item in the List Context Menu - Creating one public boolean oncontextitemselected(menuitem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); TextView tv = (TextView) getlistview().getadapter().getview(info.position, null, null); switch(item.getitemid()) { case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: /* Edit option selected */ break; return true;

97 Context Menu - Creating one This Object will have public boolean oncontextitemselected(menuitem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); TextView tv = (TextView) getlistview().getadapter().getview(info.position, null, null); switch(item.getitemid()) { information about the item pressed case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: return true; /* Edit option selected */ break;

98 Context Menu - Creating one We can get a handle on the item in the ListView by using public boolean oncontextitemselected(menuitem item) { info.position AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); TextView tv = (TextView) getlistview().getadapter().getview(info.position, null, null); switch(item.getitemid()) { case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: return true; /* Edit option selected */ break;

99 Context Menu - Creating one public boolean oncontextitemselected(menuitem item) { info.position tells us which item AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); was pressed, this is how we tell TextView which tv = Menu (TextView) Item was getlistview().getadapter().getview(info.position, pressed null, null); switch(item.getitemid()) { case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: /* Edit option selected */ break; return true;

100 Context Menu - Creating one public boolean oncontextitemselected(menuitem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getmenuinfo(); TextView tv = (TextView) getlistview().getadapter().getview(info.position, null, null); switch(item.getitemid()) { case R.id.edit_option: /* Edit option selected */ break; case R.id.share_option: /* Share option selected */ break; case R.id.delete_option: return true; /* Edit option selected */ break; By combining info.position and item.getitemid(), we know the action needs to be performed on the item

101 Context Menu - Creating one See ContextMenuExample

102 Preferences - SharedPreferences SharedPreferences represent 1 of the 5 methods for Data Storage in Android It stores key-value pairs of primitive data types boolean, int, float, long, String Data persists even if your app has been fully terminated

103 Preferences - SharedPreferences SharedPreferences are only available to app that created that created them!

104 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

105 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; The desired name of your SharedPreferences file protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

106 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; You can get a SharedPreferences file by name by calling this method protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

107 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); The second argument is the mode SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

108 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); If the SharedPreferences file doesn't exist at this point, it will be created for you SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

109 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; Try to get the boolean value protected void oncreate(bundle state){ "silentmode", "silentmode" is the key. You decide on the name of the key. super.oncreate(state); "silentmode" is not a keyword here SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

110 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); If the key doesn't exist (could be because the file was just created in the previous line of code), then this will be the value returned SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

111 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ An imaginary method that you super.oncreate(state); created to change the volume setting of the device to silent SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

112 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean We want silent data = to settings.getboolean("silentmode", persist even false); setsilent(silent); after the app has been terminated, so let's Override onstop() protected void onstop(){ super.onstop(); SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

113 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); Get a handle on our SharedPreferences again, which should have the "silentmode" value set at this point SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

114 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ If we want to modify the SharedPreferences, we need to use a super.onstop(); SharedPreferences Editor SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

115 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ Let's set the value value of silentmode super.onstop(); to the imaginary boolean value msilentmode SharedPreferences settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

116 Preferences - SharedPreferences public class SharedPrefsExample extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; protected void oncreate(bundle state){ super.oncreate(state); SharedPreferences settings = getsharedpreferences(prefs_name, 0); boolean silent = settings.getboolean("silentmode", false); setsilent(silent); protected void onstop(){ super.onstop(); Don't forget to save your SharedPreferences changes to the file! settings = getsharedpreferences(prefs_name, 0); SharedPreferences.Editor editor = settings.edit(); editor.putboolean("silentmode", msilentmode); editor.commit();

117 Preferences - SharedPreferences See SharedPrefsExample

118 Preferences - PreferenceActivity If you want to provide the user with a UI for changing preferences, you can use a PreferenceActivity in combination with SharedPreferences To create a PreferenceActivity, first create a Preference XML file

119 Preferences - PreferenceActivity File > New > Android Resource File Resource Type: XML Enter the file name, e.g. preferences.xml This creates a file in the /res/xml folder Ensure the preferences.xml file has a <PreferenceScreen> as the root element Add the various types of preferences as necessary e.g. EditTextPreference, CheckBoxPreference, Set the attributes for each preference as necessary

120 Preferences - PreferenceActivity We will use this preferences.xml file for our example <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android=" <EditTextPreference android:dialogtitle="username" android:dialogmessage="please enter your Username" android:summary="username for logging in to this app" android:title="username" android:key="username"/> <CheckBoxPreference android:summaryoff="i do not want your spam" android:key="spam" android:title="spam" android:summaryon="sign me up for spam"/> </PreferenceScreen>

121 Preferences - PreferenceActivity Creating a PreferenceActivity is easy!

122 Preferences - PreferenceActivity public class Preferences extends PreferenceActivity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences);

123 Preferences - PreferenceActivity Extend PreferenceActivity public class Preferences extends PreferenceActivity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences);

124 Preferences - PreferenceActivity public class Preferences extends PreferenceActivity { From the preferences.xml file we added previously public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences);

125 Preferences - PreferenceActivity In your main Activity, you can get the Preferences without specifying the name of the XML file

126 Preferences - PreferenceActivity public class MainActivity extends Activity { SharedPreferences mprefs; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mprefs = PreferenceManager.getDefaultSharedPreferences( getbasecontext()); String username = mprefs.getstring( "username", "None");

127 Preferences - PreferenceActivity public class MainActivity extends Activity { SharedPreferences mprefs; This will allow you to access the preference settings, even if you have more than one preference XML file public associated void oncreate(bundle with a PreferenceActivity savedinstancestate) { super.oncreate(savedinstancestate); mprefs = PreferenceManager.getDefaultSharedPreferences( getbasecontext()); String username = mprefs.getstring("username","none");

128 Preferences - PreferenceActivity public class MainActivity extends Activity { SharedPreferences mprefs; These preferences will automatically save when the user public void interacts oncreate(bundle with them! savedinstancestate) { super.oncreate(savedinstancestate); mprefs = PreferenceManager.getDefaultSharedPreferences( getbasecontext()); String username = mprefs.getstring("username","none");

129 Preferences - PreferenceActivity See PreferenceActivityExample

130 Preferences - PreferenceActivity You can also add a Listener for when a Preference has been changed When a Preference is changed, you may need to update certain values tied to these Preferences

131 Preferences - PreferenceFragment Android 3.0 and higher PreferenceFragment

132 References The Busy Coder's Guide to Android Development - Mark Murphy Android Developers The Mobile Lab at Florida State University

Preferences. Marco Ronchetti Università degli Studi di Trento

Preferences. Marco Ronchetti Università degli Studi di Trento 1 Preferences Marco Ronchetti Università degli Studi di Trento SharedPreferences SharedPreferences allows to save and retrieve persistent key-value pairs of primitive data types. This data will persist

More information

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android.

Q.1 Explain the dialog and also explain the Demonstrate working dialog in android. Q.1 Explain the dialog and also explain the Demonstrate working dialog in android. - A dialog is a small window that prompts the user to make a decision or enter additional information. - A dialog does

More information

South Africa Version Control.

South Africa Version Control. South Africa 2013 Lecture 7: User Interface (Navigation)+ Version Control http://aiti.mit.edu South Africa 2013 Today s agenda Recap Navigation Version Control 2 Tutorial Recap Activity 1 Activity 2 Text

More information

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9) Android Specifics Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9) Android Specifics ArrayAdapter Preferences Widgets Jonathan Diehl, Hendrik Thüs 2 ArrayAdapter Jonathan Diehl, Hendrik Thüs

More information

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop Workshop 1. Create an Option Menu, and convert it into Action Bar (Page 1 8) Create an simple Option Menu Convert Option Menu into Action Bar Create Event Listener for Menu and Action Bar Add System Icon

More information

Meniu. Create a project:

Meniu. Create a project: Meniu Create a project: Project name: P0131_MenuSimple Build Target: Android 2.3.3 Application name: MenuSimple Package name: ru.startandroid.develop.menusimple Create Activity: MainActivity Open MainActivity.java.

More information

Mobila applikationer och trådlösa nät, HI1033, HT2012

Mobila applikationer och trådlösa nät, HI1033, HT2012 Mobila applikationer och trådlösa nät, HI1033, HT2012 Today: - User Interface basics - View components - Event driven applications and callbacks - Menu and Context Menu - ListView and Adapters - Android

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu India Summer 2012 Review Session Android and Web Working with Views Working with Views Create a new Android project. The app name should

More information

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents Mobile Programming Lecture 3 Resources, Selection, Activities, Intents Lecture 2 Review What widget would you use to allow the user to enter a yes/no value a range of values from 1 to 100 What's the benefit

More information

Android Programs Day 5

Android Programs Day 5 Android Programs Day 5 //Android Program to demonstrate the working of Options Menu. 1. Create a New Project. 2. Write the necessary codes in the MainActivity.java to create OptionMenu. 3. Add the oncreateoptionsmenu()

More information

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017 Software Engineering Large Practical: Storage, Settings and Layouts Stephen Gilmore School of Informatics October 27, 2017 Contents 1. Storing information 2. Settings 3. Layouts 1 Storing information Storage

More information

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Have a development environment in 256 or 255 Be familiar with the application lifecycle Upcoming Assignments Readings: Chapter 4 by today Horizontal Prototype due Friday, January 22 Quiz 2 today at 2:40pm Lab Quiz next Friday during lecture time (2:10-3pm) Have a development environment in

More information

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs Overview MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs Lecture: MVC Model View Controller What is an App? Android Activity Lifecycle Android Debugging Fixing Rotations & Landscape Layouts Localization

More information

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion

External Services. CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion External Services CSE 5236: Mobile Application Development Course Coordinator: Dr. Rajiv Ramnath Instructor: Adam C. Champion 1 External Services Viewing websites Location- and map-based functionality

More information

Mobila applikationer och trådlösa nät, HI1033, HT2013

Mobila applikationer och trådlösa nät, HI1033, HT2013 Mobila applikationer och trådlösa nät, HI1033, HT2013 Today: - User Interface basics - View components - Event driven applications and callbacks - Menu and Context Menu - ListView and Adapters - Android

More information

Mobile and Ubiquitous Computing: Android Programming (part 3)

Mobile and Ubiquitous Computing: Android Programming (part 3) Mobile and Ubiquitous Computing: Android Programming (part 3) Master studies, Winter 2015/2016 Dr Veljko Pejović Veljko.Pejovic@fri.uni-lj.si Based on Programming Handheld Systems, Adam Porter, University

More information

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling Mobile Programming Lecture 2 Layouts, Widgets, Toasts, and Event Handling Lecture 1 Review How to edit XML files in Android Studio? What holds all elements (Views) that appear to the user in an Activity?

More information

Mobile Computing Fragments

Mobile Computing Fragments Fragments APM@FEUP 1 Fragments (1) Activities are used to define a full screen interface and its functionality That s right for small screen devices (smartphones) In bigger devices we can have more interface

More information

MOBILE APPLICATIONS PROGRAMMING

MOBILE APPLICATIONS PROGRAMMING Data Storage 23.12.2015 MOBILE APPLICATIONS PROGRAMMING Krzysztof Pawłowski Polsko-Japońska Akademia Technik Komputerowych STORAGE OPTIONS Shared Preferences SQLite Database Internal Storage External Storage

More information

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming University of Babylon College of Information Technology Department of Information Networks Mobile Fundamentals and Programming Lecture 7: Data Persistence : shared preferences Lecturer : Ali Kadhim Al-Bermani

More information

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers Multiple devices Multiple devices Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers Create a new directory in your project's res/ and name it using the

More information

Mobile Application Development MyRent Settings

Mobile Application Development MyRent Settings Mobile Application Development MyRent Settings Waterford Institute of Technology October 13, 2016 John Fitzgerald Waterford Institute of Technology, Mobile Application Development MyRent Settings 1/19

More information

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment FRAGMENTS Fragments An activity is a container for views When you have a larger screen device than a phone like a tablet it can look too simple to use phone interface here. Fragments Mini-activities, each

More information

Preferences. Marco Ronchetti Università degli Studi di Trento

Preferences. Marco Ronchetti Università degli Studi di Trento 1 Preferences Marco Ronchetti Università degli Studi di Trento SharedPreferences SharedPreferences allows to save and retrieve persistent key-value pairs of primitive data types. This data will persist

More information

Android Dialogs. Dialogs are simple visual objects that pop up and display a message or prompt for some user input.

Android Dialogs. Dialogs are simple visual objects that pop up and display a message or prompt for some user input. Android Dialogs Dialogs are simple visual objects that pop up and display a message or prompt for some user input. Create a new Android project with a suitable name and package. To begin with it will have

More information

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr. Android Application Model I CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr. Rajiv Ramnath 1 Framework Support (e.g. Android) 2 Framework Capabilities

More information

Android Application Model I

Android Application Model I Android Application Model I CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr. Rajiv Ramnath Reading: Big Nerd Ranch Guide, Chapters 3, 5 (Activities);

More information

ListView Containers. Resources. Creating a ListView

ListView Containers. Resources. Creating a ListView ListView Containers Resources https://developer.android.com/guide/topics/ui/layout/listview.html https://developer.android.com/reference/android/widget/listview.html Creating a ListView A ListView is a

More information

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Spring Lecture 5 Lecturer: Omid Jafarinezhad Mobile Programming Sharif University of Technology Spring 2016 - Lecture 5 Lecturer: Omid Jafarinezhad Storage Options Android provides several options for you to save persistent application data. The

More information

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27 Android Coding Dr. J.P.E. Hodgson August 23, 2010 Dr. J.P.E. Hodgson () Android Coding August 23, 2010 1 / 27 Outline Starting a Project 1 Starting a Project 2 Making Buttons Dr. J.P.E. Hodgson () Android

More information

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University. Creating dialogs Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University. Portions of this page are reproduced from work created and

More information

1. Simple List. 1.1 Simple List using simple_list_item_1

1. Simple List. 1.1 Simple List using simple_list_item_1 1. Simple List 1.1 Simple List using simple_list_item_1 1. Create the Android application with the following attributes. Application Name: MySimpleList Project Name: Package Name: MySimpleList com.example.mysimplelist

More information

More Effective Layouts

More Effective Layouts More Effective Layouts In past weeks, we've looked at ways to make more effective use of the presented display (e.g. elastic layouts, and separate layouts for portrait and landscape), as well as autogenerating

More information

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar Mobile Application Development Higher Diploma in Science in Computer Science Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology

More information

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars CS371m - Mobile Computing More UI Navigation, Fragments, and App / Action Bars EFFECTIVE ANDROID NAVIGATION 2 Clicker Question Have you heard of the terms Back and Up in the context of Android Navigation?

More information

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2 Workshop 1. Compare different layout by using Change Layout button (Page 1 5) Relative Layout Linear Layout (Horizontal) Linear Layout (Vertical) Frame Layout 2. Revision on basic programming skill - control

More information

Mobile and Ubiquitous Computing: Android Programming (part 4)

Mobile and Ubiquitous Computing: Android Programming (part 4) Mobile and Ubiquitous Computing: Android Programming (part 4) Master studies, Winter 2015/2016 Dr Veljko Pejović Veljko.Pejovic@fri.uni-lj.si Examples from: Mobile and Ubiquitous Computing Jo Vermeulen,

More information

UI Fragment.

UI Fragment. UI Fragment 1 Contents Fragments Overviews Lifecycle of Fragments Creating Fragments Fragment Manager and Transactions Adding Fragment to Activity Fragment-to-Fragment Communication Fragment SubClasses

More information

Programming with Android: Animations, Menu, Toast and Dialogs. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Programming with Android: Animations, Menu, Toast and Dialogs. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna Programming with Android: Animations, Menu, Toast and Dialogs Luca Bedogni Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna Animations vmake the components move/shrink/color vmainly

More information

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments CS371m - Mobile Computing More UI Action Bar, Navigation, and Fragments ACTION BAR 2 Options Menu and Action Bar prior to Android 3.0 / API level 11 Android devices required a dedicated menu button Pressing

More information

07. Menu and Dialog Box. DKU-MUST Mobile ICT Education Center

07. Menu and Dialog Box. DKU-MUST Mobile ICT Education Center 07. Menu and Dialog Box DKU-MUST Mobile ICT Education Center Goal Learn how to create and use the Menu. Learn how to use Toast. Learn how to use the dialog box. Page 2 1. Menu Menu Overview Menu provides

More information

Computer Science Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 26, 2017

Computer Science Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 26, 2017 Computer Science Large Practical: Storage, Settings and Layouts Stephen Gilmore School of Informatics October 26, 2017 Contents 1. Storing information 2. Kotlin compilation 3. Settings 4. Layouts 1 Storing

More information

Android Using Menus. Victor Matos Cleveland State University

Android Using Menus. Victor Matos Cleveland State University 8 Android Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright 2008-2009 CommonsWare, LLC. ISBN: 978-0-9816780-0-9 & Android Developers http://developer.android.com/index.html

More information

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters Mobile Programming Lecture 5 Composite Views, Activities, Intents and Filters Lecture 4 Review How do you get the value of a string in the strings.xml file? What are the steps to populate a Spinner or

More information

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time The 1 activity 1 thing the user can do heuristic may not make sense for larger devices Application

More information

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed. Action Bar Introduction The Action Bar is a widget that is shown on top of the screen. It includes the application logo on its left side together with items available from the options menu on the right.

More information

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State EMBEDDED SYSTEMS PROGRAMMING 2016-17 Application Tip: Saving State THE PROBLEM How to save the state (of a UI, for instance) so that it survives even when the application is closed/killed The state should

More information

By The Name of Allah. The Islamic University of Gaza Faculty of Engineering Computer Department Final Exam. Mobile Computing

By The Name of Allah. The Islamic University of Gaza Faculty of Engineering Computer Department Final Exam. Mobile Computing By The Name of Allah The Islamic University of Gaza Faculty of Engineering Computer Department Final Exam Dr. Aiman Ahmed Abu Samra Eng. Nour El-Deen I. Jaber Student Name ID Mark Exam Duration \ 1:30

More information

Distributed Systems 2011 Assignment 1

Distributed Systems 2011 Assignment 1 Distributed Systems 2011 Assignment 1 Gábor Sörös gabor.soros@inf.ethz.ch The Exercise Objectives Get familiar with Android programming Emulator, debugging, deployment Learn to use UI elements and to design

More information

Android Using Menus. Victor Matos Cleveland State University

Android Using Menus. Victor Matos Cleveland State University Lesson 8 Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright 2008-2009 CommonsWare, LLC. ISBN: 978-0-9816780-0-9 & Android Developers http://developer.android.com/index.html

More information

Android. Applications Activities Interface

Android. Applications Activities Interface Android Applications Activities Interface Applications Processes Usually each application, including all its components, runs in a single process; the Application object is always in memory Priorities

More information

Distributed Systems Assignment 1

Distributed Systems Assignment 1 Distributed Systems Assignment 1 Marian.george@inf.ethz.ch Distributed Systems Assignment 1 1 The Exercise Objectives Get familiar with Android programming Emulator, debugging, deployment Learn to use

More information

Software Engineering Large Practical: Preferences, storage, and testing

Software Engineering Large Practical: Preferences, storage, and testing Software Engineering Large Practical: Preferences, storage, and testing Stephen Gilmore (Stephen.Gilmore@ed.ac.uk) School of Informatics November 9, 2016 Contents A simple counter activity Preferences

More information

Basic UI elements: Defining Activity UI in the code. Marco Ronchetti Università degli Studi di Trento

Basic UI elements: Defining Activity UI in the code. Marco Ronchetti Università degli Studi di Trento 1 Basic UI elements: Defining Activity UI in the code Marco Ronchetti Università degli Studi di Trento UI Programmatically public class UIThroughCode extends Activity { LinearLayout llayout; TextView tview;

More information

Managing Data. However, we'll be looking at two other forms of persistence today: (shared) preferences, and databases.

Managing Data. However, we'll be looking at two other forms of persistence today: (shared) preferences, and databases. Managing Data This week, we'll be looking at managing information. There are actually many ways to store information for later retrieval. In fact, feel free to take a look at the Android Developer pages:

More information

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) Lecture 3: Android Life Cycle and Permission Android Lifecycle An activity begins its lifecycle when entering the oncreate() state If not

More information

Starting Another Activity Preferences

Starting Another Activity Preferences Starting Another Activity Preferences Android Application Development Training Xorsat Pvt. Ltd www.xorsat.net fb.com/xorsat.education Outline Starting Another Activity Respond to the Button Create the

More information

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps. 1 Action Bar Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps. 1) App Icon 3) Action Buttons 2)View Control 4) Action Overflows

More information

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) Lecture 3: Android Life Cycle and Permission Entire Lifetime An activity begins its lifecycle when entering the oncreate() state If not interrupted

More information

Android User Interfaces. Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

Android User Interfaces. Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan Android User Interfaces Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan chanhl@maili.cgu.edu.tw Basic control components Text components TextView EditText Button compoents

More information

Android framework overview Activity lifecycle and states

Android framework overview Activity lifecycle and states http://www.android.com/ Android framework overview Activity lifecycle and states Major framework terms 1 All Android apps contains at least one of the 4 components Activity (1) Building block of the UI.

More information

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { }

ActionBar. import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { } Android ActionBar import android.support.v7.app.actionbaractivity; public class MyAppBarActivity extends ActionBarActivity { Layout, activity.xml

More information

Android. Applications Activities Interface. Applications. Processes. Priorities. Activities

Android. Applications Activities Interface. Applications. Processes. Priorities. Activities Android Applications Activities Interface Applications Processes Usually each application, including all its components, runs in a single process; the Application object is always in memory Priorities

More information

Fragments. Lecture 11

Fragments. Lecture 11 Fragments Lecture 11 Situational layouts Your app can use different layouts in different situations Different device type (tablet vs. phone vs. watch) Different screen size Different orientation (portrait

More information

Introducing the Android Menu System

Introducing the Android Menu System Introducing the Android Menu System If you ve ever tried to navigate a mobile phone menu system using a stylus or trackball, you ll know that traditional menu systems are awkward to use on mobile devices.

More information

Practical 1.ListView example

Practical 1.ListView example Practical 1.ListView example In this example, we show you how to display a list of fruit name via ListView. Android Layout file File : res/layout/list_fruit.xml

More information

Mobile Development Lecture 10: Fragments

Mobile Development Lecture 10: Fragments Mobile Development Lecture 10: Fragments Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Elgayyar.weebly.com Situational Layouts Your app can use different layout in different situations: different device type

More information

Object-oriented programming on mobile devices. Professor : Laurent Mathy Teaching Assistant : Tom Barbette

Object-oriented programming on mobile devices. Professor : Laurent Mathy Teaching Assistant : Tom Barbette Object-oriented programming on mobile devices Professor : Laurent Mathy Teaching Assistant : Tom Barbette Learn by doing Android Developer website http://developer.android.com Online training http://developer.android.com/training

More information

B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau: Mã: package at.exam;

B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau: Mã: package at.exam; B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau: Mã: package at.exam; import java.util.arraylist; import android.app.activity; import android.app.alertdialog;

More information

Mobile User Interfaces

Mobile User Interfaces Mobile User Interfaces CS 2046 Mobile Application Development Fall 2010 Announcements Next class = Lab session: Upson B7 Office Hours (starting 10/25): Me: MW 1:15-2:15 PM, Upson 360 Jae (TA): F 11:00

More information

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar Design Patterns MSc in Computer Science Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

More information

Android Data Binding: This is the DSL you're looking for

Android Data Binding: This is the DSL you're looking for Android Data Binding: This is the DSL you're looking for Maksim Lin Freelance Android Developer www.manichord.com The plan Why Data Binding? Brief intro to Data Binding Library Adding Data Binding to an

More information

05. RecyclerView and Styles

05. RecyclerView and Styles 05. RecyclerView and Styles 08.03.2018 1 Agenda Intents Creating Lists with RecyclerView Creating Cards with CardView Application Bar Menu Styles and Themes 2 Intents 3 What is Intent? An Intent is an

More information

Lab 1 - Setting up the User s Profile UI

Lab 1 - Setting up the User s Profile UI Lab 1 - Setting up the User s Profile UI Getting started This is the first in a series of labs that allow you to develop the MyRuns App. The goal of the app is to capture and display (using maps) walks

More information

Building User Interface for Android Mobile Applications II

Building User Interface for Android Mobile Applications II Building User Interface for Android Mobile Applications II Mobile App Development 1 MVC 2 MVC 1 MVC 2 MVC Android redraw View invalidate Controller tap, key pressed update Model MVC MVC in Android View

More information

Open Lecture Mobile Programming. Intro to Material Design

Open Lecture Mobile Programming. Intro to Material Design Open Lecture Mobile Programming Intro to Material Design Agenda Introduction to Material Design Applying a Material Theme Toolbar/Action Bar Navigation Drawer RecyclerView CardView Support Design Widgets/Tools

More information

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial Revision history 90001431-13 Revision Date Description A October 2014 Original release. B October 2017 Rebranded the document. Edited the document.

More information

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input 1 UI Components 2 UI Components 3 A label is called a TextView. TextView TextViews are typically used to display a caption TextViews are not editable, therefore they take no input - - - - - - -

More information

Android Programming: More User Interface. CS 3: Computer Programming in Java

Android Programming: More User Interface. CS 3: Computer Programming in Java Android Programming: More User Interface CS 3: Computer Programming in Java Objectives Look at implementation of the UI from our tip calculator example Discuss dialogs Find out about fragments Revisiting

More information

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device 1 Software Practice 3 Today s lecture Today s Task Porting Android App. in real device Prof. Hwansoo Han T.A. Jeonghwan Park 43 INTENT 2 3 Android Intent An abstract description of a message Request actions

More information

Produced by. Mobile Application Development. Eamonn de Leastar

Produced by. Mobile Application Development. Eamonn de Leastar Mobile Application Development Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie A First

More information

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015 Developing Android Applications Introduction to Software Engineering Fall 2015 Updated 1st November 2015 Android Lab 3 & Midterm Additional Concepts No Class Assignment 2 Class Plan Android : Additional

More information

Distributed Systems Assignment 1

Distributed Systems Assignment 1 Distributed Systems Assignment 1 marian.george@inf.ethz.ch Distributed Systems Assignment 1 1 The Exercise Objectives Get familiar with Android programming Emulator, debugging, deployment Learn to use

More information

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml Android Application Written in Java (it s possible to write native code) Good

More information

@Bind(R.id.input_ ) EditText EditText Button _loginbutton;

@Bind(R.id.input_ ) EditText EditText Button _loginbutton; package cyborg.pantaucctv; import android.app.progressdialog; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view;

More information

LECTURE 08 UI AND EVENT HANDLING

LECTURE 08 UI AND EVENT HANDLING MOBILE APPLICATION DEVELOPMENT LECTURE 08 UI AND EVENT HANDLING IMRAN IHSAN ASSISTANT PROFESSOR WWW.IMRANIHSAN.COM User Interface User Interface The Android Widget Toolbox 1. TextView 2. EditText 3. Spinner

More information

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences ITU- FAO- DOA- TRCSL Training on Innovation & Application Development for E- Agriculture Shared Preferences 11 th - 15 th December 2017 Peradeniya, Sri Lanka Shahryar Khan & Imran Tanveer, ITU Experts

More information

Android: Intents, Menus, Reflection, and ListViews

Android: Intents, Menus, Reflection, and ListViews ,,, and,,, and Harvard University March 1, 2011 Announcements,,, and Lecture videos available at: https://www.cs76.net/lectures Section schedule: https://www.cs76.net/sections n-puzzle walkthrough: https://www.cs76.net/sections

More information

Fragments. Lecture 10

Fragments. Lecture 10 Fragments Lecture 10 Situa2onal layouts Your app can use different layouts in different situa2ons Different device type (tablet vs. phone vs. watch) Different screen size Different orienta2on (portrait

More information

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept. MAD ASSIGNMENT NO 2 Submitted by: Rehan Asghar BSSE 7 15126 AUGUST 25, 2017 SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept. Android Widgets There are given a lot of android widgets with simplified

More information

SAVING SIMPLE APPLICATION DATA

SAVING SIMPLE APPLICATION DATA 1 DATA PERSISTENCE OBJECTIVES In this chapter, you will learn how to persist data in your Android applications. Persisting data is an important topic in application development, as users typically expect

More information

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi ACTIVITY, FRAGMENT, NAVIGATION Roberto Beraldi Introduction An application is composed of at least one Activity GUI It is a software component that stays behind a GUI (screen) Activity It runs inside the

More information

Embedded Systems Programming - PA8001

Embedded Systems Programming - PA8001 Embedded Systems Programming - PA8001 http://goo.gl/ydeczu Lecture 9 Mohammad Mousavi m.r.mousavi@hh.se Center for Research on Embedded Systems School of Information Science, Computer and Electrical Engineering

More information

Vienos veiklos būsena. Theory

Vienos veiklos būsena. Theory Vienos veiklos būsena Theory While application is running, we create new Activities and close old ones, hide the application and open it again and so on, and Activity can process all these events. It is

More information

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu Announcements Group formation: Projects 2, 3 and final project will be

More information

Mobile Programming Lecture 10. ContentProviders

Mobile Programming Lecture 10. ContentProviders Mobile Programming Lecture 10 ContentProviders Lecture 9 Review In creating a bound service, why would you choose to use a Messenger over extending Binder? What are the differences between using GPS provider

More information

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables. 1 Thread A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables. Each virtual machine instance has at least one main

More information

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University User Interface: Layout Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University http://twitter.com/krunapon Agenda User Interface Declaring Layout Common Layouts User Interface

More information

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are: ETSN05, Fall 2017, Version 1.0 Software Development of Large Systems Lab 2 preparations Read through this document carefully. In order to pass lab 2, you will need to understand the topics presented in

More information

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java. Screen Slides References https://developer.android.com/training/animation/screen-slide.html https://developer.android.com/guide/components/fragments.html Overview A fragment can be defined by a class and

More information