Archiv for ‘coding hints’


published: July 22nd, 2010

Go home feature with FLAG_ACTIVITY_CLEAR_TOP

Here’s a little snippet I wrote up the other day to support a Go Home feature for the app I’ve been helping with.  It sets the FLAG_ACTIVITY_CLEAR_TOP flag on the Intent along with the HomeActivity.  This flag, according to the Android docs, launches a new instance of the activity if it isn’t currently running, and closes all the activities on top of it.  So if there are any activities on top of HomeActivity they are cleared and HomeActivity is displayed to the user.  I also added a check to determine if the supplied Context is already the HomeActivity, and if so, ignore the call.

public static void goHome( Context ctx )
{
    if ( ! ( ctx instanceof HomeActivity ) )
    {
        Intent intent = new Intent( ctx, HomeActivity.class );
        intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
        ctx.startActivity( intent );
    }
}

published: July 1st, 2010

Using layout include tag with Location proof of concept

I’ve been prototyping some code using the location api and not only have I learned some new things about this but also worked in the layout ‘include’ tag to minimize the amount of redundency in the layout file, and the Android Java code.  Include allows you to include one layout file in another, there’s an article about it on the Android dev site.  One minor speed bump I ran into was that I didn’t see an immediate way to access the resuseable portion of the layout file via a findById call.  After a little hacking around, the answer was pretty easy.

(more…)

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

published: February 5th, 2010

Overlay a bitmap on another

As I’ve been alluding to in my Twitter feed, I’m on the cusp of deploying a new version of MediaDroid, huge re-write to image gallery, but hopefully I’ll get a post about that out later.  Anyway, the previous way I was marking the images to be included in the albums was to have a combo view with a check box and a thumbnail of the image.  Although this works, it takes up a lot of screen real estate and isn’t terribly efficient.  The next version of the image gallery I wrote extended the Android ImageView class and although this solution isn’t much more efficient (the solution for that is in my newest version), it certainly increases the number of images viewed at a time.

(more…)

published: November 3rd, 2009

Is GPS on? If not prompt user to turn it on.

Here’s a quick code snippet to determine if the GPS is on, and if it isn’t go to the Setting screen allowing the user to turn it on.  Android does not allow a 3rd party app to automatically turn the GPS on/off as of 1.5, a “feature” discussed extensively on the Android Developers Forum.

(more…)

published: November 2nd, 2009

Searching for phone numbers with Android

I’ve been playing around with searching for contacts on the Android platform and thought I’d pass along a couple things I’ve learned.

One thing I’ve found out is that some of the classes from the books I’ve been using as reference have been deprecated.  Apparently, instead of using People.NUMBER, which has been deprecated for future support of multiple accounts, I should use the ContactsContact set of interfaces to get the contacts name and number.  Guess I’ll be upgrading to the Android 2.0 sdk sooner then I thought.  Although according to this forum thread the previous interfaces used to specify contact column, such as People, are still supported and will access contact information from the “primary” account.

Another cool thing I figured out was how to have the phone pad soft keyboard attached to an EditText widget.

device.png

It’s one of those, it was so easy I’m ashamed I didn’t know it, but then again, the SDK is large enough to where it’s hard to know ALL of it.  Anyway, all you need to do is define the “inputType” attribute value for the EditText widget which captures the phone number.

   	<EditText android:id="@+id/phone_nbr"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:inputType="phone"
		android:singleLine="true" />

Now, when the phone_nbr EditText box is entered, the phone pad comes up for data entry instead of the default Android soft keyboard.

There’s still many aspects of the Contacts API I haven’t investigated yet, but hopefully with the new apps I have the opportunity to work on, I’ll have lots of time to learn more about them.