Mental Jetsam

By Peter Finch

Archive for the ‘Programming’ Category

Android JSON WebService Example

Posted by pcfinch on September 27, 2012

The following code, for the Android OS, will make a HTTP request to a JSON webservice at “url” and convert the response into a JSONObject.

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url) ;
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpClient.execute(request, responseHandler);
JSONObject jsonResponse = new JSONObject(responseBody) ;

Posted in Programming | Leave a Comment »

Calculating Android canvas device pixels sizes

Posted by pcfinch on July 26, 2012

When drawing on a Android Canvas, it’s important to take into consideration the different pixel densities when rendering lines and text. Using “pixels” can make things too small on high resolution screens. Using DIP instead of pixels is a good way to keep things a similar size physical size on the screen and the following code can be used to map DP values to pixel based values.

int dpSize = 5 ;
DisplayMetrics dm = getResources().getDisplayMetrics() ;
float inPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpSize , dm);
paint.setStrokeWidth(inPixels) ;
paint.setTextSize(inPixels * 3);

Posted in Programming | Leave a Comment »

Hiding the Android soft keyboard

Posted by pcfinch on July 19, 2012

This sounded so simple… but it wasn’t as easy as I thought. All I wanted to do was hide the soft keyboard when I was switching (using a ViewFlipper) to a view that did not require it. This is what I ended up doing and it seems to work.

ViewFlipper flipper = (ViewFlipper)this.findViewById( ; // Any active view will do
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE) ;
imm.hideSoftInputFromWindow(flipper.getApplicationWindowToken(), 0);
flipper.setDisplayedChild(1) ;

Posted in Programming | 1 Comment »