Android: Layouts David Meredith

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
CE881: Mobile and Social Application Programming Simon M. Lucas Layouts.
Android User Interface
Unlocking Android Chapter 4.  Understanding activities and views  Exploring the Activity lifecycle  Working with resources  Defining the AndroidManifest.xml.
Android 02: Activities David Meredith
 User Interface - Raeha Sandalwala.  Introduction to UI  Layouts  UI Controls  Menus and ‘Toasts’  Notifications  Other interesting UIs ◦ ListView.
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
By: Jeremy Smith.  Introduction  Droid Draw  Add XML file  Layouts  LinearLayout  RelativeLayout  Objects  Notifications  Toast  Status Bar.
1 The Structure of a Web Table beginning of the table structure first row of three in the table end of the table structure table cells You do not need.
Creating Android user interfaces using layouts 1Android user interfaces using layouts.
Android Development: Application Layout Richard S. Stansbury 2015.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Modify Android Objects Using.
Android: versions Note that: Honeycomb (Android v3.0) A tablet-only release Jelly Bean (Android v4.1) Released on July 09, 2012.
CS5103 Software Engineering Lecture 08 Android Development II.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
PROG Mobile Java Application Development PROG Mobile Java Application Development Developing Android Apps: Components & Layout.
Chapter 2: Simplify! The Android User Interface
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Android Layouts. Layouts Define the user interface for an activity Layouts are defined in.xml files – within /res/layout folder – different layout can.
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
Understanding Hello Android 1 CS300. Activity  Similar to a form  Base class for the visual, interactive components of your application  Android API.
Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type.
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Using Android XML Resources.
Mobile Computing Lecture#11 Adapters and Dialogs.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 7: Reveal! Displaying Pictures in a GridView.
Android Boot Camp for Developers Using Java, 3E
UI Resources Layout Resources String Resources Image Resources.
User Interfaces: Part 1 (View Groups and Layouts).
Application Development for mobile Devices
Chapter 7: Reveal! Displaying Pictures in a Gallery.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Adapters 1 CS440. Adapters (again!)  Adapters are bridging classes, that bind data to user-interface Views. The adapter is responsible for creating the.
1 Android Development Lean and mean introduction Based on a presentation by Mihail L. Sichitiu.
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Building User Interfaces Basic Applications
BUILDING A SIMPLE USER INTERFACE. In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Http :// developer. android. com / guide / topics / fundamentals. html.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Android Fundamentals. What is Android Software stack for mobile devices Software stack for mobile devices SDK provides tools and APIs to develop apps.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Android 9: Layouts Kirk Scott.
Chapter 2: Simplify! The Android User Interface
Java FX: Scene Builder.
Mobile Application Development BSCS-7 Lecture # 8
Android 9: Layouts Kirk Scott.
Creation of an Android App By Keith Lynn
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
Android Layout Basics Topics
Android Lists and Fragments
Mobile Computing With Android ACST 4550 Android Layouts
Building User Interfaces Basic Applications
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
Building a Simple User Interface
Android Sensor Programming
Presentation transcript:

Android: Layouts David Meredith

Source Android API Guide on User Interface – ex.html

Introduction The user interface (UI) is everything that the user can see and interact with Android provides pre-built UI components such as structured layout objects and input controls that you can use to build your UI Android also provides other modules for special interfaces such as dialogs, notifications and menus

Overview All Android UI elements built using View and ViewGroup objects View is an object that draws something on the screen that the user can interact with ViewGroup is a container that holds Views and other ViewGroup objects and defines layout of all or part of the screen Views and ViewGroups for a particular screen arranged into a hierarchy

User interface layout ViewGroup is an invisible container that organizes child Views (some of which might themselves be ViewGroups) – ViewGroup is a subclass of View! Child Views inside a ViewGroup are widgets or input controls that draw some part of the UI Make your UI no more complex than it needs to be – It takes more time and memory to construct and display a complex View hierarchy

User interface layout Declare your layout either – in code, by instantiating View objects and building a tree – in an XML layout file (PREFERRED!) Name of XML element for a View is the same as the class name of the View (e.g., TextView, LinearLayout) Above is a simple vertical layout with a TextView and a Button When XML layout loaded, each node inflated into a runtime object

