Tuesday, December 2, 2014

Resources in Android

1.Strings
Its kept under values folder and strings.xml. There are different values folder based on language. Ex. values-en for english.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
</resources>

To add new resource file to eclipse. Right click on “Res” folder or any folder in which you want to add resource file.  Select “New” => “Other” => “Android” => “ Android XML values File” => Enter the file name and click on Next =>  Choose configuration Folder dialog will appear then  choose “Available Qualifiers” (If you want to choose the different language file use language as qualifier the choose the language code. Two letter language code is used.).  You can add region to distinguish UK english and US english.


{Even there is option for resources to the particular version. }

2. Layout
You can have two different layout for same layout.   Select “New” => “Other” => “Android” => “ Android XML Layout File” => Choose configuration Folder dialog will appear then  choose “Available Qualifiers” then Select “Orientation” . In this case  two folders will get created on for potrait and another for landscape.[ Landscape folder will be named “layout-land”  for Landscape use attribute “android:orientation”=“horizontal”].

You can specify the weight of particular view with attribute “android:layout_weight”=“1”;

3.Dimension
 You can create the dimens.xml and set the value in it

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>
4.Colors
You can specify the colors in colors.xml ex:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="TextViewBackground">#FFEE00CC</color>
</resources>


5. Styles

You can specify the styles  in styles.xml ex:-
<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <style name="Theme.Default" parent="@android:style/Theme"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>

<style name= "temp">
    <item name="android:background">@color/TextviewBackground</item>
</style>
</resources>

Using the xml for view is similar to to html and css. layout xml is like the html and style,colors are like css.


No comments:

Post a Comment