Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type."— Presentation transcript:

1 Resources and RelativeLayouts

2 Resources

3 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.

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

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

6 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

7 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; }

8 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.

9 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.

10 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; }

11 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

12 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()

13 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.

14 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.

15 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);

16 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);

17 Screen Support

18 Screen Support Key Terms Screen Density Resolution Orientation Density-independent pixel

19 Screen Density – The quantity of pixels within a physical area of the screen. – Usually referred to as dpi – Android groups screen densities by: low, medium, high, extra high – A low density screen has fewer pixels within a given physical area than a “normal” or “high” density screen.

20 Resolution – The total number of pixels on a screen – Use the resolution to manage layouts for your application based on available screen width/height. – Create layouts that are configured for Smallest width Available screen width Available screen height

21 Resolution If you want to create layouts for specific resolutions, find more information here.here

22 Orientation – The orientation of the screen from the user’s point of view – Either landscape or portrait – You can create specific layouts catered to your devices orientation

23 Density Independent Pixel (dp) – A virtual pixel unit – Should be used for defining UI layout that can be reused for multiple screen sizes.

24 How big is a dp? See my Android Display Metric Cheat Sheet.Android Display Metric Cheat Sheet

25 Creating an emulator for a specific device Use this articlearticle

26 UI Events

27 Handling UI Events Topics 1.Overview of UI Events 2.Implementing UI Event Listeners

28 UI Events Key Touch Click Long Click Focus Change Track ball Drag and Drop

29 Key Events Called when a hardware key is dispatched to focused a view. Event Listener Interface: View.OnKeyListener Callback Method: onKey()

30 Touch Events This is called when the user performs an action qualified as a touch event: – Press – Release – Movement (within the bounds of a view). Event Listener Interface: View.OnTouchListener Callback Method: onTouch()

31 onTouch(View v, MotionEvent event) Takes 2 parameters Returns true if the listener consumed the event, false otherwise. NameDescription v The view the touch event has been dispatched to event The MotionEvent object containing full information about the event.

32 MotionEvent Used to report movement for – Mouse – Pen – Finger (Touch) – Trackball

33 MotionEvent for touch Contains data about “pointers” aka active touch points on the device’s screen. For each pointer you can receive – X/Y coordinates – Size – Pressure – Value describing what kind of motion occurred.

34 MotionEvent for single pointer getAction() – return the action being performed (action_down/move/up/cancel). getX() - returns the X coordinate of this event for a single pointer. getY() - returns the Y coordinate of this event for the given pointer

35 getAction() ACTION_DOWN - A touch gesture has started. ACTION_MOVE - A change has occurred between touch down and release. ACTION_UP – A touch gesture has finished. ACTION_CANCEL – A touch gesture has been aborted.

36 @Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); // Remember where we started mLastTouchX = x; mLastTouchY = y; break; } case MotionEvent.ACTION_MOVE: { final float x = ev.getX(); final float y = ev.getY(); // Calculate the distance moved final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; // Move the object mPosX += dx; mPosY += dy; // Remember this touch position for the next move event mLastTouchX = x; mLastTouchY = y; // Invalidate to request a redraw invalidate(); break; } return true; }

37 MotionEvent for multiple pointers The MotionEvent class provides many methods to query the position and other properties of pointers:  getX(int) – returns X pos for given pointer index.  getY(int) – returns Y pos for given pointer index.  getPointerCount() – The number of pointers for this event

38 MotionEvent for multiple pointers  getPointerId(int) – pointer id associated with the pointer index for this event  findPointerIndex(int) – given a pointer id, find the index of its data.

39 New getAction() values ACTION_POINTER_DOWN – fired when a secondary pointer goes down. ACTION_POINTER_UP – fired when a secondary pointer goes up.

40 Pointer index Each pointer index corresponds to a finger for the event. The pointer index ranges from 0 to one less than the returned value of getPointerCount(). The order in which individual pointers appear is undefined; therefore you can’t rely on the pointer index to map to the same finger across touch events.

41 Multiple Pointers are hard! When working with multiple pointers, android provides many methods that use the pointer index. Pointer indices can change across events; therefore, we need another way to identify pointers.

42 Pointer ID The pointer id of a finger is guaranteed to remain constant as long as the pointer remains active (until it ACTION_UPs) When you want to keep track of a finger, save its ID on action down. Retrieve the saved finger’s pointer index for the current event with findPointerIndex(int).

43 Multiple Pointers Example @Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mLastTouchX = x; mLastTouchY = y; // Save the ID of this pointer mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { // Find the index of the active pointer and fetch its position final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; mPosX += dx; mPosY += dy; mLastTouchX = x; mLastTouchY = y; invalidate(); break; } case MotionEvent.ACTION_UP: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { // Extract the index of the pointer that left the touch sensor final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = ev.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly. final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = ev.getX(newPointerIndex); mLastTouchY = ev.getY(newPointerIndex); mActivePointerId = ev.getPointerId(newPointerIndex); } break; } } return true; }

44 Additional Info on Multitouch http://android- developers.blogspot.com/2010/06/making- sense-of-multitouch.html http://android- developers.blogspot.com/2010/06/making- sense-of-multitouch.html

45 Click Events A Click is a pair of down/up events performed either with the keyboard, trackball, or touch screen. If the view receives a down event but does not receive a following up event, then the click event does not occur.

46 Click Events Event Listener Interface: View.OnClickListener Callback Method: onClick() Parameters: v The view that has been clicked

47 Long Click Events Happens when a item is pressed and held (for one second). Applies to touch, trackball, hard keys.

48 Long Click Events Event Listener Interface: View.OnLongClickListener Callback Method: onLongClick() Parameters: v The view that has been clicked and held

49 Trackball events To my knowledge, trackballs on devices have completely disappeared. So don’t worry about them.

50 Focus Change events Focus Change applies to physical keyboards. There are still a few devices that support a physical QWERTY keyboard. However, BlueTooth keyboards are becoming more popular and will work with any BlueTooth enabled device.

51 What is Focus? When using a physical keyboard, users can navigate through the UI using arrow keys. If a view has focus, a focus ring/rectangle will appear around the view. Focus also occurs with EditText views when they are selected.

52 Focus Example

53 Focus Tip Unless you’re using a physical hard keyboard or soft/virtual keyboard don’t worry about focus. Sometimes developers will try and use focus at inappropriate times so use the above tip.


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

Similar presentations


Ads by Google