Friday, September 19, 2014

Android: adding a Spinner (drop-down or pick-list widget)

You start by adding Spinner UI to your res/layout/activity_xyz.xml
Make sure to give it a meaningful ID, especially if you have multiple Spinner items.

            android:id="@+id/conversionSelectorSpinner" />

If your spinner will have a pre-defined list of items that will need to be translated to another language then you probably should use string-array in res/values/strings.xml

    <string-array name="conversionTypeArray">
        <item>meters to yards</item>
        <item>kilometers to miles</item>

In your Activity class you have to add your Spinner in onCreate() method.

   protected void onCreate(Bundle savedInstanceState) {
      //set up the conversion choices using a Spinner
      Spinner conversionSelectorSpinner = (Spinner) findViewById(;
      // create adapter from the res/values/strings.xml/string-array
      // simple_spinner_item is a TextView
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.conversionTypeArray, android.R.layout.simple_spinner_item);
     // simple_spinner_dropdown_item is basically a single line TextView that serves as header of a Spinner
       // set array adapter to populate the spinner

Now if you build  the APK we will have a Spinner, but we will have to make it work for us.

We will need a variable that will hold our selection, we will make it a class member to be used in various methods.

public class MainActivity extends ActionBarActivity {
   private int selectedConversion = 0;

We will need a programmatic access to the string-array we defined, we will add that line inside our onCreate() method.

 final String conversionTypeArray[] = getResources().getStringArray(R.array.conversionTypeArray);

Finally, we will have to add a event listener to our Spinner.

      // add listener that will react when an item was selected
      conversionSelectorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // assign selected item to the class member to be used later
            selectedConversion = position;
            // if you change conversion type, it is natural that the result will be different
            // we would not have debug toasts in production version, for edu purposes only
            showToast("onItemSelected in position: " + position + " id: " + id + " selected name: " + conversionTypeArray[position]);
         public void onNothingSelected(AdapterView<?> parentView) {
            // onNothingSelected is a Callback method to be invoked when the selection disappears from this view.
            // The selection can disappear for instance when touch is activated or when the adapter becomes empty.

Android apps now can run on every OS in a Chrome browser

Android apps can now run on every OS in a Chrome browser, or at least this can be shown to be possible. Tested on OS X, Windows and Ubuntu.

"ARChon runtime lets you run unlimited number of Android APKs created with chromeos-apk on Chrome OS and across any desktop platform that supports Chrome."


On personal note, the author of the hack, Mr. vladikoff001, is our developer hero of the mega proportions. Congratulations.

Yes, this is still a buggy hack, but it definitely opens doors for bigger and better future for this Android developer.

Android Studio: Eclipse shortcuts and formatter

If you are switching between Eclipse (at work) and Android Studio (home, teaching) then you will run into a problem of remembering the keyboard shortcuts and formatting the code consistently.

In Android Studio you have an option to add "Eclipse Code Formatter" and specify the xml formatter you exported from Eclipse (for your whole team).

You also have an option of using keymaps (keyboard shortcuts) from Mac Eclipse.

GIT: working with submodules

It is a good practice that multiple projects re-use common libraries of code, an example could be model object, APIs, utility classes, etc.

In your (parent) project clone another project as submodule..

$ git submodule add ssh:// module/submodule_name
Cloning into 'module/submodule_name'...
remote: Counting objects: 47, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 47 (delta 6), reused 47 (delta 6)
Receiving objects: 100% (47/47), 1.85 MiB | 715.00 KiB/s, done.
Resolving deltas: 100% (6/6), done.
Checking connectivity... done.

You need to initialize the submodule

$ git submodule update --init --recursive

If the code you want in the submodule is in different branch then you have to checkout that branch

$ cd module/submodule_name
$ git checkout develop

Finally you can do your normal PULL, or FETCH and REBASE 

$ git submodule foreach git pull

Eclipse: show hidden files

It is common that you need to edit hidden .* files, for example if you work with GIT you may want to edit .gitignore file.

In Eclipse > Package Explorer > select drop-down arrow "View Menu" > Filters ...

Then unselect ".* resources" and the .gitignore .gitmodules, etc. should be showing. 

Tuesday, September 16, 2014

Eclipse not reading ANT env variables

Sometimes you have ANT build.xml that works perfectly from command line but does not execute in Eclipse:

Execute failed: Cannot run program "${env.THRIFT_TOOL}": error=2, No such file or directory

When you inspect your build.xml you should have (true for any executable):

<property environment="env"/><property name="thrift.compiler" value="${env.THRIFT_TOOL}"/>

If you inspect your env variable setting, on OSX ~/.profile you should have:

# Thrift tool  - updated: Sept 16, 2014
export THRIFT_TOOL=/usr/local/bin/thrift

Yet Eclipse is not running Ant correctly!

Starting Eclipse from COMMAND LINE fixes the issue:

$ bash -ic /Applications/../ 

Thrift: building from source

I was required to build Thrift from sources (to adjust it to generate Parcelable Android model classes)

uki@ thrift $ .git clone
uki@ thrift $ cd thrift
uki@ thrift $ .git checkout 0.9.1
uki@ thrift $ ./
uki@ thrift $ ./
uki@ thrift $ ./configure
uki@ thrift $ sudo make
uki@ thrift $ sudo make install

I get an error on my Mac:

./src/thrift/cxxfunctional.h:93:18: error: no member named 'bind' in namespace 'std'; did you mean 'find'?

Attempted solution 1:

I found a post on
change file:
line 51 to #include "thrifty.hh"

src/thriftl.ll:51:10: fatal error: 'thrifty.hh' file not found
#include "thrifty.hh"

Attempted solution 2:

As I was reading up I found that the version of my C++ might be wrong:

"libc++, the default C++ standard library in 10.9, doesn't include the experimental TR1 headers - just their standardized C++11 versions"

Checking the existing version of C++ on Mac:

 $ gcc --version
Configured with: --prefix=/Applications/ --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)

Installed newest C++ from brew

$ brew install gcc49

in my ~/.profile I added these 2 lines:

# C++ HOME - updated: Sept 16, 2014
export CPP_HOME=/usr/local/Cellar/gcc/4.9.1/bin/
export PATH=${PATH}:${CPP_HOME}
alias g++="/usr/local/Cellar/gcc/4.9.1/bin/cpp-4.9"
alias gcc="/usr/local/Cellar/gcc/4.9.1/bin/cpp-4.9"

now in the Terminal I can verify this:

uki@ thrift $ gcc --version
cpp-4.9 (Homebrew gcc 4.9.1) 4.9.1

The exercise is not complete yet, so I was not able to verify if this is a fix.

Monday, September 15, 2014

Eclipse: Thrift plugin

If you are editing Thrift files you will need this plugin.

In Eclipse open Help > Install New Software ... > Work With: enter this URL, click Add..

and follow the installation steps.

GIT: installation on Mac

Install Brew - Mac package installer

This "brew" tool will be useful for many other things...

$ ruby -e "$(curl -fsSL"

Check existing version of git

$ git --version
git version (Apple Git-48)

We can see that I had not the newest GIT (1.8.5), but I want to latest and greatest...

Install the latest and greatest git version

$ brew install git
/usr/local/Cellar/git/1.9.0: 1322 files, 30M

At this point the new GIT is installed, however the old GIT is still being used..

$ git --version
git version (Apple Git-48)

Checking the NEW GIT at different location...

uki@ bin $ /usr/local/Cellar/git/1.9.0/bin/git --version
git version 1.9.0

Edit the PATH so he new GIT tool is found first

Open Applications > Utilities >
We will be editing .profile file located in your home folder (~), this file may not be showing in your Finder, so you might need to do it in vi editor.

edit ~/.profile
export GIT_TOOL=/usr/local/Cellar/git/1.9.0/
export PATH=${GIT_TOOL}/bin:${PATH}
$ git --version
git version 1.9.0

Sunday, September 14, 2014

Android Class - Table of Contents

Note: content of this page is being migrated to the right margin.

Session 1

GIT: cheat sheet

UNIX commands review

Session 2 - review

Android SDK: set-up in IDE

 Android Studio - creating Android Virtual Device AVD

Android Studio - changing the app name

Android Studio - creating new Module (app)

Android Studio - Failure [INSTALL_FAILED_OLDER_SDK]

Android Studio - Error:compileSdkVersion android-L requires compiling with JDK 7

Session 3

Android Tutorial - creating calc converter app