1 / 69

Mobile Programming Lecture 4

Mobile Programming Lecture 4. Resources, Selection, Activities, Intents . Announcement. Reminder: Homework 2 is due this Friday 11:59pm. You need to demo to me in class. The second deadline is Sunday 11:59pm. You need to demo to me in Monday’s class.

ronna
Download Presentation

Mobile Programming Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mobile Programming Lecture 4 Resources, Selection, Activities, Intents

  2. Announcement • Reminder: • Homework 2 is due this Friday 11:59pm. You need to demo to me in class. • The second deadline is Sunday 11:59pm. You need to demo to me in Monday’s class. • Please export your project (.zip) and email me. • Midterm exam is on June 21st. It is a Friday.

  3. Lecture 3 Review • Why shouldn't you use Toast to debug? • How should you debug?

  4. Agenda Referencing @string resources • Setters and Getters • Selection Views • Android Components - Activity • Intents • More on the Android Manifest File

  5. Referencing @string Resources All @string Resources are stored in res/values/strings.xml You reference them in XML as "@string/string_name"

  6. Referencing @string Resources You can also reference them in Java String buttonText = ""; Resources res = getResources(); buttonText = res.getString(R.string.string_name);

  7. Referencing Resources You can store an array of strings if you need a collection instead of just a single string. This makes it an @array resource instead of @string resource! • Open res/values/strings.xml in Resources view • Add ... > String Array • enter the name of the string_array, e.g. "countries" • Repeat until you've added all values for the collection ... • Add ... > Item • Enter the name of the item

  8. Setters and Getters for Views Most of a View's attributes can be set and get programmatically

  9. Selection Widgets - Spinner Similar to a drop-down list • android:spinnerMode • "dialog" or "dropdown" • android:prompt • e.g. "Select an option" • message to display when the spinner dialog is shown • only when android:spinnerMode="dialog" • android:entries • e.g. "@array/countries" • populate the spinner using this array

  10. Selection Widgets - Spinner Similar to a drop-down list • android:prompt • e.g. "Select an option" • message to display when the spinner dialog is shown • android:spinnerMode • "dialog" or "dropdown" • android:entries • e.g. "@array/countries" • populate the spinner using this array If the list is static, you can again avoid Java by specifying the entries in XML!

  11. Selection Widgets - Spinner Similar to a drop-down list • android:prompt • e.g. "Select an option" • message to display when the spinner dialog is shown • android:spinnerMode • "dialog" or "dropdown" • android:entries • e.g. "@array/countries" • populate the spinner using this array If you don't like XML, you can set the entries in Java also.

  12. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); }

  13. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is the Java code. If you choose to do this, then you don't need to set the android:entries attribute

  14. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } You should already know what this does.

  15. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } Since you don't like XML, to do it programmatically, you need to use an Adapter.

  16. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is Java stuff. If you don't know what it is, try removing it to see what happens.

  17. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } If you have an @array resource already, then create an ArrayAdapter from that resource.

  18. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } First argument is of type Context, you can just past the "this" keyword here (because of the state of app)

  19. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } This is the resource that you're creating the ArrayAdapter from.

  20. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } 3rd argument expects the @id of a TextView.

  21. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } android.R gives you resources that exist for use even before you create your app

  22. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } So instead of creating and using your own TextView for this purpose, use this one

  23. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); mySpinner.setAdapter(adapter); } Attach the Adapter to the Spinner in order to populate the Spinner!

  24. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); mySpinner.setAdapter(adapter); } Finally, this line of code is not required, but it makes your drop down look pretty.

  25. Selection Widgets - Spinner Both ways give you a Spinner which uses predetermined entries How do you populate the Spinner at runtime, with when the entries are also determined at runtime?

  26. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); }

  27. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } Create and initialize a String array

  28. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } Create an ArrayAdapter to populate the Spinner with

  29. Selection Widgets - Spinner public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.my_spinner); String[] countries = new String[]{"Brazil","China","Denmark"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries); mySpinner.setAdapter(adapter); } 3rd argument accepts a List of Strings

  30. Selection Widgets - ListView A ListView is similar to a Spinner • they both show the user a list of items • they both have the android:entries attribute, so they can both be set using the same @array resource or the same ArrayAdapter

  31. Selection Widgets - ListView • Adapters are used to provide the data to the ListView object. • The adapter also defines how each row is the ListView is displayed. • You can use pre-existing layout for each row. • You can also define customized layout for each row. • Extends BaseAdapter class.

  32. Selection Widgets - ListView A ListView is similar to a Spinner with some exceptions, to name a few: • A Spinner is normally used for forms, a ListView not so much • A Spinner only shows one item at a time • A Spinner item can be "selected", a ListView item can be "clicked" or "selected"

  33. Selection Widgets - ListView A ListView is similar to a Spinner with some exceptions, to name a few: • A Spinner is normally used for forms, a ListView not so much • A Spinner only shows one item at a time • A Spinner item can be "selected", a ListView item can be "clicked" or "selected" I added this in hindsight. On non-trouch-screen devices it can be selected

  34. Events - Spinner OnItemSelected You can set an EventListener for when the user selects an item from the Spinner

  35. Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  36. Events - Spinner OnItemSelected Setter method for setting the EventListener mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  37. Events - Spinner OnItemSelected Anonymous class mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  38. Events - Spinner OnItemSelected When an item is selected ..., mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  39. Events - Spinner OnItemSelected You can now reference the actual View (i.e., each entry in Spinner is a TextView) mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  40. Events - Spinner OnItemSelected The position of the view in the Adapter mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  41. Events - Spinner OnItemSelected The android:id attribute of the view mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } });

  42. Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } }); Cast the generic View to a TextView

  43. Events - Spinner OnItemSelected mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() , Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(getApplicationContext(), "You didn't select anything" , Toast.LENGTH_SHORT).show(); } }); Get the android:text value

  44. Events - Spinner OnItemSelected • Note that this we're using the OnItemSelectedListener instead of the OnItemClickListener for Spinner! • If you try to use OnItemClickListener you may get errors • Also, note the difference between these two listeners and OnClickListener • OnItemClickListener • an entry in the Spinner has been clicked, don't use this • OnClickListener • the Spinner has been clicked • OnItemSelectedListener • an entry in the Spinner has been selected

  45. Events - ListView OnItemClick As mentioned before, a ListView item can be clicked, or selected In that case, you use OnItemClickListener for ListView or OnItemSelected Listener

  46. Events - ListView OnItemClick myListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } });

  47. Android Application Components So far we've only been working with 1 of the 4 Android components, namely Activity

  48. Android Application Components The 4 Android application components are • Activity • Broadcast Receiver • Content Provider • Service

  49. Activity So far we've only been working with a single Activity Remember, an Activity is a single screen with a user interface Each Activity in an application is independent

  50. Activity An Activity is the only Android application component with a UI!

More Related