Basic GUI programming. Listviews & Adapters.

Quick Review

  1. What is Log and what are the available priorities?

  2. What is a breakpoint? What can you do with it?

  3. Some activity lifecycle methods are ... ?

  4. What is an intent? What has an intent?

  5. What is an intent-filter?

  6. How is Android matching the intents?

Basic GUI programming

Components in Android

  • View - simple GUI elements

  • ViewGroup - a group of Views which can contain other ViewGroups

How can you create a component?

Programmatically

XML

<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
new TextView(
    new LayoutParams(MATCH_PARENT, 
                     MATCH_PARENT))

Components attributes

Important

  • layout_width - required
  • layout_height - required
  • layout_<something> - defines layout properties
  • ID - referenced as int
    • @ - to expand the rest of the string and identify it as a resource
    • + means that it is a new resource and must be created
    • android has built-in ids- 
      @android:id/empty
  • gravity vs. layout_gravity - inside vs. outside

Layout Params (layout_<something>)

wrap_content vs match_parent

What is a View?

A view has a location (top and left coordinates) and dimensions (width and height).

View Dimensions

  • dp - Density-independent Pixels - An abstract unit that is based on the physical density of the screen.
  • sp - Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference.
  • pt
  • px
  • mm
  • in

Containers

LinearLayout

RelativeLayout

WebView

FrameLayout

Linear vs Relative

+ layout_weight

+ vertical/horizontal

+ faster performance

 

- low flexibility

+ great flexibility

+ overlay with ease

+ support for different screens

 

- lots of ids declaration

- slow performance

Input controls

Buttons

EditText

Checkboxes

Toggle

Radio

Spinner

Pickers

Something more about controls

  • inputType - phone
    • bitwise operations
  • imeOptions - actions to be done
  • imeLabel
  • hint
  • AutoCompleteTextView

EditText

ToggleButton

  • onCheckedChangeListener
  • onToggleListener

Toast - another way for debugging

new Toast(context, "Test", Toast.LENGHT_SHORT)
.show()

Let's code!

Break (10 mins)

ListViews & Adapters

What is a ListView?

  • Sublclass of AdapterViewpopulate the layout with views at runtime.

  • Needs an Adapter to populate the components

  • The adapter is a middleman between the data source and the AdapterView layout - it adapts the data and creates each listview entry

ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myStringArray);
  • Context
  • XML layout for a single row
  • List of data

To create an adapter you need:

Let's try it!

Custom Adapters

  • extend BaseAdapter - SOURCE
  • override getView
  • override getItem
  • override getCount
  • override getItemId

Reusing views to get a custom look

Let's try it!

Just replace ListView with GridView

  • Determine columns count
  • Determine cell size
  • More scalable than ListView
  • Cells better than rows
  • Takes more screen space

Let's try it!

Summary

  1. What is a View and ViewGroup?

  2. How can you create components?

  3. Layout attributes

  4. Containers and input controls

  5. What are different dimensions (dp vs px vs pt ...)?

  6. What is a ListView?

  7. What is an adapter?

  8. What is a GridView?

Lecture 11 - Basic GUI

By naughtyspirit

Lecture 11 - Basic GUI

  • 457