HNDIT2417 Mobile Application Development

Slides:



Advertisements
Similar presentations
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Layouts.
Styles and Themes. Tell me about this XML snippet
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
Android: Layouts David Meredith
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
Creating Android user interfaces using layouts 1Android user interfaces using layouts.
Android Development: Application Layout Richard S. Stansbury 2015.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Modify Android Objects Using.
PROG Mobile Java Application Development PROG Mobile Java Application Development Developing Android Apps: Components & Layout.
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.
Linear Layout, Screen Support, and Events. Linear Layout Supports 2 orientations: 1.Horizontal 2.Vertical I often get confused with how each orientation.
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.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
전광판 만들기 UNIT 04 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Layout XML 이란 ? 기본 XML 속성 Text View 2.
UI Resources Layout Resources String Resources Image Resources.
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
User Interfaces: Part 1 (View Groups and Layouts).
Application Development for mobile Devices
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
MOBILE COMPUTING D10K-7D02 MC04: Layouts Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Android View Stuff. TextViews Display text Display images???
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
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.
Android View Stuff. TextViews Display text Display images???
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.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Resources. Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type anim/XML files that define tween.
Android 9: Layouts Kirk Scott.
Laying out Elements with CSS
Adapting to Display Orientation
GUI Programming Fundamentals
Mobile Software Development for Android - I397
Linear Layout, Screen Support, and Events
Android Basic XML Layouts
Mobile Application Development BSCS-7 Lecture # 8
Android Widgets 1 7 August 2018
CIS 470 Mobile App Development
Android Layout Basics Topics
CIS 470 Mobile App Development
Lecture 2 b: Android Layout Basics Topics
CMPE419 Mobile Application Development
CA16R405 - Mobile Application Development (Theory)
Mobile Computing With Android ACST 4550 Android Layouts
Responsive Framework.
Building User Interfaces Basic Applications
Note Q) How to format code in Enclipse? A) Ctrl + i
Android Developer Fundamentals V2 Lesson 4
Android Topics Limited Resources and why we need to consider them.
Android Developer Fundamentals V2
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android SDK & App Development
CMPE419 Mobile Application Development
Korea Software HRD Center
Building a Simple User Interface
Bootstrap 4 December 17, 2018.
CMPE419 Mobile Application Development
CSS Layout: Flexbox.
CS 240 – Advanced Programming Concepts
Android Sensor Programming
Android Sensor Programming
Android Sensor Programming
Presentation transcript:

HNDIT2417 Mobile Application Development www.hndit.com HNDIT2417 Mobile Application Development Week 3- Linear Layout and RelativeLayout

Linear Layout and RelativeLayout www.hndit.com Linear Layout and RelativeLayout

Linear Layout Supports 2 orientations: Horizontal Vertical www.hndit.com Linear Layout Supports 2 orientations: Horizontal Vertical

Horizontal Orientation www.hndit.com Horizontal Orientation One row, many columns Items placed beside each other # # # # # # # # # #

Vertical Orientation One column, many rows www.hndit.com Vertical Orientation One column, many rows Items stacked on top of each other #

Gravity Two types of Gravity in Android gravity layout_gravity www.hndit.com Gravity Two types of Gravity in Android gravity layout_gravity

www.hndit.com android:gravity Positions the contents of the view (what is inside the view) Has several values: top bottom left right center (horizontal and vertical) center_vertical center_horizontal

android:gravity Use bit masking to combine multiple values www.hndit.com android:gravity Use bit masking to combine multiple values android:gravity="center_horizontal|bottom“ android:gravity=“top|bottom” android:gravity=“top|right”

android:layout_gravity www.hndit.com android:layout_gravity Positions the view with respect to its parent Supports same values as android:gravity Not all ViewGroups support this attribute.

gravity vs. layout_gravity www.hndit.com gravity vs. layout_gravity Using gravity on a ViewGroup will position all of its children a specific way. Using a LinearLayout as the root view to encapsulate everything. The LinearLayout has an attribute of gravity with a value equal to center. (That is why all the button are centered in the screen.)

