This blog post is aimed at any Android developer who would like to learn a few things that you don’t necessarily learn through straight coding or Google searches about things you’re stuck with. These hints, tips and tricks are just some things I have discovered that I wish I had known about from the beginning to make my life easier.
Here’s the shortlist, scroll down to see more details.
#1 – Transparent colours
#2 – dp & sp as unit dimensions
#3 – Do the heavy lifting in the background thread
#4 – Show a loading/progress bar on app creation
#5 – Image caching
#6 – Don’t refresh on orientation change
#7 – Block orientation change
#8 – Refresh activity
#9 – Learn the Activity Lifecycle
I will add to the list as I remember more things or make more discoveries.
#1 – Transparent Colours
Android allows (in most places) for an extra two digits in colour codes to give colours a transparency value!
The extra two digits go at the front, FF means fully visible, 00 means invisible. Values in between can also be used to get varying levels of transparency.
Examples:
// Fully visible, grey colour jsaOverviewLayout.setBackgroundColor(0xFF2c3033);
/*
Gradient drawable that is;
-Fully visible white [left] (0xFFFFFFFF)
-Fully visible orange [middle] (0xFFFF6400)
-Invisible black [right] (0x00000000) (colour here is irrelevant)
*/
GradientDrawable divider = new GradientDrawable(
Orientation.LEFT_RIGHT, new int[] {0xFFFFFFFF, 0xFFFF6400, 0x00000000});
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:orientation="horizontal"
android:id="@+id/item_viewer_header_layout" >
#2 – dp & sp As Unit Dimensions
You may be wondering what they mean and when to use them!
Basically: ‘dp’ units should be used for layout items, while ‘sp’ should be used for font sizes!
dp and sp unit’s were designed for xml, but there are workarounds to get them working programmatically in the code.
dp = Density-Independent Pixels (can also use ‘dip’)
dp is a unit of measurement based on screen density, to allow for different screen sizes for all the Android enabled devices out there. It gives an abstract view, instead of hard-coding a layout as a set amount of pixels (px), dp allows Android to stretch/shrink the layout item based on the screen size/density.
sp = Scale-Independent Pixels
sp does the density scaling as well, but also scales the font based on user preference.
I found myself trying to always shape layouts dynamically with FILL_PARENT and WRAP_CONTENT, it was quite hard for more complex layout. Then I found the magic of using dp.
Example:
<TextView android:id="@+id/feed_page_title" android:layout_width="50dp" android:layout_height="25dp" android:text="Yippie Kai Yay" android:textSize="16sp" android:textColor="#00b000" android:typeface="serif" />
Bonus: How to use dp/sp programmatically instead of just in xml
There are lots of conversion methods available here
http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android

