Graphical User Interfaces

Similar documents
Android Layout Types

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android UI Development

Programming with Android: Layouts, Widgets and Events. Dipartimento di Scienze dell Informazione Università di Bologna

ANDROID USER INTERFACE

Programming with Android: Introduction. Layouts. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android UI: Overview

Android Basics. Android UI Architecture. Android UI 1

Adapter.

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

Chapter 8 Positioning with Layouts

Programming with Android: Widgets and Events. Luca Bedogni. Dipartimento di Scienze dell Informazione Università di Bologna

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Android List-Based Selection Widgets

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Creating a Custom ListView

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

List-Based Widgets: Lists, Grids, and Scroll Views

User Interface Development in Android Applications

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

CS378 -Mobile Computing. User Interface Basics

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

Chapter 5 Flashing Neon FrameLayout

ListView (link) An ordered collection of selectable choices. key attributes in XML:

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

ListView Containers. Resources. Creating a ListView

IMP Question. Draw a diagram for activity life cycle and explain it. Mayur S. Padia, CE Department Android Programming 1

(c) Dr Sonia Sail LAJMI College of Computer Sciences & IT (girl Section) 1

Mobile Software Development for Android - I397

GUI Widget. Lecture6

Building User Interface for Android Mobile Applications II

UI (User Interface) overview Supporting Multiple Screens Touch events and listeners

Mobile User Interfaces

Text Properties Data Validation Styles/Themes Material Design

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

TPCT s College of Engineering, Osmanabad. Laboratory Manual SDL-II. Mobile Application Development (Android) For. Third Year Students (CSE)

Mobile Application Development Android

05. RecyclerView and Styles

Mobila applikationer och trådlösa nät, HI1033, HT2013

Mobila applikationer och trådlösa nät, HI1033, HT2012

CS371m - Mobile Computing. User Interface Basics

Sizing and Positioning

Laying Out Controls in Containers

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Basic GUI elements - exercises

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Android User Interface Overview. Most of the material in this sec7on comes from h8p://developer.android.com/guide/topics/ui/index.

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Android Application Development

Practical 1.ListView example

Developing Android Applications

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Embedded Systems Programming - PA8001

Mobile Computing Practice # 2a Android Applications - Interface

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

BSCS 514- Computer Graphics. Course Supervisor : Dr. Humera Tariq Hands on Lab Sessions: Mr. Faraz Naqvi

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Let s take a display of HTC Desire smartphone as an example. Screen size = 3.7 inches, resolution = 800x480 pixels.

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

Chapter 7: Reveal! Displaying Pictures in a Gallery

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Programming Android UI. J. Serrat Software Design December 2017

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Android CardView Tutorial

Intents. Your first app assignment

Create Parent Activity and pass its information to Child Activity using Intents.

Arrays of Buttons. Inside Android

Accelerating Information Technology Innovation

Stanislav Rost CSAIL, MIT

Open Lecture Mobile Programming. Intro to Material Design

Lesson 5. List-Based Selectors. List-Based Selector: Lists, Grids, and Scroll Views... 3

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

Action Bar. Action bar: Top navigation bar at each screen The action bar is split into four different functional areas that apply to most apps.

Android User Interface

Lecture 14. Android Application Development

The World of List View. Romain Guy and Adam Powell May 19, 2010

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Android User Interface Android Smartphone Programming. Outline University of Freiburg

CS 528 Mobile and Ubiquitous Computing Lecture 2a: Introduction to Android Programming. Emmanuel Agu

Topics of Discussion

Starting Another Activity Preferences

A view is a widget that has an appearance on screen. A view derives from the base class android.view.view.

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

CS260 Intro to Java & Android 05.Android UI(Part I)

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

South Africa

Introductory Mobile App Development

Android UI DateBasics

Praktikum Entwicklung Mediensysteme. Implementing a User Interface

CS260 Intro to Java & Android 05.Android UI(Part I)

Transcription:

Graphical User Interfaces

Some suggestions Avoid displaying too many things Well-known anti-patterns Display useful content on your start screen Use action bars to provide consistent navigation Keep your hierarchies shallow by using horizontal navigation and shortcuts

Layouts A layout is a ViewGroup that is responsible for positioning its child Views. It calculates and set the position and size of those Views Measure: Determines the view size Layout: Sets position and size for the view and all its descendants Draw: Draws the view on the screen

Layouts

Two ways Declare UI elements in XML Created at build time Programmatically Instantiated at runtime Together Declare your application's default layouts in XML Add code that modifies the state of the screen objects

LinearLayout Views presented on a single row/column Defined by layout_orientation Or setorientation(int orientation) Where orientation is either HORIZONTAL or VERTICAL Two further attributes Gravity specifies how to position the child view wrt the parent Weight indicates how much of the extra space in the LinearLayout will be allocated to the view

