Working NumberPicker and Android

For the last few weeks, I’ve been working on and off on a small android project. For number selection, I wanted to use something similar to what’s in the Alarm Clock that ships with most Android phones… a simple control made out of 3 elements, a text field, an “increase” button and a “decrease” button. I was really surprised to see that this is not part of the SDK. After checking on the net, I found this site:

http://www.quietlycoding.com/?p=5

It seemed to work well most of the time, but I had a strange behavior that I thought was coming from my side… After displaying the NumberPicker in a Dialog, I was using an OnClick on some button in this dialog, and sometimes the number I was getting from the picker didn’t correspond to what was on screen. Also, after seeing the application running on my phone, I decided to show it to my fiancée, and her first try at it broke the NumberPicker! If you hold one of the buttons, the picker will loop infinitely increasing or decreasing the value.

There might be other issues but here’s my fix to the two of them:

First, the infinitely increasing value problem, it appears that the code that’s constantly run does not have exit conditions, here’s how I fixed it:

private final Runnable mRunnable = new Runnable() {
    public void run() {
    boolean repost = false;
    if (mIncrement) {
        changeCurrent(mCurrent + 1);
        if(mIncrementButton.isPressed())
        {
            repost = true;
        }
    } else if (mDecrement) {
        changeCurrent(mCurrent - 1);
        if(mDecrementButton.isPressed())
        {
            repost = true;
        }
    }
    if(repost)
    {
        mHandler.postDelayed(this, mSpeed);
    }
}
};

I simply check if the button is still pressed before reposting the “increase” callback.

As for the value, if the text edit was currently being edited and the value was fetched before it lost focus, it was still returning the old value… the fix is quite simple:

public int getCurrent() {
    validateInput(mText);
    return mCurrent;
}

Here’s the modified NumberPicker.java to be used with QuietlyCoding’s package, feel free to comment if you have any questions!

Thanks

There is no source code available for the current location

If you’ve happened to use a lib with debug information, chances are that you’ve tried to step into a function and was greeted with a pop up similar to (I’ve compiled real quick FreeImage, which is an awesome library available here, to have a lib with debug information, then moved the source elsewhere because the pdb contains the original path the source was built from):

If at this point, you press “Cancel”, you’ll be prompted with a pop up saying:

Sure, no problem.

The thing is that once this is done, anytime you’ll try to step into a function coming from the same cpp file you just canceled the look up, you’ll be prompted with the message without being able to select the file.

This is really easy to fix actually.  This setting is stored in the Solution, under the Debug Source Files panel:

Simply removing the file from the “Do not look for these source files” will re enable the file selection dialog.

Visual Studio Commands and Aliases

A while ago I received the visit of two programmers from a middleware company at work.  They were showing me how to properly use their software, so they had to integrate it in our solution.  The more senior one was typing while the junior was learning from him.  I saw him use something I’ve never seen before so I guess I learned something too!

He used “>” followed by “of” in the search tab, and as he typed, the drop down menu was populated by files matching his filter… hum!  I didn’t know Visual Studio had this built in, I thought you had to use Workspace Whiz or Visual Assist!

Here’s an example:

(screenshot taken using the BWAPI solution)

The prefix > tells Visual Studio to interpret the rest as a command.  The actual Command Window is accessible using Ctrl-Alt-A (default binding) for those who don’t want to use the mouse  to select the “Quick Find” text box.  You can either use an Alias (such as of for Open File) or the actual command (such as File.Open).

Microsoft lists some commands and aliases available here but using >Alias will list all the set aliases.

http://msdn.microsoft.com/en-us/library/c3a0kd3x.aspx

You can create aliases and remove aliases using the alias command:

http://msdn.microsoft.com/en-us/library/xasxzd71.aspx

Given that most people don’t use them at all, it is pretty standard across all PCs, so if you have to work on other people’s PCs, you should be able to use aliases instead of relying on an installed plug in!

Visual Studio Plugins

There’s a new programmer on my team and today I was reviewing his code.  He had this weird scrollbar in Visual Studio which displayed a preview of the code with an highlighted “window” of what was on the screen.

For those who’d be interested in checking it out:

RockScroll

Also, the only other plugin I use is Visual Assist X, you can get it here (not free though, but totally worth it!):

Visual Assist X