Elad Katz - Cool Stories

Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, June 23, 2009

Pico Post - New Google maps on Android

Google has released a new version of the included maps application. This is available for download through the market - which is kind of odd since it is part of the OS (just think if they told you to update the calculator through the market...)
But anyway, here it is - and finally we have Google transit and walking available for the Android!
What else is new?
Voice search
better support for Latitude
Reviews and more business information


http://www.google.com/mobile/android/maps.html

Tuesday, May 12, 2009

Running the Android API demos on the 1.5 SDK

As you may know, version 1.1 of the sdk came with a bunch of examples in the samples directory.
A huge part of that was the APIdemos project that was not included in 1.5 for some reason.
So if you do want to run it on the new SDK, this is what you have to do:
a) Download and extract the old (1.1) SDK here
b) Import the API project into your eclipse workspace
c) Change the project properties so that the project build target will be the Google APIs
d) Create a new AVD with the Google API target. see here for how-to's
e) Upadte the run configuration so that the target AVD will be the newly created one
f) Optional: you might find that in cameraPreview.java Line 69 you will get an error - an uncaught exception kind of thing. that is because the setPreviewDisplay method now throws an IOException that needs to be handled - just 'try catch' it and you'll be fine
If you need any more help, please comment here and I'll answer any questions you might have.
good luck

Monday, May 11, 2009

1st Pico-Post: How to add an SD card to an existing AVD (SDK 1.5 - Cupcake)

Hi, so first things first - what's a pico-post?
well it's basically a cool geeky way of saying a short post - I made it up and hopefully I'll make a few of those every once in a while.
Now for the pico-post itself:
With the shift to SDK v 1.5 the way that the android emulator works has been changed - you can now create several customized AVD's (Android Virtual Devices) that have different properties - this way you can test your app on several types of emulators and simulate different types of display settings etc.
The only problem is that the guys in Android forgot to add an update command to an existing AVD to add an SD card emulation.
This was much easier in the previous version of the API since all you had to do then is create an SD card and then run the emulator with a command line paramater saying - use this card (see here)
But in the new AVD that doesnt work, so we're gonna have to patch it up a bit
so for now here is the guide \ tutorial on how to add an SD card to an existing AVD in android SDK
1- find the path where your avd is stored by running the android list avd command:

$ SDK/tools/android list avd
....
Path: /home/user/.android/avd/myAVD.avd
...

2- create an "sdcard.img" in that directory -- that name is the
default and the emulator will pick it up automatically:

$ SDK/tools/mksdcard 16M /home/user/.android/avd/myAVD.avd/sdcard.img

You can pick any size you want\need for it

Note that "list avd" will not display it after (it only displays what's in the myAVD.avd/config.ini file...) so if you want it to show you can add a line like "sdcard=16M"

That's it.

Thanks to Raphael from the android handset alliance for helping me out on this!

Monday, May 4, 2009

Overcoming the AbsoluteLayout deprecation, Part Deux

Okay, so I fixed the bottom part as well - it was not fun...
first thing I did was to replace the absolutelayout to linearLayout
Surprisingly there were no compilation errors however when I tried running it, it crashed...

So I went about fixing it, but in order to explain the process I'm gonna have to give a little background, it's gonna be as short as possible, I swear!
The project I'm currently working on is a card game, and in it there is a method for redrawing the player's cards. It does this by going over the logical card objects and checking some properties and drawing them according to it. One of these properties is the "isSelected" status of the PlayingCard object. According to this property, we draw the card - if it is indeed selected - we show it as slightly (10px) above the rest of the cards, else, it is in line with the others.
One final thing to note is that this method is called after every action is done on any cards, and that this method is called and will henceforth be referred to as redrawHand().
The redrawHand() method used to draw the selected cards using absouluteLayout like this:
boolean isSelected = hand.isCardSelected(i);
android.view.ViewGroup.LayoutParams currentParams = cardView[i].getLayoutParams();
cardView[i].setLayoutParams(
new AbsoluteLayout.LayoutParams
(currentParams.width,currentParams.height,
((AbsoluteLayout.LayoutParams)currentParams).x,
isSelected ? 0 : 10));

basically, I would set the 'y' location (relative to it's container) to 0 or 10 according to the isSelected status.
Unfortunately, this is not possible in other layouts. and that's why the run failed on the first run after the change.
what I did next is try to use some of the properties inherited from LinearLayout to acheive the same result. I tried:
  • setting the Layout_gravity of the selected cards to top and the others to bottom
  • setting the Gravity of the selected cards to top and the others to bottom
    • (Ever wonder what the difference is between Layout_Gravity and Gravity? well, it's simple - Layout_Gravity is the gravity of the view in relation to it's parent and Gravity is the gravity of the contents of the view itself)
  • setting the bottom_padding to 10px for the selected cards
  • setting the layout_margin_bottom to 10px for the selected cards
  • setting the height to 10px bigger for the selected cards
  • and other fun stuff...
as you can imagine, none of these worked - most of them did the same thing - when one card is selected, the others appear selected as well - the cards go up as one...
after allot of digging and frustration i figured it out - the container for the cards was in a wrap content mode in layout height and when one of the cards was selected, all the cards filled the content of the parent and in effect went up. now I know this doesn't make a whole lot of sense now, but after a while you get used to these things and they start making sense...
So I managed to get it to work in the layout xml (was able to change the bottom margin for 2 cards and see them up) , but when it came to run-time that was a whole different story: for some strange reason - when I clicked on one of the cards, it did seem selected, but it would be the only one I could select - now this was really weird for a few reasons: a) the method worked with absoluteLayout and b) the behaviour was extremely strange: if I pick card 2 and then card 3, card 2 will pop up but 3 won't, and if I would want to show 3 as selected, i would have to click it again (to unselect it) and then unselect card 2 and then select card 3.
WTF???
after posting yet another question on the group I was told to use the container.requestLayout() method at the end of the redraw method. What this method does is basically invalidate the view so that on the next drawing cycle, it will appear as dirty and will have to be drawn again by the graphics engine. Now this all would make perfect sense if it werent for the fact that I have have been using this method for a whole bunch of other stuff and they worked and the fact that have something that's supposed to do just that - the container.invalidate() method.
Amazingly - this fixed it - I'm still not completly sure how this miracle occured but I'm gonna get to the bottom of it and let you know if there are requests for that.
so now it all works, and you are all encouraged to go the project site and look at the code - should be intersting...
http://code.google.com/p/bestcardgameever-android/
Enjoy!

