code journal

A weird problem with GridView OnScrollListener and ActionBar’s list navigation listener

Has anyone had a problem with setting a gridview’s scroll listener and item listener for a list navigation spinner in a layout? The ActionBar is actually ActionBarSherlock. I tried to use the regular ActionBar and still had the same results as shown in this video.

I have narrowed down the problem to be the gridview’s scroll listener. As soon as I set the gridview’s scroll listener, it causes these issues:
- Prevents the spinner in the action bar from receiving item click events.
- Gridview doesn’t seem to draw the item views until a touch event has occurred in the layout.

In order for the spinner to receive clicks, I have to pause the activity using the task switcher or switch to another and then come back to the activity, then it starts to work again. I am baffled as to why this is happening.

Has anyone experienced this problem before? Click here to see the post I made on Google+ which has a video attached to it.

Why I dumped my custom image manipulation code from Transitions

After spending several nights and weekends implementing and testing the code I had written for image resizing, it still didn’t work for some popular devices. This was unacceptable. As I gave this a lot of thought, I realized that I was doing it wrong. No. Not just the code. My effort. As much as the engineer that I am, I wanted to make it work. I was very interested in solving it. I wanted to own it much like every other coding problem I try to solve. But, as an Engineer, I also don’t want to “reinvent the wheel” if I am not going to improve it. I certainly wasn’t improving it.

So, for the upcoming release for Transitions, I trashed all of the code I wrote for image manipulation and simply popped in Square’s Picasso library. Why? Because, it’s simply that much less code I need to change/maintain. It is so much easier to look at my code now with all the complex bitmap decoding stuff gone. I’ll let the experts handle that. And, now I can concentrate what really matters. Putting features together. I am still innovating with Transitions. I added a feature to my open source library CustomPreferences which allows you to load a list preference from the database dynamically. It’s very useful when you let your users build their own database of things and you want to allow them to pick from that list. Currently, you can’t do this with the preference classes out-of-the-box. But, you can, with CustomPreferences.

 

 

2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

600 people reached the top of Mt. Everest in 2012. This blog got about 6,000 views in 2012. If every person who reached the top of Mt. Everest viewed this blog, it would have taken 10 years to get that many views.

Click here to see the complete report.

left-right

I created a new public repo on my GitHub page. Check it out.

UTC Offset regex validation

I was working on validating UTC Offset values in a javascript application and came up with this regex to do the job. What do you think?

//see http://en.wikipedia.org/wiki/List_of_UTC_time_offsets for list of valid positive and negative offsets
var positiveOffset = /^[+]?([0-9]\.?[0]?[0]?|1[0-4]\.?[0]?[0]?|[3,4,5,6,9,10,11]\.30|[5,8]\.45|12\.45)$/g;
var negativeOffset = /^[-]([0-9]\.?[0]?[0]?|1[0-2]\.?[0]?[0]?|[3,4,9]\.30)$/g;
var GMT = /^0\.?[0]?[0]?$/g;
if(!positiveOffset.test(value) && !negativeOffset.test(data.value) && !GMT.test(value)) {
//show error
}

Closure function vs defined (named) function

I wrote a performance test on jsperf.com to measure the difference between calling a closure function from within a loop and calling a defined function within another loop for the same number of iterations. See the difference and run the test yourself.

PHP file download

So I have been working on a PHP website for a freelance job and had to write few lines of code to download a file from the database. Very simple right? Yeah that’s what I thought too. I mean it was < than 20 lines of code. Why wouldn’t it work? What could go wrong? That’s where I was wrong. This is what I had at first:

if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$id = $_GET['id'];
$application = new Applications();
$application->get($id);
header(“Content-length: $application->size”);
header(“Content-type: $application->type”);
header(“Content-Disposition: attachment; filename=$application->name”);
$content = $application->data;
echo $content;
}
exit;

The problem I experienced was that a file would upload (different piece of code) fine. I knew that because the size attributes match when the file is written to the db. When I actually try to download the file back from the db using the script above, the file gets corrupted. I had no idea why! The weird (but made sense later) part was that plaintext files were fine no matter how large they were. It was binary files like images, DOCs, PDFs etc. So, I scoured the internet searching for answers and finally came across one thread somewhere where there was a similar discussion from over a year ago and someone said that the file stream would get random whitespace characters when downloading the file. Then it hit me, I remember seeing whitespace at the beginning of the stream when I had opened the corrupt file using notepad. The solution was to ob_clean() before you begin writing the headers and the content stream itself. You see one of the important points about writing header information to the client is that there shouldn’t be any whitespace before writing headers.

I modified the above code then to this:

if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$id = $_GET['id'];
$application = new Applications();
$application->get($id);
ob_clean(); //clears output buffer in php..see php manual
header(“Content-length: $application->size”);
header(“Content-type: $application->type”);
header(“Content-Disposition: attachment; filename=$application->name”);
$content = $application->data;
echo $content;
}
exit;

It would then download without any problems. At this time I should also caution you about dangling newline/whitespace in the php file itself which handles the download. The reason why plaintext files were ok is because plaintext files are not written in binary so whitespace or not it doesn’t matter for those file types and they will upload/download fine, at least in my case they did. Do you have a better way to handle this? If so, please leave a comment. Let me know about it.

canvas-charts

I just committed my first open source project on GitHub. You check it out here.

Here’s the example client with graphs:

Horizontal Bar Graph

 

Line graph

unknowncoder:

Very neat!

Originally posted on CSS3 Wizardry:

Recreating the iOS 5 Switch Control with HTML5, CSS3 and a Bit of ECMAScript 5

The final result of this post will run in iOS 5, Safari 5.1, as well as the latest versions of Chrome, Firefox and Opera.

Previously I had created a version of the switch control in iOS. With the launch of iOS5 Apple complete updated the look of the switch control. They went with a rounded style, which they also did with most controls in their desktop operating system, Lion.

After playing around with the early betas of iOS 5, I came up with the following reproduction of the new switch control look using just HTML5, CSS3 and some JavaScript for the interactive part. Functionally the switch control is nothing more than a fancier way of presenting a checkbox. So, for our purposes we are going to use a checkbox. Except that we need a couple…

View original 1,699 more words

Android NetworkOnMainThreadException

Android has been lenient about network access on the main thread in prior versions and now with ICS, they are cracking down on us.

It is bad practice however to have operations that are intensive and for a network operation it is even more sensitive since the network speed is a varying factor that can block the UI for an unknown amount of time. Although it is annoying to have your app crash all of a sudden in new Android versions because you (and by that I mean, I) made a bad programming mistake, it is for your own good to consider porting your network access code to an AsyncTask. Writing an AsyncTask is shit easy.

private class SomeTask extends AsyncTask<Void, Void, Void>{

@Override

protected Void doInBackground(Void…params){

//your network access code goes here

}

}

and then in your UI thread or main thread, just instantiate this task and execute it and you’re done. Obviously, I have simplified the AsyncTask by not going into much details about the arguments and result codes etc..You can read all about that here. I can’t tell you how much I love the Android documentation. Say what you want about their fragmentation problem; I just love their documentation. Just amazing. There’s nothing you can’t find in there unlike Apple’s. I spent a whole day trying to figure out how to create those provisioning and distribution certificates and there’s not an article on the internet that ties it all together!

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: