User Interface Android Applications. Activities An activity presents a visual user interface. Each...

33
User Interface Android Applications
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    2

Transcript of User Interface Android Applications. Activities An activity presents a visual user interface. Each...

Page 1: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

User Interface

Android Applications

Page 2: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Activities

• An activity presents a visual user interface.• Each activity is given a default window to draw

in.• The visual content of the window is provided

by a hierarchy of views.• Each view controls a particular rectangular

space within the window.

Page 3: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Hierarchy of Views

• Parent views contain and organize the layout of their children.

• Leaf views (those at the bottom of the hierarchy) draw in the rectangles they control and respond to user actions directed at that space.

Page 5: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

View Group

• A ViewGroup is a special view that can contain other views (called children.)

• The view group is the base class for layouts and views container

• Some layout paramemeters:fill_parent, match_parent, wrap_content

Page 6: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Widgets

• The View class serves as the base for subclasses called "widgets," which offer fully implemented UI objects, like text fields and buttons.

• A widget is a View object that serves as an interface for interaction with the user.

Page 7: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Widgets: Ready-made views in Android

• buttons, • text fields, • scroll bars, • menu items, • check boxes,• more

Page 8: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

src/[package name]/[activity name].java

package com.example.helloandroid;

import android.app.Activity;import android.os.Bundle;

public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}

Notice that the class is based on the Activity class.

Page 10: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Declaring Layout

• Your layout is the architecture for the user interface in an Activity. It defines the layout structure and holds all the elements that appear to the user.

• You can declare your layout in two ways:– Declare UI elements in XML– Instantiate layout elements at runtime

Page 11: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Declare UI elements in XML• Android provides a straightforward XML vocabulary

that corresponds to the View classes and subclasses, such as those for widgets and layouts.

<?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" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>

Page 12: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World,

HelloAndroid!</string> <string name="app_name">Hello, Android</string></resources>

Page 13: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Hello World, HelloAndroid!

Page 14: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Instantiate layout elements at runtime

• Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

Page 15: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

src/[package name]/[activity name].java

package com.example.helloandroid;

import android.app.Activity;import android.os.Bundle;import android.widget.TextView;

public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello World, HelloAndroid!"); setContentView(tv); }}

Page 16: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Application Resources

• Common files in res/ directory:– drawable/icon.png: Icon for the program in

launcher– layout/main.xml: User interface for main Activity– values/strings.xml: Any Strings appearing in UI

• Resources –separate program logic from “other stuff”Strings, images, UI layouts

Page 17: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

res/layout/main.xml<?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" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>

Page 18: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Hello Android

Page 19: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

XML files

• The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior.

• Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile.

Page 20: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Modified main.xml (1)<?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>

Page 21: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Modified main.xml (2) <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 22: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Linear Layout

Page 23: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Identify the changes in main.xml

Page 24: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Modified main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation=“horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent“

Page 25: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Identify changes in original main.xml

Page 26: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

ID

• Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute.

android:id="@+id/my_button"

Page 27: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Vertical LinearLayout to hold a TextView and a Button

• <?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" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /></LinearLayout>

Page 28: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Page 29: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Page 30: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

android:id="@+id/my_button"

• The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.

• The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).

Page 31: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

Loading the XML Resource

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView.(R.layout.main_layout);}

Page 32: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

R.java

• Eclipse combs through res/ directory, generating Java class to access resources in code.

• Examples:R.string.<string_name>, R.layout.<layout_name>

• Accessing resources through the R class lets Android determine the right resource to use

Page 33: User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.

gen/ [Generated Java Files]/R.java/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */

package com.example.helloandroid3;

public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; }}