Layouts Layout defines visual structure of a user interface – e.g., for an Activity or an app widget You can declare Layouts either in your Java code or in an XML layout file – Can declare default layouts in XML – Modify state of UI components in your code

Declaring UI in XML resource Declaring UI in XML resource lets you separate presentation from logic and behaviour – Can modify presentation without changing code Can create XML layouts for different screen orientations, sizes and densities or different languages Easier to debug when layout defined in XML

Defining layout in XML XML vocabulary for defining layouts follows naming of classes and methods – XML element names correspond to class names – XML attribute names correspond to methods Though there are a couple of small differences – e.g., EditText.setText() corresponds to text attribute

Each layout file contains one root element, which must be (i.e., correspond to) a View or a ViewGroup object – This will usually be a Layout Layout is a subclass of ViewGroup! The root element contains child elements which correspond to Views (often widgets) and ViewGroups (often Layouts) The layout XML file should end with “.xml” and be stored in the res/layout/ directory Writing an XML layout file

Loading an XML layout resource When app is compiled, each XML layout file is compiled into a View resource Load the layout by calling setContentView() from the onCreate() callback implementation in your Activity class Pass setContentView() the id of the layout resource, as defined in the R.java file Example shows case for layout declared in main_layout.xml

Attributes Each View and ViewGroup element in an XML layout file has its own set of attributes (e.g., id, text) – These attributes are inherited by any class that subclasses View or ViewGroup Some attributes are layout parameters that describe layout orientations of a View object (e.g., layout_width, layout_height)

ID Any View object may have an integer ID Assigned in id attribute as a string – Associated with an integer constant in indicates the type of the value following the slash – in this case, an “id” type resource +: indicates that this is the first time the resource is being defined and that it should be added to R.java

Android resources You can also access a number of Android framework IDs Here you do not use the “+” symbol, since you are not defining the ID, just using one that has already been defined You declare the type of the resource to be to indicate that the id is defined in the android package namespace – “android” namespace means referencing a resource defined in android.R resources class, not private resources class

Creating and referencing Views First define the view or widget in the layout file and assign a unique id Then create an instance of the View and capture it from the layout (usually in onCreate()) In a RelativeLayout, often need to define View position relative to other sibling Views, referred to by their IDs ID should be unique throughout the entire View tree

Layout parameters XML layout attributes with name layout_something define layout parameters A ViewGroup implements a nested class that extends ViewGroup.LayoutParams – i.e., a class called LayoutParams, defined inside ViewGroup The ViewGroup.LayoutParams class inside a ViewGroup defines size and position of each of its child Views

Layout parameters Every LayoutParams subclass has its own syntax for setting values Each child element must define LayoutParams appropriate for its parent A child ViewGroup may also define different LayoutParams for its own children

layout_width and layout_height Every ViewGroup.LayoutParams defines a width (layout_width) and height (layout_height) – Every view in the ViewGroup must define these in its XML declaration Many ViewGroups also include optional margins and borders layout_width and layout_height can be defined by absolute distances (DISCOURAGED!) Usually define layout_width and layout_height using a constant such as – wrap_content – View expands to size of content – fill_parent or match_parent – View as big as parent will allow Can also use density-independent pixel units (dp)

Layout position View controls a rectangular area in the UI View has left, top, width and height properties, expressed in pixels relative to the parent Has getter methods: getLeft(), getTop(), getWidth(), getHeight() L T W H

Size, padding and margins of a View getWidth(), getHeight() – actual width and height of View on screen at drawing time after layout getMeasuredWidth(), getMeasuredHeight() – how big a view wants to be within its parent setPadding(), getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() – pixels of padding between contents of view and edge of its rectangle ViewGroups can be used to define margins T W H

Common layouts Each subclass of ViewGroup has its own way of managing the placement of the Views that it contains Try to keep your View hierarchies shallow – otherwise they may take a noticeable time to draw

Building layouts with an Adapter If you want to populate a ViewGroup with child views at runtime, you can define a subclass of AdapterView An AdapterView uses an Adapter to bind data to its layout Adapter serves as an intermediary between the AdapterView and a data source Adapter retrieves data items from a database or an array and converts them into Views that can be added to the AdapterView layout ListView and GridView are common layouts backed by Adapter

