Mobile Applications (Android Programming) Semester II 2016 - 2017 Dr. Saman Mirza Abdullah Class Objective • The objective of this class is: o To learn how Android applications store and access important resources such as strings, graphics, and other data. o To learn how to organize Android resources within the project files. o To learn the localization and configuration of different devices. Mobile Application - Ishik 2 Resources Directory Mobile Application - Ishik 3 Resources and Codes • Good developers not put data within the source code. • Instead, data are accessed programmatically. • Storing application resources in a single place o Is a more organized approach to development, o Makes the code more readable and maintainable o Localization (adaption) of applications for different languages and devices. Mobile Application - Ishik 4 Resources • All Android application are composed of two things: • Functionalities: o Is a code that determines the behaviors of the application, such algorithms or models for problems. • Resources: o They are data or information accessed by the source code, such as string, style and themes, dimensions, image and icons, audio files, vedios, and other data. Mobile Application - Ishik 5 Storing Resources • Resources are separately stored from .java class files • They are stored in XML, but still row data and graphs are accepted. • They are stored in /res directory o File names should be lowercase. o No duplicated. Mobile Application - Ishik 6 Resource Directories Mobile Application - Ishik 7 Resource Type and File Hierarchy http://d.android.com/guide/topics/resources/available-resources.html Mobile Application - Ishik 8 Default VS Alternative Resources • There are many criterion for storing resources. o Default resources are those that stored automatically by the created applications. o Creating some special resources are needed in some cases: • Internationalization, when an application needed to work with multilanguages. • Localization, when there is a need to run an application smoothly on different devices and orientations. Mobile Application - Ishik 9 Alternative Resources: Examples • An application with different graphical size. /res/drwable-ldpi/mylogo.png (low-density screen) /res/drwable-mdpi/mylogo.png (medium-density screen) /res/drwable-hdpi/mylogo.png (high-density screen) /res/drwable-xhdpi/mylogo.png (extra high-density screen) /res/drwable-xxhdpi/mylogo.png (extra extra high-density screen) • Application for different orientations: /res/layout-port/main.xml (layout loaded in portiere) /res/layout-land/main.xml (layout loaded in landscape) Mobile Application - Ishik 10 Access Resources • All resource files will be compiled and seen as R.Java class file and it is sub-classes. • The file will be automatically generated when any resources are added. • Through this files the content is accessed. • Example: o A String named strHello defined within the resource file called /res/values/string.mls o Can be accessed R.string.strHellow doesn't return data. o String mystring = getResource().getString (R.String.strHello) Mobile Application - Ishik 11 Accessing Resource Programmatically • Using String resources programmatically: Mobile Application - Ishik 12 String Resources • Using Bold, Italic, and underline. Mobile Application - Ishik 13 Working with Images • Applications often include visual elements such as icons and graphics. • Android supports several image formats. • can be directly included as resources for your application. • All resource filenames must be lowercase and simple Mobile Application - Ishik 14 List of Image Formats Mobile Application - Ishik 15 Accessing Image Resource • Image resources are simply another kind of Drawable called a BitmapDrawable. • For Accessing, mostly it needs only the resource ID of the image. • It can be accessed directly using BitmapDrawable function. Mobile Application - Ishik 16 Color State List • Some time it needs to color an object based on state of the object. • Could be done by special recourse called selector. • Example: o A Button control may need to by gray when disable, green when activated, and yellow when processed. Mobile Application - Ishik 17 Color State List Mobile Application - Ishik 18 Color State Defining Mobile Application - Ishik 19 Button State Color • we define a Button and set the textColor • the state list resource file text_color.xml is defined as list of state color. Mobile Application - Ishik 20 Class End Mobile Application - Ishik 21
© Copyright 2025 Paperzz