Archiv for ‘coding hints’


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.

published: October 22nd, 2009

AlertDialog for Android

Are you utilizing the full potential of AlertDialog?  I had no idea it was so versatile until I starting playing around with it recently.  I knew you could use it for a yes/no dialog for your mobile application, but I didn’t know it could also be an About dialog.  The API makes it really easy, although it doesn’t seem like the setPositiveButton() method is in the Android documentation.  The code snippet below cuts it down to the bare minimum.  It appears that a null can be passed instead of an empty OnClickListener and the action simply closes the window…which is the desired effect.

AlertDialog.Builder ad = new AlertDialog.Builder( this );
ad.setTitle( "About My Mobile Application" );
ad.setMessage( "This text explains My Mobile Application, thanks for " +
                       "purchasing it from the Android Market!" );
ad.setPositiveButton( "OK", null );
ad.show();

I’ve recently cleaned up a bunch of my code with this simple Dialog. And next I may have to look into the FrameLayout feature mentioned in the Android documentation.

published: January 28th, 2009

Use Math.round not (int) - I never claimed to be perfect!

Well this post will show my ignorance towards floating point math.  So my new Android Application, which I hope to put up on the Market soon, is a shopping list which allows you to track how much you’ve spent as your are shopping.  This obviously deals with the price of the products you are purchasing so I expect the user to enter floating point values, but I convert to integers to make math functions go faster.

(more…)

published: November 25th, 2008

Android - Ruby on Rails Project Part 3: Using Android Cursors

In this post, I show how I removed the Project POJOs and marshal the XML data directly into the Android database.  Usually I have a layer of objects defining the model API and encapsulating the database, but with Android, Google has pre-written those objects in the form of cursor adapters.  Furthermore, there is little chance on a platform like Android that a dramatic change in the back end model will occur, as opposed to a larger less constrained application where a MySQL db is swapped with a Oracle database, then swapped with a distributed data system.

(more…)

published: June 25th, 2008

Rails hint, or at least a reminder for myself

Here is some coding I used to set up two simple forms to edit attributes for a simple Project/Task feature of my new application. For the life of me, I was having a hell of a time figuring out how to default the UI elements to show the current value for the model object.

I finally got it by setting adding the “task.complete” and the “task.assigned_user_id” values of the check_box_tag and select_tag.

Still to do, make the calls to the controllers restful and add in AJAX so it doesn’t refresh the entire page.

<% form_tag (:controller => 'tasks', :action => 'task_completed', :id => task) do %>
Complete? <%= check_box_tag :completed, '1', task.complete, :onchange => 'submit()' %>
<% end %>

<% form_tag  (:controller => 'tasks', :action => 'change_assigned_to', :id => task) do %>
Assigned to: <%= select_tag (:user_id, options_for_select( @users_select, task.assigned_user_id), :onchange => 'submit()') %>
<% end %>