90 likes | 189 Views
Cincinnati Android Developers Meetup Basic User Interface & Lifecycle 8/17/2011. Android Activity Lifecycle. More Info – http://developer.android.com/guide/topics/fundamentals/activities.html. Activities and Intents.
E N D
Cincinnati Android Developers MeetupBasic User Interface & Lifecycle8/17/2011
Android Activity Lifecycle More Info – http://developer.android.com/guide/topics/fundamentals/activities.html
Activities and Intents Two frequently used terms in android development are “Activity” and “Intent” Activities are stored in a stack, if the OS determines that it needs more memory it will kill an activity that is eligible A single activity usually just represents a single UI screen An intent is the application’s way of telling the OS that it wants to do something Most commonly, intents are used to bring some activity to the top of the stack, so that it is displayed to the user In order to move from one activity to another, startActivity() is called and an intent to start the desired activity is passed in Ex. startActivity(new Intent(this, NewActivity.class)); Note that “this” refers to the context. “this” will only work if you’re currently in the class scope that extends Activity If your scope is something else, like a ClickListener, use getBaseContext() instead of “this”
Basic Layouts There are a three main layouts that are most commonly used. RelativeLayout Allows child elements to position based on the position of other child elements or the parent layout_alignLeft, layout_below, layout_toTheRightOf, etc LinearLayout Specify an orientation, either vertical or horizontal Automatically stacks child elements in a single row/column based on order (top to bottom) Can use weight, which is similar to colspan, allows certain elements to take up more or less space than others AbsoluteLayout Allows the children to specify exact locations with x and y coordinates Much less flexible and harder to maintain than other layouts
Basic Elements Elements are created in an xml file within the layout folder, or can be dynamically created in the activity class and displayed on screen. Any elements created in the xml file can be referenced and changed by the activity class by using the R class The R class is unique to each application, and basically maintains references to all elements that have an id specified To grab a reference to the element, we use findViewById() and pass in the id of the desired element All elements MUST specify a layout_height and layout_width You can either use a numeric value or use the predefined “wrap_content” or “fill_parent” values Some common elements, and their similar HTML identities TextView (span), ImageView (img), ScrollView (div w/scrollbar), ListView (ul), EditText (text input), Button (submit input), etc Laying these elements out depends on the type of layout their parent has
Menus Each screen can have one main menu that’s called when the user hits the menu key A menu consists of items, which can be text and/or an image The menu item layout is defined in a separate xml file, commonly added to a “menu” folder in the res directory Each item gets an id assigned to it A single method in the activity class handles all menu items, in order to determine which menu item was clicked, we use a switch statement Two methods need to be added to an activity class in order for it to use a menu public booleanonCreateOptionsMenu(Menu menu) – This method will inflate the xml layout public booleanonOptionsItemSelected(MenuItem item) – This method handles all clicks on the menu, this is where you switch statement goes
Application Preferences Creating, using, and saving application preferences is super easy Create a new folder named “xml” in the res directory if it doesn’t already exist, then create an xml file called preferences.xml Below are a couple element types that are valid in this file, there are many more available to use PreferenceCategory – Creates a header and is used to group similar preferences together EditTextPreference – Creates a preference item that, when clicked, brings up a text field editor Create a class that extends PreferenceActivity In the onCreate method, you just need to call addPreferencesFromResource() and pass in the preferences.xml file we just created Any data entered or updated on this screen is now automatically saved for this application and reloaded even if the application is closed
Lists This is a special type of activity that displays a scrollable list on the screen Your activity class must implement ListActivity, and you must override a new method onListItemClick() – called when one of the list items is clicked, listView.getItemAtPosition() is used to determine which item was clicked A new layout xml file will be created, we’ll call it row.xml for now This will dictate what elements to use when displaying a single list item Each item in the list will re-use this layout for displaying the data specific to that item In addition to the new method, you will also create a custom ListAdapter inner class This adapter will be specific to the type of object displayed in the list The adapter class will extend ArrayAdapter<[List Object]> Within the adapter class, you’ll need to create a constructor and two other methods public intgetCount() – will return the number of items in the list at any given time public View getView() – this method is called when the adapter is determining what to display on the screen for each item in the list. This is where your row.xml is inflated
Next Meeting Wednesday September 21, 2011 Cool Apps Cut the Rope