Difference between revisions of "MAP524/DPS924 Lecture 4"

From CDOT Wiki
Jump to: navigation, search
(RelativeLayout)
(Views)
Line 40: Line 40:
 
* NumberPicker: Lets the user choose a number. Needs to be configured in java before it works to have a minimum and maximum value using setMinValue() and setMaxValue().
 
* NumberPicker: Lets the user choose a number. Needs to be configured in java before it works to have a minimum and maximum value using setMinValue() and setMaxValue().
 
* Spinner: Like the number picker but with any strings as values to pick from. You'll need an Adapter similar to the one you used with the AutoCompleteTextView in the last lab, otherwise it will show nothing.
 
* Spinner: Like the number picker but with any strings as values to pick from. You'll need an Adapter similar to the one you used with the AutoCompleteTextView in the last lab, otherwise it will show nothing.
 +
 +
= Fragments =
 +
 +
Were introduced with 3.0 and made things much more complicated. But if you're developing for phones and tablets they actually make things easier.
 +
 +
Sometimes (like with the date picker) you'll have a hard time to avoid using a fragment, but almost always you can avoid writing your own fragment if you want to.

Revision as of 20:48, 12 July 2015

Layouts

Layouts are a way to arrange your widgets on the screen semi-automatically, so that your app looks normal regardless of screen size and resolution, android version, theme in use, font size, etc.

In most cases you will not be referencing your layout from code, since Android will handle everything you'd expect a layout manager to do. It's the contents of the layout that you care about.

No matter which layout you choose to use at first it will take you time to figure out the combination of android:layout_* attributes to make it look exactly as you want. We'll try a few experiments in class today, and you should spend a few hours playing with them also.

LinearLayout

Puts all its contents in one line: either horisontal or vertical. Though very simple this can be used to create nearly any kind of layout you like, especially since you can put one layout inside another one.

You can use layout_weight to decide how to allocate space to each View in the layout. If only 1 View has a layout weight then it will automatically fill the rest of the LinearLayout in that dimension.

Scrolling

In order to make the contents of the LinearLayout scrollable you have to do something a little strange: you make the layout a child of a ScrollView. Android will handle everything else, including creating and showing/hiding the scroll bar.

RelativeLayout

The default layout but it's quite a bit more complicated than LinearLayout. Instead of everything being in one line, every View in this layout is arranged relative to some other view (or a side of the layout).

For example if you have two buttons you can arrange Button1 to align with the layout it's in using android:layout_alignParent* and align Button2 under Button1 using android:layout_below. See the XML Attributes section of RelativeLayout.LayoutParams

This layout also can be used to create pretty much any imaginable layout, but I find it much harder to maintain.

Views

Depending on how advanced your application is you may need to make your own views, but in most cases you'll be able to work with the existing ones.

All these views are usually defined in your layout XML file, and most of them will be controlled in some way from code. In the last lecture we saw how to get a reference to an XML-defined element in your java code.

  • TextView: Used to show text. Can change attributes like the text size and colour. Can't be used for complex text but can be made to contain clickable links.
  • Button: obvious enough.
  • CheckBox: obvious enough. Can be [un]checked from code at any time.
  • RadioButton: When you want it to work like a real radio button you need to add more than one to a RadioGroup. See the documentation.
  • EditText: Like the AutoCompleteTextView you used in the last lab, but without the auto complete feature. Can be made read only, be restricted to one line, and using the addTextChangedListener callback you can do very interesting things. Also note by changing android:inputType you can change the keyboard that's presented to the user.
  • ImageView: Show an image from your resources (or the Android resources). Use the an android:src attribute to set the source of the image.
  • ProgressBar: By default isn't actually a bar, it's a spinning wheel. To make real use of it you really need something running in the background (a thread or AsyncTask). See the documentation.
  • NumberPicker: Lets the user choose a number. Needs to be configured in java before it works to have a minimum and maximum value using setMinValue() and setMaxValue().
  • Spinner: Like the number picker but with any strings as values to pick from. You'll need an Adapter similar to the one you used with the AutoCompleteTextView in the last lab, otherwise it will show nothing.

Fragments

Were introduced with 3.0 and made things much more complicated. But if you're developing for phones and tablets they actually make things easier.

Sometimes (like with the date picker) you'll have a hard time to avoid using a fragment, but almost always you can avoid writing your own fragment if you want to.