mgmblog.com

April 19, 2011

New version of Jumpy

Filed under: Android Apps — Tags: — michael @ 8:30 am

jumpy_shot_2.png

Got the newest version of Jumpy up on the Android Market.  Fixed the board layout so it’s saved when the screen orientation changes, and going to the Settings screen.  Also some new graphics from Jason at embracedesign.com, he’s been a huge help! I love the new crop circles.

Jumpy has been a great app for me because it’s really helped me learn how the who deployment process works without having to maintain a huge code base.

More to come, got an idea for something called challenges, hopefully it won’t take me to long to figure out how to implement it.  I’m sure more design changes will make their way in also, maybe even get some animated stuff in.

Thanks for checking it out, just in case you missed the link above, you can find Jumpy here, on the market.

March 28, 2011

Using an Android View to display a line

Filed under: Android, coding hints — michael @ 10:49 am

Here’s a cool trick I’ve used a couple times to display a horizontal line on the screen between widgets. It uses an Android View with a background color.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        style="@style/WordsOfWisdomText"
	android:text="Turn off your lights to safe energy"/>
    <View
	android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="#FF909090" />
    <TextView
	style="@style/WordsOfWisdomText"
	android:text="World peace is a reality"/>
    <View
	android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="#FF909090" />
    <TextView
	style="@style/WordsOfWisdomText"
	android:text="Eat your vegetables"/>

</LinearLayout>

(more…)

March 23, 2011

Inflating a view for a AlertDialog

Filed under: Android, Jumpy, coding hints — Tags: — michael @ 11:26 pm

I’m in the process of updating Jumpy not only to give it some polish, but also learn more of the ins and outs of the Android Market marketing capabilities…hopefully I’ll also figure out how to insert advertising and possibly make a little extra cash.

Anywho, as I’ve been updating some of the code, I came across my custom AboutDialog and decided it needed to go into an AlertDialog instead.  By using the code below, I was able to use the same layout resource of the AboutDialog (removing a couple widgets) and fully utilizing the AlertDialog by instantiating a View via the LayoutInflater and setting it into the AlertDialog.

    private void openAboutDialog()
    {
    	AlertDialog.Builder dialog = new AlertDialog.Builder( this );
    	dialog.setView( getLayoutInflater().inflate( R.layout.about_layout, null ) );
    	dialog.setTitle( getString( R.string.about_jumpy_title ) );
    	dialog.setPositiveButton( getString( R.string.done ), null );
    	dialog.show();
    }

It’s really nice that with the getLayoutInflater().inflate call you only have to supply the resource since the root ViewGroup is optional.  Now I’m able to get rid of the AboutDialog class and remove 50 some odd lines of code from the applications.

January 1, 2011

Happy New Years, Simple pattern for opening an Android Activity

Filed under: Android, coding hints — Tags: , — michael @ 5:56 pm

Another New Year and like many other bloggers I’m thinking about resolutions of writing on my blog more often.  Actually, my resolutions are a little grander then just this blog, but I’m still ironing them out.

So instead of going into my life/work goals of 2011 I’d rather share a code snippet/Android pattern I’ve been using quite a bit lately.  It grew from working with other Android developers and wanting to specify a mini API for opening an Activity.  Many Activities pass data when they open another Android Activity; using Intents, name/value pairs can be defined for the data elements.  One problem is though, you must use code inspection to determine what data is passed in, and what the format it is.  What I’ve started to do is use a static method in the Activity as a means of starting it.  Here’s a simple code snippet to demonstrate what I’m talking about

public class ActivityOne extends Activity
{
    private static final String VALUE1 = "value1_param";
    private static final String VALUE2 = "value2_param";

    public static void startMe( Context ctx, int val1, String val2 )
    {
        Intent intent = new Intent( ctx, ActivityOne.class );
        intent.putExtra( VALUE1, val1 );
        intent.putExtra( VALUE2, val2 );
        ctx.startActivity( intent );
    }

    private int _value1;
    private String _value2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_one );

        Bundle extras = getIntent().getExtras();
        if ( extras != null )
        {
            _value1 = extras.getInt( VALUE1 );
            _value2 = extras.getString( VALUE2 );
        }
    }
}

This way ActivityOne can be started from another Android Activity using the following call,

ActivityOne.startMe( this, 123, "abc" );

This places the actual means of defining the Intent for ActivityOne in one place, and if this contract is changed, ie adding another variable or changing types, it can be changed in the method signature of startMe and all the places this is called will fall out using a coding tool like Eclipse.

Hope this little snippet can give you some ways on writing cleaner Android code, Happy New Years!

July 22, 2010

Go home feature with FLAG_ACTIVITY_CLEAR_TOP

Filed under: Android, coding hints — Tags: , — michael @ 8:30 am

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 );
    }
}

July 1, 2010

Using layout include tag with Location proof of concept

Filed under: Android, coding hints — Tags: , — michael @ 10:26 am

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…)

June 28, 2010

Multiple calls to addPreferencesFromResource from PreferenceActivity

Filed under: Android, coding hints — Tags: — michael @ 12:50 pm

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!

June 17, 2010

Android Item Resource has a Name and Type

Filed under: Android, coding hints — Tags: — michael @ 8:24 am

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.

June 10, 2010

ArrayAdapter and AlertDialog for single choice items

Filed under: Android, coding hints — Tags: — michael @ 8:55 am

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…)

June 5, 2010

Cropping images from a png file in Android

Filed under: Android, coding hints — Tags: — michael @ 11:08 pm

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.

Newer Posts »

Powered by WordPress