gravity vs. layout_gravity www.hndit.com gravity vs. layout_gravity Using layout_gravity, a child can override the gravity set on it by its parent. Using a LinearLayout as the root view to encapsulate everything. The blue button has an attribute of layout_gravity with a value equal to right. (That is why all the button are centered in the screen, except the blue button which is on the right.)

What does this render? www.hndit.com <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout>

What does this render? www.hndit.com <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout>

Why isn’t the content centered? www.hndit.com Why isn’t the content centered? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout>

What is the width of the LinearLayout? www.hndit.com What is the width of the LinearLayout? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout>

What is the width of the LinearLayout? www.hndit.com What is the width of the LinearLayout? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout> Because the parent LinearLayout has width “wrap_content”, it’s essentially “hugging” the two views inside and so they have no space to center themselves in.

Adjust LinearLayout Width www.hndit.com Adjust LinearLayout Width <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:gravity="center" > <TextView android:layout_width="wrap_content" android:text="@string/hello" /> <ImageView android:src="@drawable/icon" /> </LinearLayout> A possible fix is to make the linearlayout as wide as its parent so use match_parent

Take 2: What does this render? www.hndit.com Take 2: What does this render? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height=“match_parent" > <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:text="@string/hello" /> <ImageView android:layout_gravity="right" android:src="@drawable/icon" /> </LinearLayout>

Take 2: What does this render? www.hndit.com Take 2: What does this render? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=“match_parent" android:layout_height=“match_parent" > <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:text="@string/hello" /> <ImageView android:layout_gravity="right" android:src="@drawable/icon" /> </LinearLayout> Okay so my my parent fills the entire width and I tell the TextView to position itself with respect to its parent on the left, and I tell the ImageView to position itself with respect to its parent on the right, but how come it doesn’t work?

LinearLayouts and layout_gravity www.hndit.com LinearLayouts and layout_gravity Depending on the orientation of the LinearLayout, the LinearLayout’s children can only use certain values for layout_gravity.

layout_gravity with horizontal orientation www.hndit.com layout_gravity with horizontal orientation For a horizontal Linear Layout the following values make sense: top center Bottom That is because the children of a horizontal Linear Layout are laid out horizontally one after the other. With horizontal orientation, the only thing can be controlled using the android:layout_gravity is how a child view is positioned vertically.

layout_gravity with vertical orientaiton www.hndit.com layout_gravity with vertical orientaiton For a vertical Linear Layout the following values make sense: left center Right That is because the children of a vertical Linear Layout are laid out vertically one below the other. With vertical orientation, the only thing can be controlled using the android:layout_gravity is how a child view is positioned horizontally.

layout_gravity & gravity Example www.hndit.com layout_gravity & gravity Example

layout_weight Attribute only works for LinearLayout www.hndit.com layout_weight Attribute only works for LinearLayout Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with this attribute.

www.hndit.com layout_weight The size of the View will be determined based on the relation of all sibling’s weights to each other. If, for example, all views define the same weight, then the available space will be distributed equally among them.

www.hndit.com layout_weight Which axis (width or height) should be affected can be controlled by setting the respective size to a value of 0dp. Weights don’t have to add up to 1, although it’s common to distribute layout weight over all children as fractions of 1 (percentage semantics). The relation between all weights is what matters.

www.hndit.com Problem I have 4 Views whose width I want to be equally distributed inside their parent. I want this to work on any device, and I don’t want to calculate the width each time the app runs.

layout_weight to the rescue www.hndit.com layout_weight to the rescue Using layout_weight we can assign an equal weight to each child view and at runtime the LinearLayout will equally distribute any remaining space to the children.

layout_weight to the rescue www.hndit.com layout_weight to the rescue <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pink" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#00F" > <View android:layout_width="0dp" android:layout_weight=".25" android:background="#0F0"/> android:background="#F00"/> android:background="#FC0"/> android:background="#FF0"/> </LinearLayout>

www.hndit.com RelativeLayout

www.hndit.com About RelativeLayout RelativeLayout is a view group that displays child views in relative positions.

