2010/11/25

Gallery, clicking and selecting

Android SDK includes a nice-looking component named Gallery.
It's supposed to provide you with functionality of flickable view-changer.
It get's an BaseAdapter and displays views it receives.
All good until you actually start using it.

First issue: 
setSelection(index, animated) method ignores the second parameter. There's no 'todo' anywhere, 'deprecated' or even acknowledgement from Google. It just doesn't give a crap what you pass there. It always navigates to your item without animation.
Now let's say that I want to change items in the gallery every X seconds. How can I make a pretty animation showing this lovely change? By hacking the bastard. Basically, the animations are initiates by the Fling motion that the component detects. If you simulate the correct fling - you will get the View flip animation.

gallery.onRealFling(null, null, -800, 0);
Here's a tricky part - the X velocity parameter needs to be adjusted per application, because to fling a larger view, you need a bigger velocity. So play with that value until you get a smooth transition.


Second issue:
Items inside the layout that you return in your adapter cannot receive click events. They just can't. No good reason for it, but someone botched up the bubbling up of mouse events in the Gallery, so now you can't have clickable items. If you set any one of them as clickable - the gallery stops handling dragging.
But fear not - you can fix it!
The solution is as follows - you add a touch listener to the gallery, and catch a Single Tap event (I did it with gesture listener), to differentiate it from the dragging that the gallery needs to handle.
Then, from the the location of the touch event, you can calculate where exactly inside your sub view the click was made - and from that you can handle the actual click.

Here's the code:

2010/10/24

Making a real marquee out of Android TextView

Android TextView supports a Marquee ellipsis method, which starts animating the text in the TextView if it's width exceeded the width of the view.
But let's say you want it to animate the text constantly? Here's a classic trick - pad the string until it goes out of the width of the view, so it will start the scrolling.


The hard part here was to find out how to calculate the line width (since the TextView itself doesn't supply that info. Had to dig android sources for that.

2010/09/22

A PDF Intent

Here's a naughty bit of behavior from the Android's Intents.
When you want to customize the mime-type of the requested intent, the setter for reason resets the previously set Data parameter. Cute right?
And to overcome this great issue, the API has another method setDataAndType(). See what they did there?
Anyways - here's a correct method (opening a PDF in this example):



NullPointerException Confusion

It's good to know that our field is called Computer Science. We're scientists. We like facts, laws and rules... Yeah, right. You think that if you've learned the basics of Data Structures, Algorithms and other courses in your B.Sc. it would prepare you for the real world. Nope. It's craaaaazy out there.

Today I had a real head-scratching moment when a completely innocent 'hashtable.put(key,val)' started giving me NullPointerException. Now, since I do know how hashes work, all I checked was - is the table itself null ? Nope, it's good. So what the hell?

Here comes the scary part:

put

public V put(K key,
             V value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.The value can be retrieved by calling the get method with a key that is equal to the original key.
Specified by:
put in interface Map<K,V>
Specified by:
put in class Dictionary<K,V>

Parameters:
key - the hashtable key.
value - the value.
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.
Throws:
NullPointerException - if the key or value is null.
Apparently the usual approach is not good for the Java folk. They don't want no stinking nulls in their precious hashes!
The reasoning behind the idiocity is this - the default behavior of get() method in hashtable is to return null if key was not found. So when the designers of the API chose that wrong path, they had to follow it up with a wronger path, since they couldn't distinguish between a null from 'no key found' and 'null found  as a value'.

I hate people.

2010/09/15

GridViews, Buttons, Oh My.

In Android, there is a versatile class called GridView which can display any custom designed cells. You provide it with a layout for each item, fill in the values - and voila - it's working.
All is well, until you try to add something interactive to the Grid cell - like a button - you come across an interesting effect.
If you add a onClick listener to both the GridView and the Button inside the GridView - only one will get the click event. And you won't guess which one :) (hint: it's the button)
So basically, you can have interactivity - but not paired with OnItemClick events of the Grid.
Bummer, right?
Wrong. This can be rather easily fixed, and the problem lays in the way Android handles focus resolution between parent and child Views. For ListView you can play with Focusability parameter which controls who reacts first, and on GridView, just the the Button's Focusable parameter to false - and it just works. It still gets the touch events which cause the click, but it doesn't interfere with the focus mechanics of the Grid.
You're welcome!

2010/07/18

The Saga of COMException (0x800736b1)

So I had a very lovely weekend being sexually assaulted by a pesky problem while installing an ASP.NET application on a Windows XP IIS server.


Server Error in '/AppName' Application.


This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)



