layouts, values, internationalization, animation, menus, etc. • Provide different resource versions depending on qualifiers • Name the folders using the form: • <folder_name>-<qualifier_config> • Can add more than one but respecting an order • Samples • drawable-hdpi: high density version (~240dpi) • drawable-land-xhdpi: extra high density version for landscape mode (~320dpi) • values-es: Strings and values to use when the locale is “es” (Spanish) • layout-large-land-car-night-finger-v11: guess it! • More info: http://developer.android.com/guide/topics/resources/pro viding-resources.html Resource folders 2 Pro tip Exclude resources that begin with _ Tip Folders without qualifiers are the default values
override it • Different versions of same resource must have the same file name. • Same view in different layout versions must have the same id. • If the resource does not match any qualifier, android tries to find the best match. • Resources are accessed in two ways: – In code: R.string.app_name – In XML: @string/app_name Automatic handling of resources 3 Pro tip Access android core resources with android.R.anim.fade_in or @android:anim/fade_in
is equivalent to one physical pixel on a 160 dpi screen – px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels – Don’t ever EVER use pixels, use dp instead (or sp for font sizes). Supporting different screen densities 4 Relative sizes for bitmap drawables that support each density http://developer.android.com/guide/practices/screens_support.html
for different devices and “avoid” fragmentation. Supporting different screens 5 • Screen madness: • Use smallestWidth qualifier: sw<N>dp (sw480dp, sw600dp) • Qualifiers from Android 3.2 to set different layouts • 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc). • 480dp: a tweener tablet like the Streak (480x800 mdpi). • 600dp: a 7” tablet (600x1024 mdpi). • 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc) res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
• Types of containers – LinearLayout: Easiest to learn. Displays views either horizontally or vertically. – RelativeLayout: Positions views relative to other views. Good to create layouts that overlap with transparencies. – FrameLayout: Basic layout. Stack views one above the other. Not very useful – Also TableLayout and GridLayout • Android drawing algorithm – onMeasure. How big does the views want to be. – onLayout. Drawing the views Efficient layouts 6
• (aka use RelativeLayout) • Reuse layouts • You can use <include/> to add other layout file • <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> • Avoid duplicating containers of same type • Use <merge/> • Attaches it’s children to it’s parent • Great combo with <include/> Efficient layouts (II) 7
stretch and areas where content is placed • Image expands to fit content preserving complex image shapes like corners or decorations Drawables 10 • Top and left • Define stretchable areas (no shrinkable!) • Bottom and right • Content area, the rest is padding
drawables or colors to different states of the view. – The order is important. First match. – Density independant. Store the file in /res/drawable/btn_nav_bg_selector.xml Drawables 11 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_nav_forward_default" android:state_enabled="true" android:state_window_focused="false"/> <item android:drawable="@drawable/btn_nav_forward_disabled" android:state_enabled="false" android:state_window_focused="false"/> <item android:drawable="@drawable/btn_nav_forward_disabled" android:state_enabled="false" android:state_window_focused="true"/> <item android:drawable="@drawable/btn_nav_forward_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/btn_nav_forward_pressed" android:state_enabled="true" android:state_focused="true"/> <item android:drawable="@drawable/btn_nav_forward_default" android:state_enabled="true"/> <item android:drawable="@drawable/btn_nav_forward_default" android:state_focused="true"/> <item android:drawable="@drawable/btn_nav_forward_default"/> </selector> Disabled Pressed Default android:background="@drawable/btn_nav_bg_selector"
of each other in one single drawable – Useful for composing • Level List – Similar but only displays one drawable at once – Useful for non default states (i.e. traffic lights) • Transition drawable – Performs a cross fade between drawables • Clip drawable – Clips a portion of the drawable – Useful for customizing progress bars • Scale drawable – Scales a drawable Other drawables 13 http://developer.android.com/guide/topics/resources/drawable-resource.html Inset Layer List Clip
or full applications – <activity android:theme="@style/Theme.Junaio"> – <application android:theme="@style/Theme.Junaio"> Themes 17 <style name="Theme.Junaio" parent="android:Theme"> <item name="android:windowBackground">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">false</item> <item name="android:buttonStyleToggle">@style/Topbar.Button</item> </style> Pro tip Predefined theme names are not well documented and they are a bit tricky buttonStyle buttonStyleToggle radioButtonStyle …