Title: PowerPoint Template
1Programming with Android Introduction
Programming with Android Layouts
Luca Bedogni Marco
Di Felice Dipartimento di
Scienze dellInformazione
Università di Bologna
2Views outline
- Main difference between a Drawable and a View is
reaction to events - Could be declared in an XML file
- Could also be declared inside an Activity
- Every view has a unique ID
- Use findViewById(int id) to get it
- Views can be customized
2
3Some useful methods
- getLeft()
- getTop()
- getMeasuredWidth()
- getMeasuredHeight()
- getWidth()
- getHeight()
- requestLayout()
- invalidate()
3
4ViewGroup and layout
- ViewGroup is a view container
- It is responsible for placing other views on the
display - Every layout must extend a ViewGroup
- Every view needs to specify
- androidlayout_height
- androidlayout_width
- A dimension or one of match_parent or
wrap_content
4
5Layouts
- Some layouts are pre-defined by Android
- Some of these are
- LinearLayout
- RelativeLayout
- TableLayout
- FrameLayout
- AbsoluteLayout
- A layout could be declared inside another layout
5
6LinearLayout
- Dispose views on a single row or column,
depending on androidlayout_orientation - The orientation could also be declared via
setOrientation(int orientation) - orientation is one of HORIZONTAL or VERTICAL
- Has two other attributes
- gravity
- weight
6
7LinearLayout
lt?xml version"1.0" encoding"utf-8"?gt ltLinearLayo
ut xmlnsandroid"http//schemas.android.com/apk/r
es/android" androidlayout_width"fill_parent"
androidlayout_height"fill_parent"
androidorientation"vertical" gt lt!-- Also
horizontal --gt ltButton
androidid"_at_id/button1"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString1" /gt
ltButton androidid"_at_id/button2"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString2"
/gt lt/LinearLayoutgt
7
8LinearLayout
8
9LinearLayout
lt?xml version"1.0" encoding"utf-8"?gt ltLinearLayo
ut xmlnsandroid"http//schemas.android.com/apk/r
es/android" androidlayout_width"fill_parent"
androidlayout_height"fill_parent"
androidorientation"vertical" gt ltButton
androidid"_at_id/button1"
androidlayout_width"match_parent"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString1" /gt
ltButton androidid"_at_id/button2"
androidlayout_width"wrap_content"
androidlayout_height"match_parent"
androidtext"_at_string/buttonString2"
/gt lt/LinearLayoutgt
9
10LinearLayout
10
11LinearLayout weight
lt?xml version"1.0" encoding"utf-8"?gt ltLinearLayo
ut xmlnsandroid"http//schemas.android.com/apk/r
es/android" androidlayout_width"fill_parent"
androidlayout_height"fill_parent"
androidorientation"horizontal" gt ltButton
androidid"_at_id/button1"
androidlayout_width"match_parent"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString1"
androidlayout_weight"1" /gt ltButton
androidid"_at_id/button2"
androidlayout_width"match_parent"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString2"
androidlayout_weight"2"
/gt lt/LinearLayoutgt
11
12LinearLayout weight
12
13LinearLayout gravity
lt?xml version"1.0" encoding"utf-8"?gt ltLinearLayo
ut xmlnsandroid"http//schemas.android.com/apk/r
es/android" androidlayout_width"fill_parent"
androidlayout_height"fill_parent"
androidorientation"horizontal" gt ltButton
androidid"_at_id/button1"
androidlayout_width"match_parent"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString1"
androidlayout_weight"1" /gt ltButton
androidid"_at_id/button2"
androidlayout_width"match_parent"
androidlayout_height"wrap_content"
androidtext"_at_string/buttonString2"
androidlayout_weight"2"
androidlayout_gravity"center_vertical"
androidgravity"topcenter" /gt
lt/LinearLayoutgt
13
14LinearLayout gravity
14
15LinearLayout problem
15
16RelativeLayout
- Disposes views according to the container or
according to other views - The gravity attribute indicates what views are
more important to define the layout - Useful to align views
16
17RelativeLayout
lt?xml version"1.0" encoding"utf-8"?gt ltRelativeLa
yout xmlnsandroid"http//schemas.android.com/apk
/res/android" androidlayout_width"match_pare
nt" androidlayout_height"match_parent" gt
ltEditText androidid"_at_id/username"
androidtext"username"
androidinputType"text"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidlayout_alignParentRight"true"
androidlayout_toRightOf"_at_id/usernameLabel" gt
lt/EditTextgt ltTextView
androidid"_at_id/usernameLabel"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidlayout_alignBaseline"_at_id/username"
androidtext"Username" /gt
17
18RelativeLayout
ltEditText androidid"_at_id/password"
androidtext"password"
androidinputType"textPassword"
androidlayout_below"_at_id/username"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidlayout_alignLeft"_at_id/username"
androidlayout_alignParentRight"true"
androidlayout_toRightOf"_at_id/usernameLabel" gt
lt/EditTextgt ltTextView
androidid"_at_id/passwordLabel"
androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidlayout_alignBaseline"_at_id/password"
androidtext"Password" /gt lt/RelativeLayoutgt
18
19RelativeLayout
19
20TableLayout
- As the name say, similar to a Table
- Has some attributes to customize the layout
- androidlayout_column
- androidlayout_span
- androidstretchColumns
- androidshrinkColumns
- androidcollapseColumns
- Each row is inside a ltTableRowgt element
20
21TableLayout
lt?xml version"1.0" encoding"utf-8"?gt ltTableLayou
t androidlayout_width"fill_parent" androidlayo
ut_height"fill_parent" xmlnsandroid"http//sche
mas.android.com/apk/res/android" androidid"_at_id/
tableLayout"gt ltTableRow androidlayout_width
"wrap_content" androidlayout_height"wrap_content
androidid"_at_id/firstRow"gt
ltButton androidid"_at_id/button1 androidlayo
ut_width"wrap_content androidlayout_height"
wrap_content androidtext"Button" /gt
ltButton androidid"_at_id/button2
androidlayout_width"match_parent"
androidlayout_height"match_parent"
androidtext"Button" /gt ltButton
androidid"_at_id/button3"
androidlayout_width"match_parent"
androidlayout_height"match_parent"
androidtext"Button" /gt lt/TableRowgt
21
22TableLayout
ltTableRow androidlayout_width"wrap_con
tent" androidlayout_height"wrap_conten
t" androidid"_at_id/secondRow"gt
ltButton androidlayout_column"1" android
layout_span"2" androidid"_at_id/button4
" androidlayout_width"wrap_content"
androidlayout_height"wrap_content"
androidtext"Button"gt
lt/Buttongt lt/TableRowgt lt/TableLayoutgt
22
23TableLayout
23
24FrameLayout and AbsoluteLayout
- FrameLayout
- Adds an attribute, androidvisibility
- Makes the user able to define layouts managing
the visibility of views - AbsoluteLayout
- Deprecated
- Specify position with x and y
- Pay attention to different resolutions
24
25Adapters
- Used to visualize data
- Make a ViewGroup to interact with data
- Some methods
- isEmpty()
- getItem(int position)
- getCount()
- getView()
25
26AdapterView
- A ViewGroup subclass
- Its subchilds are determined by an Adapter
- Some subclasses
- ListView
- GridView
- Spinner
- Gallery
26
27ListView example
public class HelloAndroidActivity extends
Activity _at_Override public void
onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState)
setContentView(R.layout.list) String
data "First", "Second", "Third"
ListView lv (ListView)findViewById(R.id.list)
lv.setAdapter(new
ArrayAdapterltStringgt(this, android.R.layout.simple
_list_item_1, data)) lt?xml
version"1.0" encoding"utf-8"?gt ltListView
xmlnsandroid"http//schemas.android.com/apk/res/
android" androidlayout_width"match_parent"
androidlayout_height"match_parent"
androidorientation"vertical"
androidid"_at_id/list" /gt
27
28ListView
28
29Other views/adapters
- Spinner, selection of multiple items
- Gallery, images
- ExpandableListView, list with hidden values
- TabWidget, tabbed layouts
29