Exception Details:System.Runtime.InteropServices.COMException: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)


Ok, this is lovely. Tracing the error code was pretty straight forward and pointed to the lack of VC++ Redistributable DLLs on the target machine. They are present on the developer box, but are missing on target. Sounds easy enough, downloaded vc_redistx86.exe , installed - same result.

Excellent. So, using depends.exe, I checked that the DLL in question is looking for msvcr80.dll and friends. Curiously enough - they were installed on the system, but the problem persisted, the application just didn't pick them up.

So I dug some more, and came across a folder \Windows\WinSxS (http://msdn.microsoft.com/en-us/library/aa376307(VS.85).aspx). It's Microsoft's solution to DLL Hell. There are several versions of the same CRT DLLs, in subfolders with different version names.

Which pointed me to the fact that it's not that the DLL is missing, but it's of incorrect version. So what was left is to discover how exactly does the application select which DLL to load. (it's obviously not by name!). So after frantic Google searches I came across a beast whom I met before but didn't pay attention to - "XP Manifest". 

It's a XML file which is embedded inside the application/DLL as a resource, and can define properties like 'Dll dependency'. So a quick run of ResHacker, patching the XP Manifest, saving the new app, re running, seeing that it works and then promising my self to tear away the nads of whomever though it would be a great idea to make me spend my weekend tracking this shit, and not just showing me a freaking help message!

2010/07/05

Bitmap Button in Blackberry

I won't bitch about the state of Blackberry UI framework, just mention that there's isn't a standard button class that supports images. WTF?
In any case, here's the code that implements one.

Open an external browser on Android

Ok, so I wanted to open an external browser from my Android application. Crazy, right? Anyways, after some searching and trying to read almost empty Android SDK docs on the subject, I came across this golden turd:


        try{  
            Intent i = new Intent();
            ComponentName comp = new ComponentName(
                             "com.google.android.browser",
                                    "com.google.android.browser.BrowserActivity");
            i.setComponent(comp);
            i.setAction("android.intent.action.VIEW");
            i.addCategory("android.intent.category.BROWSABLE");
            Uri.Builder z = new Uri.Builder();          
            i.setData(z.build());
            getContext().startActivity(i);
        } catch (URISyntaxException e) {
                e.printStackTrace();
        }



At this point I lost all faith in humanity and all that's sacred.
Come on people, there must be a better way!

And here I come to save the day :) The code is actually very short and simple:


getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

2010/06/26

Inter-process Communication of BlackBerry

Maybe I just don’t know how to read the docs, or how to Google, but I was looking for this info way too much, somehow it was burried. In any case – let’s say you have to apps on the device, and they use a common PersistenStore. One app adds stuff there in the background. The other – shows a UI with a list of the items from the store. All nice and synchronized. The only problem is – there is no way to detect that the PersistentObject that you are using was updated! So if you want to have your list refreshed automatically – you have to get creative.

On the producer side, after updating the PersistentStore, post a global notification with a unique ID:
ApplicationManager.getApplicationManager().postGlobalEvent(Main.NOTIFICATIONS_ID_1);

On the consumer side (UI), register for the notifications, then catch that message and update the UI accordingly:

Why I Hate BlackBerry. Also, how to make a folder.

Let’s be clear, RIM should be ashamed of the way their developer toolkit looks. The documentation is sparse at best, their own ide JDE looks and works like something from the 70s, and their Eclipse plugin refuses to work with actual devices and crashes on Simulator hot swaps. I am not even mentioning the fact that the UI has to be built in code like in the DOS days. Seriously? No GUI builders? In 2010? Sheesh. Now to the tip of today: How to make a folder on a SD Card.

Sounds simple right? Open a FileConnection, call a mkdir() method? Nope. IO Exceptions, Cannot access root file system and other not very helpful messages (all that is in addition to the fact that you cannot work with both debugger and SD card at the same time. very convenient).

Basically, the hidden gem is – you must have a trailing slash at the end of your path. Otherwise – no mkdir() for you!

Gist, GitHub and Live Writer

Microsoft has a small project they purchased with some company that is actually very nice – small, free and better than most commercial products – Live Writer http://windowslivewriter.spaces.live.com . Supports different blogging sites, has many nice features. All in all – a solid product.

It also supports a very simple plugin model for easy extensibility of the program.

As you might have noticed before, I am posting snippets of code using a most excellent http://gist.github.com service.

So I decided to make a plugin for Live Writer that will simplify that process. 20 minutes of work and it is done. You can find it at the Live Writer Extension Gallery. The sources (badly written due to laziness) can be downloaded here.

2010/06/24

Detecting network availability on Android

This tip is not really a ‘hidden feature’, but it’s useful and can come in handy if you don’t want to register BroadcastReceiver and add listeners for network events, but just want to check if you have network access at a certain point.

2010/06/23

Transparent UIWebView on iPhone

Here's a little trick that allows you to create a WebView in your iPhone application that has it's background transparent, showing whatever controls you have underneath.
Could be used to create some sort of animation/background images behind the view.

2010/06/20

Shaking on the iPhone

Let’s say you have an iPhone application with many different View Controllers, and in some of them you want to detect a ‘Device Shake’ event. You can’t just catch it in the Controller itself, since it provide methods for that. So the solution is this – you subclass a UIWindow to detect an event and then broadcast it to your app. Then catch the event in appropriate places using NSNotificationCenter.

Don’t forget to change the parent class of your main Window to MotionWindow to catch the events.

Enjoy, here's the code:

 

2010/06/16

Get your Blackberry app version programmatically

Let's say you want to add a 'About' dialog in your application and you want to show current application's version.

Sounds easy, right? Well, not exactly. In BlackBerry there could be several apps per COD file, called ‘modules’, so you have to traverse them to find your own module. Then there’s the case with simulators which are not close to the real device and therefore cause many issues, so you have to add a special case for them… All this sums up to the following piece of code:



2010/06/15

Sending an SMS on BlackBerry

While reading documentation on SMS sending in BlackBerry JDE JavaDocs you come across a fact that several important parts are missing.
Documentation mentions SMS 'port', a number you are supposed to write after the phone, using the :123 format. Googling, you might see on many sites a random number provided, then on others you see a + sign used in the phone number.
Don't believe the hype!
Here's a simple function, that, given a phone number (just digits) and SMS text, will simply send a SMS or return false in case something bad happens. Easy peasy!




2010/06/14

Set Sail

During my many hours of laboring as a mobile application developer I came across an interesting fact - the amount of documentation provided to the programmers by the different OS manufacturers is incredibly scarce and partial, missing many small details that can cause countless hours of hear-pulling and cursing.

Thankfully we now have sites like StackOverflow which connect developers and empower them, but even there you sometimes miss this important information.

So I present to you, dear Internet, this blog, in which I will some times post notes, code snippets, suggestions and findings that I believe can be useful to many developers word wide.

P.S. I do not claim the be the uber-specialist, nor do I claim copyright for the code. It's been either collected from pieces on the net, or glued together by me.
You are free to use and repost anything you find here, though I would appreciate a mention in you comments if you plan on using the content.