Android - Interface and Layout: L. Grewe
Android - Interface and Layout: L. Grewe
LAYOUT
L. Grewe
Android GUI –the concept
GUI = hierarchy of View and ViewGroup objects
---specify Layout
android.widget.TableLayout
android.widget.RelativeLayout
android.widget.FrameLayout
android.widget.ScrollLayout
other
ListView: a list of scrollable items
scrollable grid
Interfaces: Two Alternatives
for creation: Code or
XML
You have two ways you can create the
interface(s) of your Application.
1. Code = write code using SDK with
classes like LinearLayout, TextView, ……
Here is code
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
of
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Login"
android:id="@+id/textView_TopLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout created
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="login"
android:id="@+id/textView_login" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_Login" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="password"
android:id="@+id/textView_password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_Password" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button_Enter" />
</LinearLayout>
XML Interface tags
Besides drag and
drop you can edit
the xml file
directly….you will
see examples
throughout this
lecture
Lets return to looking at some
of the possible ViewGroup
Layouts
Layout Options ---there are
a number
Determines how the layout is structured.
LinearLayout
A Layout that arranges its children in a single column or a single
row. The direction of the row can be set by calling
setOrientation(). You can also specify gravity, which specifies
the alignment of all the child elements by calling setGravity() or
specify that specific children grow to fill up any remaining space
in the layout by setting the weight member of
LinearLayout.LayoutParams. The default orientation is
horizontal.
RelativeLayout
FrameLayout
LinearLayout RelativeLayout GridLayout
GridLayout AND MANY MORE (see children of ViewGroup in
API)
LinearLayout
Some Examples
LinearLayout
Good:
Simple
Know exactly how it will look on every device
Bad:
Well for many interfaces too simple….
BUT see next slide
BUT, REMEMBER you can have a ViewGroup
(another Layout) inside as a member of the
LinearLayout to make a more COMPLEX interface
ALSO can make more coplex
LinearLayout Very SIMPLE
Example
arranges by single column (vertical orientation)
<Text View
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“@string/hello”/>
</LinearLayout>
VERY simple example – LinearLayout with one
child View object, a TextView saying Hello….
LinearLayout Example 2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent“
android:orientation="vertical" >
<Button android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Web Browser“
android:onClick="onClickWebBrowser" />
<Button android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content“
android:text="Make Calls"
android:onClick="onClickMakeCalls" />
<Button android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content“
android:text="Show Map"
android:onClick="onClickShowMap" />
<Button android:id="@+id/btn_launchMyBrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch My Browser"
LinearLayout with 4 child View objects,
android:onClick="onClickLaunchMyBrowser" />
Xml
android:orientation=“vertical”
2
TextViews
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Another Nested LinearLayou
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
Example <LinearLayout
android:layout_height="match_parent" <TextView android:layout_width="match_p
arent"
android:layout_margin="10dp" android:layout_width="wrap_conten
t" android:layout_height="wrap_c
android:orientation="vertical"> ontent"
android:layout_height="wrap_conte
<TextView nt" android:layout_marginTop="10d
android:layout_width="wrap_content" android:layout_marginTop="20dp" p"
TableLayout
<TableRow>
<TextView
android:text="@string/table_layout_4_save"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_save_shortcut" This Table has 2 Rows
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
TableLayout example 2
Here use gravity to move the 2nd item in
row to the right
ONLY partial XML code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Open..."
android:padding="3dip" />
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip" />
</TableRow>
speed:
Measure: 0.977ms
Layout: 0.167ms
speed:
Measure: 0.598ms
Layout: 0.110ms
Draw: 2.717ms Draw: 2.146ms
RelativeLayout is FASTER
More on improving
performance
Go to Developer site and search on
“Improving Layout Performance”
https://developer.android.com/training/i
mproving-layouts/index.html
Related Layout Tags
to create a DYNAMIC layout contents –one
where the contents are dynamic (maybe read
in from database or???)
Subclasses of AdapterView
SOME Examples of Layout Tags
that can load contents/data
dynamically
ListView Gallery GridView
Attributes
android:animationDuration setAnimationDuration(int) Sets how
long a transition animation should run (in milliseconds) when
layout has changed.
android:gravity setGravity(int) Specifies how to place the content
of an object, both on the x- and y-axis, within the object itself.
android:spacing setSpacing(int)
android:unselectedAlpha setUnselectedAlpha(float) Sets the alpha
on the items that are not selected.
See our website,
book and developer
Code—setting up Gallery website for more
detailed explanation
of Gallery
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //use xml file that contain a
<Gallery>
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
Toast.makeText(HelloGallery.this, "" + position,
Toast.LENGTH_SHORT).show();
}
}); We need a “HANDLE” to the Gallery
} we created from XML in our Java code to
handle events when an item in Gallery is
clicked on
How to populate the
GridView, or Gallery or
ListView
Look on our class website for more
detailed examples (too much code to put
into powerpoint) or look in your book or
at developer website or search on
google for tutorials
This is a power point “beginning” lecture on Layout and we can’t
cover all the many and elaborate examples --- that is best served by
websites and tutorials and books
Note there is also Absolute
Layout
You position View widgets exactly
where you want them.
It is DEPRECATED