Android snippet: refer to a colour in code / programmatically

Here’s another possible Android gotcha.  Say you have a colour like this defined in an xml file: <color name=”red”>#FF0000</color> Then in some code you do something like: TextView textView = new TextView(context); textView.setTextColor(; You’ll find that the text comes out like some darkish grey colour and this is because it used the resource id (which […]

Android snippet: converting DIP / DP to pixels

Sometimes in android you have to deal with pixels, such as view.setPadding(int, int, int, int).  Obviously this is not ideal but no fear, there is a way to work around this. Simply come up with the value you want in dip, say 10, and then convert into pixels like so: float sizeInDip = 10f; int […]