more on user interface android applications. layouts linear layout relative layout table layout grid...

31
More on User Interface Android Applications

Post on 21-Dec-2015

254 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

More on User Interface

Android Applications

Page 2: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Layouts

• Linear Layout• Relative Layout• Table Layout• Grid View• Tab Layout• List View

Page 3: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Linear Layout<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="red" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="green" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout>

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="row one" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row two" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row three" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="row four" android:textSize="15pt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>

</LinearLayout>

Page 4: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Linear Layout

• 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.

Page 5: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Relative Layout• <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:"/> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /></RelativeLayout>

Page 6: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Relative Layout

• RelativeLayout is a ViewGroup that displays child View elements in relative positions.

• The position of a View can be specified as relative to sibling elements (such as to the left-of or below a given element) or in positions relative to the RelativeLayout area (such as aligned to the bottom, left of center).

Page 7: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Table Layout• <?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_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>

<TableRow> <TextView android:layout_column="1" android:text="Save..." android:padding="3dip" /> <TextView android:text="Ctrl-S" android:gravity="right" android:padding="3dip" /> </TableRow>

<TableRow> <TextView android:layout_column="1" android:text="Save As..." android:padding="3dip" /> <TextView android:text="Ctrl-Shift-S" android:gravity="right" android:padding="3dip" /> </TableRow>

<View android:layout_height="2dip" android:background="#FF909090" />

<TableRow> <TextView android:text="X" android:padding="3dip" /> <TextView android:text="Import..." android:padding="3dip" /> </TableRow>

<TableRow> <TextView android:text="X" android:padding="3dip" /> <TextView android:text="Export..." android:padding="3dip" /> <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" /> </TableRow>

<View android:layout_height="2dip" android:background="#FF909090" />

<TableRow> <TextView android:layout_column="1" android:text="Quit" android:padding="3dip" /> </TableRow></TableLayout>

Page 8: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Table Layout

• A layout that arranges its children into rows and columns.

• A TableLayout consists of a number of TableRow objects, each defining a row.

• TableLayout containers do not display border lines for their rows, columns, or cells.

• Each row has zero or more cells; each cell can hold one View object.

• A table can leave cells empty.

Page 9: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Grid View

• <?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center"/>

Page 10: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Grid View

• GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

• The grid items are automatically inserted to the layout using a ListAdapter.

Page 11: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Tab Layout• <?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout></TabHost>

Page 12: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Tab Layout

• To create a tabbed UI, you need to use a TabHost and a TabWidget.

• The TabHost must be the root node for the layout, which contains both the TabWidget for displaying the tabs and a FrameLayout for displaying the tab content.

• You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities.

Page 13: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

List View

A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

Page 14: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Programmatic Layout

• Can also define layout in Java:public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LinearLayoutll= new LinearLayout(this);Button button1 = new Button(this);button1.setText("Hello");Button button2 = new Button(this);button2.setText("World");ll.addView(button1);ll.addView(button2);setContentView(ll);}

• In practice, much easier to define XML layout

Page 15: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Activity Lifecycle

• Activities in the system are managed as an activity stack.

• When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

Page 16: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

An activity has essentially four states

• Active• Paused• Stopped• Inactive

• The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy().

Page 17: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View
Page 18: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onCreate()

• Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc.

• This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

• Always followed by onStart().

Page 19: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onStart()

• Called when the activity is becoming visible to the user.

• Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

Page 20: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onResume()

• Called when the activity will start interacting with the user.

• At this point your activity is at the top of the activity stack, with user input going to it.

• Always followed by onPause().

Page 21: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onPause()

• Called when the system is about to start resuming a previous activity.

• This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc.

• Implementations of this method must be very quick because the next activity will not be resumed until this method returns.

Page 22: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onStop()

• Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one.

• This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

Page 23: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onDestroy()

• The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.

Page 24: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

onRestart()

• Called after your activity has been stopped, prior to it being started again.

• Always followed by onStart()

Page 25: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Process Lifecycle

• The Android system attempts to keep application process around for as long as possible, but eventually will need to remove old processes when memory runs low.

• The system will kill less important processes (the last ones) before it resorts to killing more important processes (the first ones).

Page 26: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

foreground activity

• The foreground activity (the activity at the top of the screen that the user is currently interacting with) is considered the most important.

• Its process will only be killed as a last resort, if it uses more memory than is available on the device.

• Generally at this point the device has reached a memory paging state, so this is required in order to keep the user interface responsive.

Page 27: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

visible activity

• A visible activity (an activity that is visible to the user but not in the foreground, such as one sitting behind a foreground dialog) is considered extremely important and will not be killed unless that is required to keep the foreground activity running.

Page 28: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

background activity

• A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes.

• If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

Page 29: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

empty process

• An empty process is one hosting no activities or other application components (such as Service or BroadcastReceiver classes).

• These are killed very quickly by the system as memory becomes low. For this reason, any background operation you do outside of an activity must be executed in the context of an activity BroadcastReceiver or Service to ensure that the system knows it needs to keep your process around.

Page 30: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Service

• Sometimes an Activity may need to do a long-running operation that exists independently of the activity lifecycle itself.

• An example may be a camera application that allows you to upload a picture to a web site.

• To accomplish this, your Activity should start a Service in which the upload takes place.

Page 31: More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View

Service: Class Overview

• A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

• Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml.