Tuesday, 13 December 2011

Android - ProgressDialog without Dimming Screen

By default, when Android's ProgressDialog is shown, the rest of the screen is dimmed automatically. This is pretty useful in most cases, but sometimes you want to keep the content in the background clear and visible too.

This is possible albeit not overly obvious. It turns out that when ProgressDialog is constructed, it has a flag set specifying that behind the dialog should be dimmed. As passing in 0 to the constructor of the dialog as the chosen style doesn't work, you actually have to create the object, and then remove the offending flag manually.

An example follows, where this is an Activity:

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();
    case 200:
        HttpEntity entity = response.getEntity();
if(entity != null)
String responseBody = EntityUtils.toString(entity);

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:
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.