Archiv for June, 2010


published: June 28th, 2010

Multiple calls to addPreferencesFromResource from PreferenceActivity

Just found this out, maybe it’s obvious, but something new for me.  When you create a PreferenceActivity you call addPreferencesFromResource with the name of the resource file defining your preferences to display in the activity.  You can make multiple calls to the addPreferencesFromResource method; this gives your Android application a way to show preferences that may be specific to that component of your app by breaking your preferences up into multiple resource files and aggregating them as needed.

I’d think if this functionality was used you’d want one screen that would aggregate all the preferences (maybe a Dashboard or Home screen), but individual activities could call other preferences activities that would further filter the preferences specific to that activity.

Just another option that you may find useful in your app….cheers!

published: June 17th, 2010

Android Item Resource has a Name and Type

Here’s a handy trick to use instead of constants in your code.  It uses the Item resource tag to define application specific constants which are located in a resource XML file.

For example, given the following XML located in a file in the res/values directory

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <item name="host" type="setting">10.0.2.2</item>
 <item name="port" type="setting">3000</item>
</resources>

I can now use the following Java syntax within an Android Activity:

String host = getString( R.setting.host );
int port    = getResources().getInteger( R.setting.port );

This gives me a scheme to have a common place with ‘types’ to define constants. I’ve just barely started using it, but it seems like a nice clean way of handling these ‘magic’ values without using static final variables.

published: June 10th, 2010

ArrayAdapter and AlertDialog for single choice items

I thought this was pretty cool…I used the AlertDialog.Builder, an ArrayAdapter along with the View Holder Android UI pattern to build an AlertDialog containing a list of choices with images for each entry.

(more…)

published: June 5th, 2010

Cropping images from a png file in Android

So I’ve never tried to crop single images from a multi-image bitmap ( like these images ), but after some tweeking I ended up with the following code snippet, I’m sure it’s not the only way, but it seems to work out pretty well.

private Drawable getImg( int idx )
{
    InputStream is = getResources().openRawResource( R.raw.myicons );
    BitmapDrawable bm = new BitmapDrawable( is );
    try {
        is.close();
    } catch (IOException e) { /** noop, stream is closed **/ }
    BitmapDrawable img = new BitmapDrawable( Bitmap.createBitmap( bm.getBitmap(), 22 * idx, 0, 22, 22 ) );
    img.setBounds( 0, 0, 40, 40 );

    return img;
}

Of course, you’ll have to supply your own myicons.png in the /res/raw directory and a corresponding size (mine is 22).  You’ll also want to change the code so the BitmapDrawable “bm” is only loaded once, most likely in a constructor, then used multiple times from the above getImg method.

published: June 4th, 2010

setCompoundDrawable to add Drawables to TextView

Finally trying to get a couple new posts up, have a bunch of material just not a lot of free time to get ‘em posted.  Here’s a quick and easy one.

I was using the setCompoundDrawable method for a TextView to set an image to the left of some text, I was hoping it was going to be as easy as loading the drawable and calling

txtVw.setCompoundDrawables( img, null, null, null );

Unfortunately nothing showed up, and I had no internet connection at the time, so I was left to fumble around trying a bunch of different things…nothing worked.  Finally, after getting to work and looking at some existing code from a co-worker, I found out that using setBounds will define a bounding rectangle around the image.  So the final lines of code look like…

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );

UPDATE: See comments to make this even easier, thanks Romain Guy and Nerdrow