Monday, 12 December 2011

Android - Show Soft Keyboard When Activity Starts

This morning I needed to have a soft keyboard (AKA, the on-screen keyboard) showing when an activity launched. Why? Well it was login screen for my app, and the input text field would have focus by default so there was no need for the user to tap on the field prior to the keyboard appearing.

I came across code-based solutions which didn't work for me unfortunately.

The solution was thankfully available using a manifest-based approach.


Tuesday, 6 December 2011

Android - Get body of HTTP Response as String


Making HTTP requests in Android often results in the same type of boiler plate code being produced. One in particular that annoys me is having to get the response body as in InputStream and using BufferedReader and the likes.


Bundled along with Android is the org.apache.http libraries, which provide classes for easy interaction for common HTTP-related activities.


The following snippet shows how to perform an HTTP GET on a URI, and turn the response body into a string. In my case, I take the string and turn into a JSON object, but you might parse as XML or whatever.


DefaultHttpClient http = new DefaultHttpClient();
HttpGet httpMethod = new HttpGet();
httpMethod.setURI(new URI(SERVER_URI_GOES_HERE));
HttpResponse response = http.execute(httpMethod);
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode)
{
    case 200:
        HttpEntity entity = response.getEntity();
if(entity != null)
{
String responseBody = EntityUtils.toString(entity);
                 
        }
        break;

Thursday, 1 December 2011

Android - Creating a new Activity in Eclipse

The method of creating new Activities in Eclipse is somewhat counter-intuitive if you ask me.

Open your manifest file, which is likely called "AndroidManifest.xml" if you've stuck with defaults. You should see multiple tabs at the bottom, which will give you different views on the one manifest file. Choose the "Application" tab.

You'll see a horribly cluttered screen here, with one of the sections named as "Application Nodes"; this lists all of your existing Activities and allows you to add new ones. Nodes...? C'mon Google!! Anyway, hit the "Add..." button.

You'll see the following prompt:


Choose "Activity" from this list. This will create a new section named as "Attributes for Activity" to the right of "Application Nodes". Don't be tempted to go filling in values yet. See the hyperlinked text, "Name*", yup, click on that:



This will launch the "New Class Wizard" with the superclass already set appropriately for you. Name your class, choose your package as you normally would.

The last step is to add a new layout XML for the Activity (assuming you need one). Under your project, there will be a folder named "res", and under that a folder named "layout". Right click on "layout", and choose "New", then "Other...". Select "Android XML Layout File" from the list.



Give it a name that corresponds to your Activity in some meaningful way, but you are limited as to what characters you can use:
 File-based resource names must contain only lowercase a-z, 0-9, or _
Once created, open your new Activity class up, and you'll see the onCreate method has been templated for you. After the line, super.onCreate(savedInstanceState); add the following:
setContentView(R.layout.name_of_activity_layout_file);
If Eclipse fails to autocomplete once you have typed R, it could be because it has imported android.R automatically and not your own R class. Simply the delete the following line if it exists:
import android.R;
After that, organize your imports  (CTRL+SHIFT+O) and if prompted, choose your own R class and not the default android.R.

Saturday, 12 November 2011

Typing into Windows Phone 7 Emulator

Little tip that can save a ton of time for when typing using the emulator. The on-screen keyboard will jump up by default and allow you to use the mouse to click on the keys you are after. However, by default, it will not allow you to use your computer's keyboard to input text.

A very counter-intuitive way around this, is when the on-screen keyboard is shown on the emulator, simply hit the PAGE UP button on your machine's keyboard. This will dismiss the keyboard and let you type at full speed.

And yes, PAGE DOWN will allow you to restore the on-screen keyboard.

Monday, 31 October 2011

Setting up Synology DS411J NAS Drive

I recently made the big decision of buying a NAS drive. I have previously relied upon external USB drives and they have been fine up until recently. My previous system was to buy a hard drive, use it until it neared being full, buy a bigger one, copy everything over, stop using the old hard drive (too small by comparison now) and rely fully on the new hard drive. This is madness. If you do this, and you like your data, this is madness. Why? Well, you have everything you own on one hard drive. One drive, which could fail at any moment. Not to scaremonger too much, but I began feeling pretty uneasy about this.

My solution was to buy a NAS box which can hold up to 4 drives. I only bought two hard drives (each 2TB) to go inside the NAS, but the box itself can support up to 12TB. I decided to set this up in a mirroring setup, meaning whatever is added to one drive is automatically written to the other. Obviously then, my 2x2TB hard drives only allow for 2TB storage (and not 4TB) but with the huge and potentially live-saving advantage that if any of the drives failed, there would be a perfect mirror still with all my data intact.

For the NAS drive, I opted for the Synology DS411J (read details on Amazon) and for the hard drives themselves, I opted for Samsung HD204UI Spinpoint 2TB SATA 3.5" Hard Drives (read details on Amazon). Or scroll to the bottom for the Amazon links widget.

Not the ugliest box

Thursday, 27 October 2011

iPhone Development - Turn On Automatic Reference Counting (ARC) on a Single File in a Project


Off the back of my other post, Turn Off ARC, which instructs you how to configure Xcode to disable ARC for individual files, this post is the polar opposite; how to enable ARC for individual files.

To enable ARC for a single class, you should go to the "Project Navigator", click on your project, and then select the "Build Phases" tab. From the "Compile Sources" dropdown, find your class on which you wish to enable ARC. Note, you enable this on the .m implementation files, not the .h header files.

Notice that there are two columns here, "Name" and "Compiler Flags". Double click in the relevant "Compiler Flags" column for your desired class, and the following:

-fobjc-arc

Clean and Rebuild your project to pick up these changes. Note, as far as I know, you cannot apply this to whole directories in one go, and have to apply it one file at a time.

Wednesday, 26 October 2011

iPhone Development - Turn Off Automatic Reference Counting (ARC) on a Single File in a Project

Xcode has introduced ARC from version 4.2 and above (compatible with iOS 4.0 and above) and it is excellent. All new projects you create will have it enabled by default (you can uncheck this during the new project wizard if you so desire) and that is just lovely.

But what if you need to import legacy code, or another party's source code that hasn't been coded with ARC enabled? You need to remove all manual memory management commands such as release, retain, autorelease etc... if you want to enable ARC. So if your legacy or third party code has these in it and you can't or don't want to edit the code, then you'll have to tell Xcode that ARC is disabled for these files.

Thankfully, Xcode allows you to have ARC enabled for the whole project, but disabled on a select number of files.

To disable ARC for a single class, you should go to the "Project Navigator", click on your project, and then select the "Build Phases" tab. From the "Compile Sources" dropdown, find your class on which you wish to disable ARC. Note, you disable this on the .m implementation files, not the .h header files.

Notice that there are two columns here, "Name" and "Compiler Flags". Double click in the relevant "Compiler Flags" column for your desired class, and the following:

-fno-objc-arc


Clean and Rebuild your project to pick up these changes. Note, as far as I know, you cannot apply this to whole directories in one go, and have to apply it one file at a time.


For details on how to enable ARC for individual files, see my other post, Turn On ARC.