Tag Archive for 'android'

Page 2 of 3

Hide Android soft keyboard when Activity starts

If you have an EditText on your Activity layout, you might encounter on real devices, that the soft keyboard annoyingly shows up.
The easiest solution to hide it, is to set soft input mode in Activity.onCreate method:
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Faster Singleton method in Java

The fastest Singleton design pattern implementation by Bill Pugh:

public class Singleton
{
protected Singleton()
{
// ...
}

private static class SingletonHolder
{
private final static Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance()
{
return SingletonHolder.INSTANCE;
}
}

Easy shadow making around ImageView

If you want a realistic, nice shadow around an image in an ImageView you might configure it with the following layout:

Source with shadow image: Stackoverflow.com

Note This solution works best with fitXY scaleType, it does not apply to aspect corrected images. (it can be done with custom Drawable object)

Pull-to-Refresh ListView for Android

Although this kind of list refreshing is rather an iPhone feature, and not really used on Android platform, sometimes it is requested to use.
An easy to use listener-based implementation can be found here by Johan Nilsson.
Alternative version thanks to Guille Polito.

Update here is another lib, used in Friendcaster and co: Android-PullToRefresh by Chris Banes

Disabling Edittext input on Android

Disabling Edittext item in Android is not straightforward, because the setDisabled method does allow text input by user.
To prevent user editing use one of the followings:
setKeyListener(null);
or
android:inputType="none"

Source: Stackoverflow.com

Converting View to Bitmap without displaying it

Here is the Android code for converting a View object to Bitmap without displaying it. This can be useful for displaying complex views in a Gallery widget.

Bitmap bmp = Bitmap.createBitmap(Utils.getPixelForDp(width, this), Utils.getPixelForDp(height, this), Bitmap.Config.ARGB_8888);
View v = getLayoutInflater().inflate(layoutid, null);
v.measure(MeasureSpec.makeMeasureSpec(bmp.getWidth(), MeasureSpec.EXACTLY), 
MeasureSpec.makeMeasureSpec(bmp.getHeight(), MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
v.draw(new Canvas(bmp));

Best source: Stackoverflow.com

Determining GPS coordinates from address on iOS/Android

Sometimes GPS coordinates for locations aren’t stored in databases, in this case another servers should be used to determine the latitude/longitude of an object. Fortunately there are ready solutions for this problem via Geocoding.

Geocoding on iOS
Geocoding on Android

Signing Android application

Signing an Android application, is not too complicated, however could be tricky, and it is always good to have a quick summarizer tutorial about it:
http://www.androiddevelopment.org/2009/01/19/signing-an-android-application-for-real-life-mobile-device-usage-installation/

Coverflow widget for Android

Very useful example about creating a coverflow widget using the Gallery component provided by the framework:
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html

If you wish to know more about the Gallery widget, here is a good tutorial with examples:
http://android-pro.blogspot.com/2010/07/android-gallery-control.html

An older version of the widget can be found here, however this is much more complicated…