Resources Presentation

Goals

  • Know how to create resources of different types
  • Know how to define style values and styling collections in XML resource files
  • Know how resources are selected by qualifiers

Drawables

  • Drawables are an abstraction in Android for handling backgrounds, images and other "drawable" things
    • Images files like png, jpg
    • A single solid color
    • Gradient colors - smooth transition from one color into another
    • 9-patch images - special image file that have both non-stretchable and stretchable parts
  • Used primarily as background for a View
  • Specified in resource folder "res/drawable"
    • Binary files like png and jpg images
    • XML files that can be converted into objects like ShapeDrawable

Bitmap drawables

  • Just drag and drop image files onto the drawable folder in project tree
    • Default project might not have the generic "drawable" folder
    • Create the "drawable" folder under "res" for putting generic images and other resources
  • File name must comply with Java naming rules
    • Keep it to a-z, 0-9 and underscores
  • File name is also the resource identifier
  • /res/drawable/background.jpg
    <ImageView android:src="@drawable/background"
    

Shape drawables as xml resource file

  • Use for rounded corners
  • Use for gradient backgrounds
  • Add xml resource file with shape as root element
  • /res/drawable/example.xml
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <corners android:radius="2dp" />
      <gradient android:startColor="#ee8888" android:endColor="#dd3333"
        android:type="linear" android:angle="270" />
    </shape>
    
  • Reference the drawable as android:background in your View

Styling values in a central place

  • Apply a single styling value for many Views in many Activities
    • Color
    • Measurement (like textSize, padding)
    • Kind of a style "variable"
  • Defined in a XML resource file like /res/values/variables.xml
    <resources>
      <color name="dark">#339933</color>
      <dimen name="heading">20sp</dimen>
    </resources>
    
  • Reference in any View attribute that can hold that particular value
    <TextView android:textColor="@color/dark"
      android:textSize="@dimen/heading"
    

Collect multiple styling attributes into a single style

  • Defined in a XML resource file like /res/values/styles.xml
    <resources>
      <style name="checker">
        <item name="android:background">#333399</item>
        <item name="android:layout_margin">5dp</item>
      </style>
    </resources>
    
  • Referenced in any View as a single attribute (NOT android prefix!)
    <Button style="@style/checker"
    

Resource Qualifiers

  • A qualifier is a condition that can be used to determine which specific resource file to use
  • Qualified resource files have the same file name, but are put in different folders
  • The folder determines the qualifier, what to check and what value to trigger the particular file
  • The folder names is usually enough to realize what the qualifier is
    • -land for landscape orientation
    • -sv for Swedish language
  • Development environment will help in choosing and specifying qualifiers

Qualifier use cases

  • Landscape/Portrait orientation of device
    • Specify a different layout for landscape orientation, taking into the account that there is more space horizontally than vertically.
  • Language of device
    • Usually only used for texts in the user interface, even though you could use it for other resources too.
  • Tablet devices (screen size/resolution) Specify a different layout when there is more display space on the device, like on 7 or 10 inch tablet compared to a phone.

Strings and language support

  • If you have any ambition to translate your application into more than one language
    • Make sure NO string literals for user interface text is stored in Java
    • All user interface text i xml attributes should also be string resources
  • Stored as resource xml files in /res/values/strings.xml
    <resources>
      <string name="msg">Welcome to this application!</string>
    
  • Reference in layout xml files
    <TextView android:text="@string/msg" />
    
  • Reference in Java
    _textView.setText(R.string.msg);
    

More information

  • Book references