Resources. Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type anim/XML files that define tween.

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

Android User Interface
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.
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
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
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.
PROG Mobile Java Application Development PROG Mobile Java Application Development Developing Android Apps: Components & Layout.
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
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.
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.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
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.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
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.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
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
Lab7 – Appendix.
Introduction to android
Android Application 2D Graphics cs.
Adapting to Display Orientation
GUI Programming Fundamentals
Mobile Software Development for Android - I397
Android Widgets 1 7 August 2018
Creation of an Android App By Keith Lynn
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Politeknik Elektronika Negeri Surabaya
HNDIT2417 Mobile Application Development
Android SDK & App Development
CIS 470 Mobile App Development
Android Programming Lecture 6
Android Layout Basics Topics
CMPE419 Mobile Application Development
CA16R405 - Mobile Application Development (Theory)
Building User Interfaces Basic Applications
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Android Developer Fundamentals V2
Android Developer Fundamentals V2 Lesson 4
Android Developer Fundamentals V2
CIS 470 Mobile App Development
Android SDK & App Development
CMPE419 Mobile Application Development
Android Project Structure, App Resources and Event Handling
Korea Software HRD Center
Building a Simple User Interface
CMPE419 Mobile Application Development
User Interface Screen Elements
User Interface Screen Elements
Android Sensor Programming
Presentation transcript:

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 do I access the string in 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.

Android View Stuff

TextViews Display text Display images???

TextView Drawables TextViews allow drawables to appear to the left of, above, to the right of, and below the text.

<LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drawable Left" android:layout_margin="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drawable Top" android:layout_margin="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drawable Right" android:layout_margin="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drawable Bottom" android:layout_margin="10dp"/>

setError() Method available to TextView and EditText to display an error message to the user. This is very useful for user input validation.

ImageView Use this View when you want to display an image in your application. Many beginners will misuse the ImageView by using the incorrect property.

ImageView src property Use the android:src property to set a drawable as the content of the ImageView. Don’t use android:background unless you want the image to have a background img.

ImageView Example <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"

ImageView Example with a background <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFF0000"/>

ImageView scaleType Control how your image is scaled and positioned inside the ImageView – Useful when your image is too big – Useful when your image is too small

ImageView scaleType : center Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : centerCrop Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : centerInside Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : fitCenter Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : fitStart Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : fitEnd Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : fitXY Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView scaleType : matrix Scale TypeDescription centerDisplays the image centered in the view with no scaling. centerCropScales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view. centerInsideScales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center. fitCenterScales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view. fitStartSame as fitCenter but aligned to the top left of the view. fitEndSame as fitCenter but aligned to the bottom right of the view. fitXYScales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio. matrixScales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.

ImageView ScaleType Info ?p= &seqNum=2 ?p= &seqNum=2

Android:tint ImageView’s have an attribute that allows the source image of the view to be tinted by a color.

ImageButton We’re familiar with a Button – Default background provided by the platform – Displays Text Well Android supports an ImageButton – Looks like a regular button – Default background provided by the platform – Displays an Image

Why Buttons are awesome Android provides a method for giving a button a state list that defines which images should be shown while the button is: – Normal – Focused – Enabled – Disabled – Pressed

Hiding the default background on an ImageButton If you want to use an ImageButton but don’t want to see the default background, you can hide it. – Set the android:background=“# ” – Set the

ShapeDrawables An XML file that defines a geometric shape, including colors and gradients. Creates a ShapeDrawable.ShapeDrawable

shapecornersgradientpaddingsizesolidstroke

ShapeDrawable Examples

What is a 9-Patch An Image that has the capability to specify stretchable regions.

Use Case for 9-Patch

More Info on 9-Patch Android 9 Patch Tool A Simple guide to 9-patch for Android UI