www.hndit.com About RelativeLayout The position of each child view can be specified as relative to other sibling elements (such as to the left-of or below another view) or in positions relative to the parent area (such as aligned to the bottom, left of center). layout_alignParentTop layout_below layout_alignParentLeft layout_below layout_alignParentRight

www.hndit.com About RelativeLayout By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties.

RelativeLayout Layout Parameters www.hndit.com RelativeLayout Layout Parameters

www.hndit.com About RelativeLayout Unless you specify a vertical position/alignment, a child view will match the top of its parent. Unless you specify a horizontal position/alignment, a child view will match the left of its parent.

Can we simplify the layout params used? www.hndit.com Can we simplify the layout params used? layout_alignParentTop layout_below layout_alignParentLeft layout_below layout_alignParentRight

Can we simplify the layout params used? www.hndit.com Can we simplify the layout params used? layout_below layout_below layout_alignParentRight

layout_alignParentRight www.hndit.com How do we make the bottom views pushed down to the bottom of their parent? layout_below layout_below layout_alignParentRight

www.hndit.com How do we make the bottom views pushed down to the bottom of their parent? layout_alignParentBottom layout_alignParentBottom layout_alignParentRight

Using RelativeLayoutParams www.hndit.com Using RelativeLayoutParams Some layout parameters take true or false as values and others take View ids.

RelativeLayout Layout Params www.hndit.com RelativeLayout Layout Params Layout params that use true or false All layout parameters that have the word parent layout_centerHorizontal layout_centerVertical

RelativeLayout Layout Params www.hndit.com RelativeLayout Layout Params Layout params that use View ids Need to reference an existing id or create one for a view that will be defined later in the layout. Even if you aren’t planning on using an ID in code for finding a View. You still need it for RelativeLayout to layout views correctly.

Using existing ids www.hndit.com <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="150dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" /> android:id="@+id/right" android:layout_width="100dp" android:layout_below="@id/top" android:layout_alignParentRight="true" /> </RelativeLayout>

Creating an id for layout params www.hndit.com Creating an id for layout params <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="150dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_toLeftOf="@+id/right" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> android:id="@id/right" android:layout_width="100dp" android:layout_alignParentRight="true" /> </RelativeLayout>

Controlling Dimension of View with RelativeLayout params www.hndit.com Controlling Dimension of View with RelativeLayout params With the available options, you can not only position views in relationship to other views, but you can also size views.

Controlling Size with Relative Layout www.hndit.com Controlling Size with Relative Layout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> android:id="@+id/right" android:layout_width="100dp" android:layout_alignParentRight="true" /> </RelativeLayout> How to make the View @id/left’s right edge extend to View @id/right?

RelativeLayout Layout Parameters www.hndit.com RelativeLayout Layout Parameters

Controlling Size with RelativeLayout www.hndit.com Controlling Size with RelativeLayout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> android:id="@id/right" android:layout_width="100dp" android:layout_alignParentRight="true" /> </RelativeLayout>

www.hndit.com How to make a new View’s left edge match @id/left and right’s edge match @id/right? <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> android:id="@id/right" android:layout_width="100dp" android:layout_alignParentRight="true" /> </RelativeLayout>

www.hndit.com How to make a new View’s left edge match @id/left and right’s edge match @id/right? <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> android:id="@id/right" android:layout_width="100dp" android:layout_alignParentRight="true" /> android:layout_width="0dp" android:layout_marginTop="5dp" android:layout_below="@id/left" android:layout_alignLeft="@id/left" android:layout_alignRight="@id/right" /> </RelativeLayout>

The Power of RelativeLayouts www.hndit.com The Power of RelativeLayouts With all the different layout parameters, one can see why RelativeLayout is super powerful.

The Power of RelativeLayouts www.hndit.com The Power of RelativeLayouts Many novice Android Devs will over use LinearLayouts by having several nested inside each other to accomplish a task that is done much easier with RelativeLayout. Good Android Devs will use the minimum numbers of Views required to accomplish a layout.