Sunday, February 12, 2012

Styling an Android button the easy way

There are a lot of tutorials on how to this, even the android developers documentation talks about the 9-patch tool.
It can be a bit confusing but I guess if you've got the time you should always read as much as you can on each topic.

The easy steps are:
  1. Just open your favorite png editor (GIMP?) and create the background for your button
    Raw button
    This only takes 30 seconds in your favorite graphics editor.
  2. It is at this point where you need to open the "draw9patch" tool you can find in your "android-sdk/tools" folder.
    Using this tool you open the file you just created.
    draw9patch tool
    You see I've already marked the areas I want the button to repeat. It will tell Android that which parts of the image it should tile/repeat when re/sizing the background to fit buttons of different sizes.
    It also shows you the preview in the right side.
  3. Now to save your image. Point the draw9patch to your "res/drawable" folder and give the image a name. Let's call it "green_button_style.png". After save, you'll notice the tool has actually created a file named "green_button_style.9.png". Don't let the additional "9" fool you. When using the resource identifier, it will be left out.
  4. Now you'll want to point the background of your buttons to this graphic. Open your layout's xml and  find the button you want to style. Add the background attribute (android:background) and use the identifier for your new image which is "drawable/green_button_style" (as I said, you leave the "9" part out).

    Code for the styled android button
That's it, you're done!

Sunday, February 5, 2012

Android Google MapView Tutorial - FIX

If you're a newbie like me in the Java/Eclipse/Android business, you probably started by doing the tutorials on developers.android.com
For those of you who got to the google maps tutorial, here are a few fixes of the issues you found:

Since we've defined a constructor that takes a context as a parameter
(HelloItemizedOverlay(Drawable defaultMarker, Context context)it would be a shame not to use it, right? They probably intended to call it but forgot to update the code. So replace
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);in the HelloGoogleMapsActivity file inside the onCreate method with this
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);For the next problem, which is not seeing your icon after you made this changes, make sure you redefine your constructor like this replace
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
       super(defaultMarker);
       mContext = context;
}
with this
public HelloItemizedOverlay(Drawable defaultMarker, Context context) { 
       super(boundCenterBottom(defaultMarker));
       mContext = context; 
}

MapActivity cannot be resolved to a type

So I got this error while trying to create an Android project to use Google Maps.

The thing is, my project's target was Android 2.3.3. It should have been Google API (whatever version). If you don't have a Google API for your device's API version (I didn't), just open Window->SDK Manager and download a Google API for your version.

Then change your project's target API: right click on your project -> Configure -> Android, and choose your version.

After that, you also need to import the google maps package.

import com.google.android.maps.MapActivity


That's it, you're done!

First steps to a "Hello World" application on Android

Prerequisites: check out the android developer guide


As a "java challenged" person myself, I had no eclipse or java SDK on my computer.

So I got everything I needed:

You should check this page out if you want to know how to install the ADT Plugin for eclipse.

Now the next thing everybody says you should do is create an "Android Virtual Device" (AVD). This will allow you to run/debug your application on your machine. That is, you won't download your application on a real android phone, like the one you have on your desk right now! Because let's face it, you should at least have an Android Phone if you want to write software for one!

Creating the AVD is not a big deal, you just use Eclipse. Go to Window->AVD Manager and click New. Nothing too difficult, is it?

Next, the easiest part. Choose File->New->Project->Android Project. This will create a mock Android project, which you'll tweak a bit to display your message. Just override the "onCreate" method with this one:


   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(tv);
    }



*you'll notice this only adds a couple of lines to the original method
The code for "Hello World" application, just like in all the examples on the net

Assuming you've written your "Hello World" 2 lines of code, you'd want to run the application in your newly set up AVD. Just use CTRL-F11 or choose Run->Run from the menu.

For many of us, and I can not stress this enough ->for me, this part was where I got stuck. The simulator started up no problem, up to the part with the Android flashing logo. And this is where it hung. I waited for about ten minutes, then I switched machines. I did everything from scratch, hoping to get it right a second time on this DIFFERENT machine.

NO.

It didn't work. Same problem.

So I did my way!

No just mine, but most of the people out there who write code for Android. I just used my real Android powered phone.

It sounded scary at first, but is was actually pretty easy to get it working. Just follow these steps:
  • Set up your device to allow it to be used as a development tool.  Go to Settings->Applications
    • choose to "Allow installation of non-Market applications"
    • go to "Development" and choose to use "Debug mode when USB is connected".
  • connect your Android phone to the computer using your USB cable. 
    • if you need to install the ADB driver for your phone, just do a search on google on how to do just that:   search for 'adb driver for' (add your phone model)
    • do a quick test to confirm your device is available: using command prompt, go to "C:\Program Files (x86)\Android\android-sdk\platform-tools" and call "adb device". If you see anything listed after that, then that means your device is all set up and ready to go. Your platform tools path might be different than mine, but only you know that.
If you made this far, you are both perseverent and lucky, just like me! Congratulations, you can now use eclipse to run your application on your Android device. Just hit CTRL+F11 and check your phone. It should already be running your app!
"Hello World" application on Android
Actual device screen capture