Monday, May 21, 2012

Coding Practice: InterviewStreet.com

A few months back I wrote about betterprogrammer.com. The site gives you several Java programming problems to do, and it ranks you afterwards based on how many people have taken the test. It’s a good way to sharpen your coding skills.

 InterviewStreet is like BetterProgrammer, but much more helpful and addicting. For starters, it doesn’t limit you to just using Java. You have a much larger array of programming language options to use.

If you’re the type that gets addicted to getting the highest score in a video game, than you should enjoy playing around on InterviewStreet . The site has a handful of brain teaser type problems that require more advanced Computer Science skills. You can find problems that require dynamic programming, unique data structures, or just a careful eye towards implementing an algorithm as efficiently as possible.

The site gives you a few test cases to help you write your code. When you think your program is good enough, you submit it to the site, where they run your code against several internal unit tests and show you the results. Depending on how well you did, you’re awarded a certain amount of points.

There is a scoreboard on the site to let you know how you rank versus all the other users, and what you could potentially do to move up in the ranks. I certainly suggest you check it out. It’s a great way to sharpen your skills and have some competitive fun at the same time.

Wednesday, May 9, 2012

Implementing Facebook’s Notification Widget in Android

Have you ever checked out the Facebook app? When you click on the Notifications button at the top, the app creates a nice overlaid window that contains a scrolling list view of all your info. It doesn’t dim out the background, and it also disappears if you click anywhere on the screen that isn’t in the overlaid window itself. The overlay is a great idea to add some polish to a UI.

clip_image002

The other day I began trying to figure out how Facebook does this overlay. A good way to reproduce this type of overlay is to actually use a Transparent Activity. You can define a Transparent Activity in the Android Manifest as follows:

   1: <activity
   2:     Android:name=".OverlayActivity"
   3:     android:theme="@android:style/Theme.Translucent.NoTitleBar" >
   4: </activity>
   5:  

This gives you an empty activity. So when this activity is started, it looks as if nothing has happened, and the user can no longer click on the UI elements from the previous activity. Now that we have an activity that lets the background of the last activity be visible, we need to design the overlay itself in XML. In Facebook’s case, this would be a ListView and the rectangle graphic. The layout would also need to be positioned correctly. Notice in the Facebook image above how the image is positioned in such a way that the rectangle pointer is directly below the button that activated it.

The beauty of using this implementation is that the overlay’s functionality is completely segregated because it is housed in a new activity. In the OverlayActivity, we simply generate our list and set up the click handlers for the list. There are just a few more tricks left to get all of the functionality that Facebook has added.

To make the overlay disappear when any area outside of the overlay is clicked, some work needs to be done in the Overlay’s layout. This involves setting up a click handler to cover all of the area that the Overlay’s visible widgets aren’t using. By setting the click handler in the root layout close the activity on click, we have our desired functionality. This works well in Android because any click handlers set up on other views (in Facebook’s case, the ListView) would override the root’s click handler.

To do one step above Facebook’s implementation, animation could be added to provide transitions for when the overlay is shown and removed. For example, we could easily add fade in and fade out animations that would make the overlay look even sharper. Since we have an activity, it’s very easy to add any kind of animation to the overlay. For the animation fun, check out the Alpha animation in Android.