Changes

Jump to: navigation, search

MAP524/DPS924 Lecture 3

1,198 bytes added, 18:01, 18 July 2015
IDs
** Easiest way (if available) is to register the event handler via the layout xml file.
* '''Layout''': We'll look at several types of layouts in detail next week.
* '''Fragment''': created using an XML layout file that looks like a sub-layout and behaves like a sub-activity. We'll look at fragments next week.
* '''Content Provider''': One way for an application to provide access to data it holds. It's one form of IPC usually used for more standard types of data. See a list of tutorials at the [http://developer.android.com/guide/topics/providers/content-providers.html bottom of this page]. We'll also look at them more closely after the break.
* '''Menu''': Used to be a critical part of any android application until the menu button's been removed from Android tablets and later phones. Now is still used but isn't as prevalent.
Making sure that your user experience is still nice despite that you need to navigate the diagram above to figure out when to load, unload, save, reload both data and the state of the UI.
 
== IDs ==
 
IDs are used as a way to connect the elements defined in XML with your java source code. In XML for nearly every element you can specify an "id" attribute. For exmaple:
 
<source lang="xml"><Button android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/></source>
 
You will rarely if ever change the "@+id/" prefix, but after the slash you can set whatever ID you like. Normally you want something that will describe what that element is and does, for example "sendEmailNowBtn". Otherwise your code will be harder to read ("what my_button, they're all my buttons).
 
To get a reference to the button in your Java code you use findViewById() with a cast. In this example:
 
<source lang="java">Button myButton = (Button) findViewById(R.id.my_button);</source>
 
findViewById looks up the element by ID (my_button). Remember the R class is automatically generated based on your XML files.
 
The return is a View, but we know it's a button so we'll cast it to Button. Without a case you'd only be able to use te View methods on the button. Button is obviously a subclass of View.

Navigation menu