Filling an AdapterView with data Choose an AdapterView – e.g., GridView or ListView Bind the AdapterView to an Adapter – retrieves data entries from an external source and creates a View for each entry Android provides several subclasses of Adapter – most common are – ArrayAdapter – SimpleCursorAdapter

ArrayAdapter If the data source for your layout is an array, then use an ArrayAdapter By default: – ArrayAdapter creates a TextView for each item in the array and sets the TextView text using the item’s toString() method

ArrayAdapter Example shows how to display an array of Strings in a ListView Construct an ArrayAdapter as above, where arguments are as follows – “this” is the app Context – “android.R.layout.simple_list_item_1” is a reference to the layout that contains a TextView for each String – “myStringArray” is your array of Strings

Using an ArrayAdapter Once you’ve constructed your ArrayAdapter, you just call setAdapter() on your ListView You can customise appearance by overriding toString() method for the objects in the array If you want each item to be displayed in another type of View – extend ArrayAdapter – override getView() to return the type of View you need e.g., use an ImageView to hold each image in an Array of images

SimpleCursorAdapter When your data comes from a Cursor, use a SimpleCursorAdapter to adapt it to an AdapterView Must specify a layout for each row in the Cursor Must specify which columns in the Cursor are inserted into which Views in the layout e.g., to display names and numbers of contacts, – specify a String array giving the columns in the Cursor that will be used – specify an array of int IDs giving the corresponding Views for the Cursor columns (see above)

Using a SimpleCursorAdapter When you instantiate the SimpleCursorAdapter, pass – the layout to use for each result (row) – the Cursor containing the results (rows) – the array of columns – the array of Views SimpleCursorAdapter creates a View for each row in the Cursor using the layout provided by inserting each item in fromColumns into the corresponding View in toViews If data in underlying data source changes, send a notifyDataSetChanged() –attached view will refresh itself with the new data

Handling click events Respond to click events on AdapterView items by implementing the AdapterView.OnItemClickListener interface Then set the AdapterView’s OnItemClickListener to an instance of this implementation

LinearLayout LinearLayout is a ViewGroup that aligns all its children in a single row or column Direction specified with android:orientation attribute Horizontal list will be height of the tallest element Vertical list will be width of the widest element LinearLayout respects margins between children and gravity of each child – gravity is right, center, or left alignment

Layout weight in LinearLayout In LinearLayout, can assign a weight to each child with android:layout_weight attribute – Remaining space is allocated in proportion to the assigned weight of the children – Default weight is 0 If three text fields have weights, 1, 1 and 0, then – third text field is only big enough to contain its content – other two share equally the space remaining after all three fields are measured

LinearLayout in which elements are equally sized Set android:layout_height (for vertical layout) to “0dp” Set android:layout_width (for horizontal layout) to “0dp” Set android:layout_weight of each view to 1

LinearLayout Example

RelativeLayout RelativeLayout displays children in positions relative to each other (siblings)... – e.g., “left-of” or “below” a sibling...or relative to the parent RelativeLayout area – e.g., aligned to the bottom, left of centre

RelativeLayout RelativeLayout can eliminate nested ViewGroups and keep the View hierarchy flat – improves performance! Can sometimes replace several nested LinearLayouts with a single RelativeLayout

Positioning Views RelativeLayout lets child views specify their positions relative to each other and the containing Layout e.g., align two elements by right border, make one below another, centered in the screen, centered left, etc. By default, all child Views drawn in the top-left of the layout Define positions using layout properties available from RelativeLayout.LayoutParams

Examples of layout properties android:layout_alignParentTop – if “true” then top edge of View matches top edge of parent android:layout_centerVertical – if “true”, child centered vertically in parent android:layout_below – Top edge of this View below View specified with resource ID android:layout_toRightOf – Left edge of this View to the right of View specified with resource ID There are many more RelativeLayout layout attributes!

RelativeLayout layout properties Value for a layout property in RelativeLayout is either – a boolean – enables layout position relative to parent RelativeLayout – an ID – references another View in Layout relative to which this View should be positioned Views can be declared in any order in the XML layout file – so a View can depend on a View that is declared later in the XML file

Example of RelativeLayout