Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type.

Slides:



Advertisements
Similar presentations
Programming with Android: Application Resources Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
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.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
Android: Layouts David Meredith
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Modify Android Objects Using.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Mobile Computing Monetizing An App Copyright 2014 by Janson Industries.
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.
CE Applied Communications Technology Android lecture 2 - Structures Android File structure Resources Drawables Layout Values R Class Manifest Running.
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.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Exceptions Handling Exceptionally Sticky Problems.
Resources. Application Resources Resources are strings, images, and other pieces of application information that are stored and maintained (externalized)
UI Resources Layout Resources String Resources Image Resources.
User Interfaces: Part 1 (View Groups and Layouts).
Application Development for mobile Devices
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Copyright© Jeffrey Jongko, Ateneo de Manila University Basic Views and Layouts.
Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type.
Programming in Java CSCI-2220 Object Oriented Programming.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Android View Stuff. TextViews Display text Display images???
ListView and ExpandableListView
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
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.
Menus. Menus are a common user interface component in many types of applications. The options menu is the primary collection of menu items for an activity.
Introduction to android
Modern Programming Tools And Techniques-I
GUI Programming Fundamentals
Mobile Software Development for Android - I397
Instructor: Mazhar Hussain
Inheritance and Polymorphism
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
ANDROID UI – FRAGMENTS UNIT II.
HNDIT2417 Mobile Application Development
CIS 470 Mobile App Development
Android Programming Lecture 6
Android Layout Basics Topics
Chapter 15: In App Advertising
CA16R405 - Mobile Application Development (Theory)
Android Developer Fundamentals V2 Lesson 4
CIS 470 Mobile App Development
Korea Software HRD Center
Android Sensor Programming
Presentation transcript:

Resources and RelativeLayouts

Resources

Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type anim/XML files that define tween animations. color/XML files that define a state list of colors. drawable/Bitmap files (.png,.9.png,.jpg,.gif) or XML files that are compiled into drawable resources layout/XML files that define a user interface layout. menu/XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. values/XML files that contain simple values, such as strings, integers, and colors. xml/Arbitrary XML files that can be read at runtime.

Using Resources in XML Three Important Pieces – resource_type – resource_name/id These Three pieces create a path to a resource

Using Resources in Code How to I access the string is Code? In code, you gain access to the resources via the R class For each type of resource, there is an R subclass

R subclasses public final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; } Dimen subclass for all dimensions Drawble subclass for all images ID subclass for all ids specified in layout

What does the static keyword mean? public final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; }

Static Keyword Static variables are associated with the class, rather than with any object. Every instance of the class shares a class variable. Since static variables are associated with the class, we don’t need an instance of the object to access the static vars.

Static Keyword To access static variables all you need to know is the class name. If the static variable is public, you can easily get the variable.

The R file only contains integers public final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; }

The R file only contains integers The R class by itself won’t get you the resource you want. Instead you’ll have to – use methods which take resource ids to get the resource you’re interested in. – Use the Resources Object to obtain the real resource

Common methods that take Resource Ids 1.findViewById() 2.TextView.setText() – This method has two signatures, one for a String and another for Resource Id 3.View.setBackgroundResource()

Using the Resources Object The Activity provides a method getResources(). getResources() returns an instance of the Resources class that is specific to your application. That means you only have access to your resources not resources in another application.

Resources Class The Resources class provides getter methods to retrieve any type of resource available to your app. – String – String Array – Integer – Integer Array – Color – Drawable – Dimension – Animation – Etc.

How to get a color value from resources //getResources() is a method provided by the Activity Resources res = getResources(); //res.getColor(id) takes a resource identifier and returns a color or 0 if id not found //We must use the R class to access the color subclass to access the identifier for the color blue int blueColor = res.getColor(R.color.blue);

How to get a string value from resources //getResources() is a method provided by the Activity Resources res = getResources(); //res.getString(id) takes a resource identifier and returns a string or throw an exception if the id not found //We must use the R class to access the string subclass to access the identifier for the string app_name String appName = res.getString(R.string.app_name);

RelativeLayout

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

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

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

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? layout_alignParentTop layout_below layout_alignParentLeft layout_below layout_alignParentRight

Can we simplify the layout params used? layout_below layout_alignParentRight

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

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

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

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 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 <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="150dp" android:layout_height="75dp" android:background="#000" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" />

Creating an id for layout params <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="150dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" />

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 <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" /> How to make the right edge extend to

RelativeLayout Layout Parameters

Controlling Size with RelativeLayout <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" />

How to make a new View’s left edge and right’s edge <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" />

How to make a new View’s left edge and right’s edge <RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_alignParentRight="true" /> <View android:layout_width="0dp" android:layout_height="75dp" android:layout_marginTop="5dp" android:background="#000" />

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

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.