Overcoming the AbsoluteLayout deprecation, Part I

Hello constant readers :)
As you have probably read in the previous post, I've been having some problems with the new 1.5 SDK, one of which was to get the AbsoluteLayout removed from my project since it has been deprecated...
Now as I showed you before, I have two instances of it in my project - one for the thrown cards (in the middle of the screen) and one for the player hand (bottom of the screen).
The reason that I used the Absolute Layout for the thrown cards was that I needed to show cards that were overlapping. Now even as I was writing it, I thought to myself that using absolute layout would be a bad idea - why? because unlike the Iphone, the android system is not limited to one device and one screen size - at this time, there are NetBooks with 12" screens with a resolution of 1024X768 that can run android as well as more than 10(!!!) new devices scheduled to come out this or next year. So that means you cant count on absolute positioning by pixels because they can change and the game should work on all of them...


Android Netbook

So the mistake came back to bite me in the ass, and I needed to fix it, only question is how???
After making a few inquiries (1,2) it was pretty clear what I had to do - make a custom layout - and once again the question remained how...
I was starting to read up on that but have decided that before I'm going to go head first into a world of unknown pain, I'm gonna play around with the properties of the layouts I know.
Here is the process in pictures:







My first attempt:
I decided I'm going to use a FrameLayout instead of my AbsLay and add left padding for each card, thus creating an overlap...
what I got was kinda weird (Isn't it always in UI?)
The cards got smaller as the padding got bigger.
so I had to scratch that idea...



Attempt 2:
Tried using left margin instead of padding - that didn't work, all that happened was that all the cards were on the same location - you could say that they were overlapping at 100% :)
scratch that too...



Attempt 3:
Switched to LinearLayout and gave it some left margins - notice what happened there? the margins made the cards seem further apart instead of overlapping - and when you think about it - it makes sense - so what did i do? see attempt 4 below...

1 - one card on the table


2- several cards on the table


Attempt 4:
You guessed it!
I used negative values...
as you can see - that got me a little closer but not quite there yet...
for some reason - the first card had almost no overlapping and it seemed like the overlapping between the last two cards was exactly what i was looking for
so I was encouraged and made some more attempts...



Attempt 5 :
here I tried reversing the order of the margins - so the card were now arranged in ascending negative margin order - I got the exact opposite of the previous attempt...
wanna guess what I did next?
I'll give you a hint - in attempt 4 I used descending negative values between cards and in 5, ascending...
guess!




Attempt 6.1
Perfect, almost...
Here I used a constant negative left margin (of 10 pixels)
only problem was that I also sized the frame that contains all the cards with the sum of the cards images so it won't shrink the cards on the right and left of it and for it cropped the right of that King card there...



Attempt 6.2
So I gave it a little extra - what do I mean?
The cards were 72px wide each, I have 5 cards and a margin of 10 for each card so it should have been 72+10+10+10+10 = 112
it worked with 130 :)
good enough for me :D
so there you have it, success, sorta.

So I managed to do it without creating a new Layout, but next I'm going to have to change the layout for the bottom cards, will I be able to do that without creating a new layout? It's gonna be fun finding out!
Stick around for more, and comment if you like it or if you feel something is missing!
See you soon
Elad Katz

Friday, May 1, 2009

Cupcake

So cupcake is out - also known as version 1.5 of the SDK.
As the early adopter that I am, I immediately went about to upgrading my project.
updating the ADT in eclipse was really quite simple (followed the instructions at http://developer.android.com/sdk/1.5_r1/upgrading.html ), but once the upgrade was complete the problems started...
First thing was that now you have to make you own Emulator (now known as AVD - Android Virtual Device) - that went pretty smoothly, the only thing that kinda bothered me is that the camcorder does not work in the emulator but according to google, it's not supposed to...(http://groups.google.com/group/android-developers/browse_thread/thread/a32a2f351f2fa004/9811bcf7bef9254d#9811bcf7bef9254d)
the second thing i noticed was that the device is a little sluggish but we'll see about that
third thing was that since the new API contains a new Widget for a sliding drawer

i of course tried to put it in my project but failed due to an exception which google is looking into:
http://groups.google.com/group/android-developers/browse_thread/thread/c579d40ff8bca172/58d4fcef1762259a#58d4fcef1762259a

next thing i noticed is that absoluteLayout is now deprecated :(
i knew it was a bad idea to use it the second i used it...
The reason i needed it was this:
notice the cards on the middle that are overlapping?
see the bottom cards that have some sticking up?
i'm gonna have to figure out a new way to do that without using the absolute layout...
stay tuned here or at http://code.google.com/p/bestcardgameever-android/
it'll show up soon