Setting color from resource on Android


It is straight forward to set a color for text or background for example with the Android SDK but there is a mistake I made several times when getting started and this is what I want to cover in this short post.

Let’s take the example of setting the background color of a View and following the best practices, we want to use a color defined in colors.xml in the values directory of res.

The signature for the method used is setBackgroundColor(int color)

The mistake I made was to call it as myView.setBackgroundColor(R.colors.my_color) which does not trigger any warnings or errors at compile time nor at runtime but will give you grey no matter what color code you defined.

When thinking about it, it makes sense because R.colors.my_color returns the id value of the resource and not the actual color defined by the resource. So what we need to write instead is:
myView.setBackgroundColor(getResources().getColor(R.colors.my_color))

Note: if you are calling this outside of an Activity class or a class extending Activity, you need to specify the context for getResources so you may end-up with something like this:
myView.setBackgroundColor(context.getResources().getColor(R.colors.my_color))

Advertisements

Getting “The process cannot access the file because it is being used by another process” when trying to start a website


Today when trying to start a website that was mysteriously stopped on my development machine, I got the following error message:
The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)

This is a very misleading error message and I spent some time looking at my running processes and checking with EMCO’s UnlockIT to try figuring out what was locking my files. Looking at the Events log gives a little more accurate error that looks like this:

Source: HttpEvent
Msg: Unable to bind to the underlying transport for [::]:80. The IP Listen-Only list may contain a reference to an interface which may not exist on this machine. The data field contains the error number.
--------------------------------------------------------------
Source: IIS-W3SVC
Msg: The World Wide Web Publishing Service (WWW Service) did not register the URL prefix http://*:80/ for site 1. The site has been disabled. The data field contains the error number.

The actual issue is actually about the binding of the site. It you have any other process using port 80 (or whatever port you assigned to this site), you will get that error message when trying to start it.

To solve this problem, you have 2 options:
1. Change the port number for the problematic site’s binding to any available port
2. Turn off the service using the conflicting port (you can also assign a different port to that service)

All-in-all, this is a very minor problem that can be very easily resolved but the initial error message is leading us down the wrong path.

Hope this will help someone with the same issue

Note: my environment is Windows 7 x86 and running IIS 7.5

Update: Just finally found out that what took over my port 80 was Skype. No idea why and I have no intention on finding out so from now on, Skype is off.