Monday, July 23, 2018

Android Resources

Good Practice to have non code resources as external resources. These include images, String constants, colors, animations, themes, menus  and most powerful ones which are layouts. It makes them easy to manage 
and update external resources. Alternative resources can be easily specified for different hardware configurations, languages, locations. Android automatically selects current resources when application starts. Even after starting the app, layout may be switched based on size and orientation.

How To Create Resources


res folder has all resources. Sub folders for each resource types. Default ones are values, drawable-hdpi, drawable-mdpi, drawable-ldpi, layout. The different resources will be compiled and compressed when the application is built as efficiently as possible. At that time, R.java file is generated which is compiled into R class file that containes references to these resources to be used from your application code.
Names of resource files should contain only lower case letters, numbers and underscore and period only.
1.       Simple Values – strings, colors, dimensions, styles, string/int array. Location: res/values folder.
eg.
res/values/simple_values.xml
<resources>
<string name=”app_name”>To Do List</string> // here you can use <b>, <i>, <u> tags
<plurals name=”androidPlural”>
<item quantity=”one”>One android</item>
<item quantity=”other”>%d androids</item>
</plurals>
<color name=”app_background”>#FF0000FF</color> // #RGB, #RRGGBB, #ARGB, #AARRGGBB are supported
<dimen name=”default_border”>5px</dimen>
<string-array name=“string_array“>
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
<array name=“integer_array“>
<item>3</item>
<item>2</item>
<item>1</item>
</array>
</resources>
2.       Styles and Themes – Common attribute values which can be used by multiple views.

<resources>
<style name=”base_text”>
<item name=”android:textSize”>14sp</item>
<item name=”android:textColor”>#111</item>
</style>
<style name=”small_text” parent=”base_text”> // inheritance of style by specifying parent attribute
<item name=”android:textSize”>8sp</item> // overriding old attribute, or redefining new attribute
</style>
</resources>

3.       Drawables – bitmaps, NinePatches (Stretchable PNGs), every drawable in separate individual file
4.       Layouts – Presentation Layer, XML Specification of UI Elements, once defined here, it must be inflated into the UI, by invoking setContentView() method from inside onCreate() method of activity. Folder is res/layout, each layout in separate file,
5.       Animations – Property Animations (3.0 version, res/animator, file name is used as identifier like drawables and layouts, they are extremely used to animate fragments), View Animations(Animating particular Views), Frame Animations (Switch drawables after a particular time).
6.       Menu - res/menu , 1 file for 1 menu
7.        
System Resoucres
In addition to the resources we supply, Android Platform includes several system resources which can be used in Code directly or in other resources. User “android.R.” instead of “R.”. eg. android.R.string.httpErrorBadUrl
<EditText
android:id=”@+id/myEditText”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@android:string/httpErrorBadUrl”
android:textColor=”@android:color/darker_gray”
/>


How to access resources in code?
Internationalization of resources
Resources r=getResources();
=>We have getter methods for each Resource Type?
r.getDrawable(R.drawable.app_icon);
r.getText(R.string.some_string_name);
r.getColor(R.color.some_color);
r.getStringArray(), r.getIntArray(),

Project/
     res/
          values/
               strings.xml
          values-fr/
               strings.xml
          values-fr-rCA/
               strings.xml

No comments:

Post a Comment

Near by App

https://drive.google.com/file/d/0B2ag35s4X53Eb2pSQVI1SzNudE0/view