<LinearLayout xmlns:android= android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingleft="16dp" android:paddingright="16dp" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /> </LinearLayout> Example

Example 2 <LinearLayout xmlns:android= android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/b1" android:layout_weight="1" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="2" android:gravity="center_vertical center_horizontal" android:text="@string/b2" /> </LinearLayout>

Example 3 <LinearLayout xmlns:android= android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height= fill_parent" android:text="@string/b1" android:layout_weight="1" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="2" android:gravity="center_vertical center_horizontal" android:text="@string/b2" /> </LinearLayout>

Be careful <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:text="@string/b1" />

RelativeLayout Displays child views in relative positions The position of each view can be specified as Relative to sibling elements (such as to the left-of or below another view) Relative to the parent area (such as aligned to the bottom, left or center) Useful to align views Can eliminate nested view groups and keep your layout hierarchy flat

Some layout properties android:layout_alignparenttop (boolean) android:layout_centervertical (boolean) android:layout_below (ID) android:layout_torightof (ID) Dependencies against other views in the layout can be declared in any order

<RelativeLayout xmlns:android= <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/reminder" /> <Spinner android:id="@+id/options" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_alignparentleft="true" android:layout_toleftof="@+id/ok" /> <EditText android:id="@+id/password android:text="@string/password" android:inputtype="textpassword" android:layout_below="@id/options" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_torightof="@+id/passwordlabel" /> <TextView android:id="@id/passwordlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@id/password" android:text="@string/password" /> <Button android:id="@id/ok" android:layout_width="96dp" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_alignparentright="true" android:text="@string/done" /> </RelativeLayout> Example

ConstraintLayout Now default layout in Android Studio Designed to Create efficient UI Be a top-level, flat layout Flat hierarchies display quick and are memory efficient Solution to address the performance problems of other layouts Nested weighted Linear Layouts Relative Layouts

ConstraintLayout One horizontal and one vertical constraint for the view Each constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline Each constraint defines the view's position along either the vertical or horizontal axis Each view must have a minimum of one constraint for each axis, but often more are necessary.

Adnroid Studio

Chains 1. Spread 2. Spread inside 3. Weighted 4. Packed

FrameLayout Designed to display a single or multiple UI elements The position of multiple children can be controlled by assigning a gravity to each child Elements that overlap are displayed overlapping Child views are drawn in a stack, with the most recently added child on top Adds android:visibility to manage the visibility of views

Example <FrameLayout xmlns:android= android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:src="@drawable/ic_launcher" android:contentdescription="@string/test" android:scaletype="fitcenter" android:layout_height="fill_parent" android:layout_width="fill_parent"/> <TextView android:text="@string/test" android:textsize="30sp" android:textstyle="bold" android:textcolor="#003399" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center"/> </FrameLayout>

TableLayout <TableLayout xmlns:... <TableRow android:paddingtop="10dp" android:gravity="center"> <TextView... android:layout_gravity="center" android:layout_span="2"/> </TableRow> <TableRow android:layout_margintop="20dp" > <TextView... /> <EditText... /> </TableRow> <TableRow android:layout_margintop="20dp" > <TextView... /> <EditText... /> </TableRow> <TableRow android:gravity="center android:layout_margintop="20dip" > <Button... android:layout_span="2"/> </TableRow> </TableLayout>

WebView Helps deliver a web page as part of a client application You Must enable JavaScript if needed Can create interfaces between JavaScript code and Android code Can control navigation WebView mywebview = (WebView) findviewbyid(r.id.webview); mywebview.loadurl("http://www.example.com");

Lists and grids When the content for a Layout is dynamic or not predetermined, we need a layout that subclasses AdapterView to populate the layout with Views at runtime Items are automatically inserted into the Layout by an Adapter It pulls content from a source such as an array or database query Converts each item result into a View that is placed into the Layout ListView displays a list of scrollable items GridView displays items in a two-dimensional, scrollable grid

Adapter Used to visualize data Acts as a bridge between an AdapterView and the underlying data for that view ListView, GridView, Spinner are subclasses of AdapterView Provides access to the data items Makes a ViewGroup to interact with data Also responsible for making a View for each item in the data set Some methods isempty(), getitem(), getcount(), getview()

A couple of Adapters ArrayAdapter should be used when the data source is an array It creates a view for each array item by calling tostring() on each item and placing the contents in a TextView SimpleCursorAdapter should be used when data come from a Cursor We must specify a layout for each row in the Cursor and specify the columns that should be inserted into each view of the layout onitemclicklistener should be used to respond to click events on each item in an AdapterView

ListView example <ListView xmlns:android= android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/list" />

