2010/10/24
Making a real marquee out of Android TextView
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
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
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:
Apparently the usual approach is not good for the Java folk. They don't want no stinking nulls in their precious hashes!put
public V put(K key, V value)
- Maps the specified
keyto the specifiedvaluein this hashtable. Neither the key nor the value can benull.The value can be retrieved by calling thegetmethod with a key that is equal to the original key.
- Parameters:
key- the hashtable key.value- the value.- Returns:
- the previous value of the specified key in this hashtable, or
nullif it did not have one.- Throws:
NullPointerException- if the key or value isnull.
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.
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/05
Bitmap Button in Blackberry
In any case, here's the code that implements one.
Open an external browser on Android
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!
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/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
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!