oncreate() public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } String[] data = {"First", "Second", "Third"}; ListView lv = (ListView) findviewbyid(r.id.list); lv.setadapter(new ArrayAdapter<String>(this, android.r.layout.simple_list_item_1, data)); simple_list_item_1 <TextView xmlns:android= android:id="@android:id/text1" style="?android:attr/listitemfirstlinestyle" android:paddingtop="2dip" android:paddingbottom="3dip" android:layout_width="fill_parent" android:layout_height="wrap_content" />

GridView example <GridView xmlns: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" />

oncreate() public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); GridView gridview = (GridView) findviewbyid(r.id.gridview); gridview.setadapter(new ImageAdapter(this)); } gridview.setonitemclicklistener(new OnItemClickListener() { public void onitemclick(adapterview<?> parent, View v, int position, long id) { Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } });

ImageAdapter (I) public class ImageAdapter extends BaseAdapter { private Context mcontext; // references to our images private Integer[] mthumbids = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };

ImageAdapter (II) // create a new ImageView for each item referenced by the Adapter public View getview(int position, View convertview, ViewGroup parent) { ImageView imageview; if (convertview == null) { // if it's not recycled, initialize some // attributes imageview = new ImageView(mContext); imageview.setlayoutparams(new GridView.LayoutParams(85, 85)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); } else { imageview = (ImageView) convertview; } } } imageview.setimageresource(mthumbids[position]); return imageview;

RecyclerView (flexible version of ListView) It is a container for displaying large data sets that can be scrolled very efficiently It simplifies the display and handling of large data sets by providing Layout managers for positioning items Default animations for common item operations, such as removal or addition of items A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible Recycling views improve performance by avoiding the creation of unnecessary views or performing expensive lookups

CardView (extends FrameLayout) Shows information inside cards that have a consistent look across the platform A RecyclerView comes with an adapter to inflate a layout for organizing the cards Cards need a dedicated layout RecyclerView and CardView widgets are part of the v7 Support Libraries

Styles and themes A style is a collection of properties that specify the look and format of a View or window Styles share a similar philosophy to cascading stylesheets A theme is a style applied to an entire Activity or application Every View in the Activity or application will apply each style property it supports Android provides a large collection of styles and themes

Example <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#00ff00" android:typeface="monospace" android:text="@string/hello" /> <TextView style="@style/codefont" android:text="@string/hello" /> ProjectName/res/values/styles.xml <resources> <style name="codefont" parent="@android:style/textappearance.medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textcolor">#00ff00</item> <item name="android:typeface">monospace</item> </style> </resources> This example style can be referenced from an XML layout as @style/codefont

Views and events The user interacts with the Views and generates events Events for clicks, long clicks, gestures, focus, external events Android manages the creation and distribution of these events, but not the reactions to them We must implement them by hand

Two elements Event Handlers Views have callback methods to handle specific events E.g., when a Button is touched, method ontouchevent() is called Special-purpose reactions are obtained by extending the particular View class and by overriding the method Suitable for custom elements Event Listeners Are Interfaces that contain a single callback method This method is called by the Android framework when the event is fired on the particular View

Some Listeners OnClickListener: onclick() OnLongClickListener: onlongclick() OnFocusChangeListener: onfocuschange() OnKeyListener: onkey() OnCheckedChangeListener: oncheckedchanged() OnTouchListener: ontouch() OnCreateContextMenuListener: oncreatecontextmenu()

Option 1 Implement the callback method Define the listener object as an anonymous class Pass an instance of the ActionListener implementation to the View through method setonxxxlistener Button btn = (Button) findviewbyid(r.id.button); btn.setonclicklistener(new OnClickListener() { @Override public void onclick(view view) { // Implementation } });

Option 2 Implement the callback method Implement the interface in the Activity Pass an instance of the listener to the View through method setonxxxlistener public class MyActivity extends Activity implements OnClickListener { Button btn = (Button) findviewbyid(r.id.button); btn.setonclicklistener(this); } public void onclick(view v) { // Implementation }

Option 3 Use onclick in the XML definition of the View (if possible) Implement the method in the activity <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button" android:onclick="myonclick" /> public void myonclick(view view) { // Implementation }

How to fire events in the code Through perform something methods The corresponding listener (if set) is activated performclick() activates the view's OnClickListener, if defined Used to produce events As consequences of other actions To test listeners

Composing layouts Create a new XML file and define the layout Example, a title bar that should be included in each activity Add the <include/> tag inside the new layout The <merge/> tag helps eliminate redundant view groups in your view hierarchy When you include this layout in another layout, the system ignores the <merge> element and places the elements directly in the layout

<include/> <FrameLayout android:layout_width="match_parent android:background="@color/titlebar_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /> </FrameLayout> <LinearLayout android:layout_width="match_parent android:layout_height="match_parent android:orientation="vertical android:background="@color/app_bg android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> </LinearLayout>