Thursday, October 14, 2010

Spinner down arrow stretching?

.. Styling the inner and outer views of a Spinner individually.


I was working with a Spinner with a lot of rows fetched from a database, and wanted to put a bit of padding around each item to make it easier for the user to select them.

So I went into my res\layout\spinner_view_row.xml (which controls how the Spinner rows display) and added this:

android:padding="5dip"

Then, when I went and re-ran my app, what used to look like this:



.. now looks like this:



.. Ooops. 


Looks like a condom doesn't it. Not what I was trying to achieve, really. If I made the padding large enough, it will also look like that before I've even selected anything.

Not what I wanted at all.

But I do want that padding around each item in my Spinner, otherwise my users will have too much trouble choosing items from my latest super-dooper, take-the-world-by-storm, #1 in the world market app, and it might not stay #1 for long at all.

Luckily, the answer is really, really simple.

You might recogise the below as the piece of code that binds a cursor from the database (containing all the items we want to display), to the xml view spinner_view_row, located at  res\layout\spinner_view_row.xml (this is the xml file in which we put the extra padding, above).


  final SimpleCursorAdapter ingredientAdapter =
  new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, ingredientsCursor, from, to); 
  ingredientAdapter.setDropDownViewResource(R.layout.spinner_view_row);

  spnIngredients.setAdapter(ingredientAdapter);

All we need to do to avoid the stretchy condom spinner arrow is to define another xml view in res\layout\ and call it something like spinner_view_closed, then paste into it the same code that you have in spinner_view_row.
Simply then customise this xml to have less padding, or a smaller text size for instance, then replace the reference to
simple_spinner_item
with a reference to this new xml file, like this:

  final SimpleCursorAdapter ingredientAdapter =
  new SimpleCursorAdapter(this, R.layout.spinner_view_row_closed, ingredientsCursor, from, to); 
  ingredientAdapter.setDropDownViewResource(R.layout.spinner_view_row);

  spnIngredients.setAdapter(ingredientAdapter);


.. and your new Spinner will look like this when open:



.. and this when closed.



Easy eh?
.. Told you so.

Monday, October 4, 2010

Android Market expands: More Countries, More sellers, More buyers

Support for paid Android application sales is now expanded to developers in 29 countries, with the addition of Argentina, Australia, Belgium, Brazil, Canada, Denmark, Finland, Hong Kong, Ireland, Israel, Mexico, New Zealand, Norway, Portugal, Russia, Singapore, South Korea, Sweden, Switzerland and Taiwan.

In addition, Android Market users from 32 countries will be able to buy apps, with the addition of Argentina, Belgium, Brazil, Czech Republic, Denmark, Finland, Hong Kong, India, Ireland, Israel, Mexico, Norway, Poland, Portugal, Russia, Singapore, Sweden, and Taiwan.

.. Get programming people!

Official blurb here.

Using EditText's inputType to control what type of keyboard is shown

As software developers, there are many circumstances in which you'll want to limit the input options available to your users. We've all seen situations along the lines of the person who enters 'two' in a field where we've only been expecting the number 2. Oh how that screws things up! Oh the laughs we've had eh?

Fortunately, in Android it's easy to gently shepherd our precious users to input the sort of data we're expecting, and we do that with the editText's inputType attribute.

You can set your editText inputType as ‘Phone’ for example, and the user can able to type only numbers. If it is ‘Time’ it will allow only time related characters to be entered. Handy eh?

There are many options, and I've included (what I think is) all of them at the end of this post for your pleasure.

In the meantime, here are some examples that will hopefully illustrate this option:

        <EditText android:id="@+id/etWidth1"
            android:hint="@string/widthLabel"
            android:minWidth="75dip"
            android:inputType="textCapWords"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </EditText>

notice the capitalised first letter of the each sentence, that happens automatically!



        <EditText android:id="@+id/etWidth1"
            android:hint="@string/widthLabel"
            android:minWidth="75dip"
            android:inputType="textCapCharacters"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </EditText>




        <EditText android:id="@+id/etWidth1"
            android:hint="@string/widthLabel"
            android:minWidth="75dip"
            android:inputType="text"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </EditText>
ok, so nothing special to see here..


        <EditText android:id="@+id/etWidth1"
            android:hint="@string/widthLabel"
            android:minWidth="75dip"
            android:inputType="phone"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </EditText>



        <EditText android:id="@+id/etWidth1"
            android:hint="@string/widthLabel"
            android:minWidth="75dip"
            android:inputType="textUri"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </EditText>


Remember, limiting the allowable input characters is only one part of good design.

You still need to run a sanity check on all fields that allow the user to enter data.

This is absolutely essential in many, many situations. Do a google search for 'sql injection' for more information on how unsafe fields can result in your entire system being vulnerable.

Here is (what I think is) the full list of EditText InputTypes available to you :

text
textCapCharacters
textCapWords
textCapSentences
textAutoCorrect
textAutoComplete
textMultiLine
textImeMultiLine
textNoSuggestions
textUri
textEmailAddress
textEmailSubject
textShortMessage
textLongMessage
textPersonName
textPostalAddress
textPassword
textVisiblePassword
textWebEditText
textFilter
textPhonetic
number
numberSigned
numberDecimal
phone
datetime
date
time

Sunday, August 1, 2010

Using spinner.setSelection & finding the spinner doesn't show the selected item when closed?



Ok, I've just spent a couple of hours trying to figure this out, and now I have, I thought I'd share the incredibly simple solution with you.

The issue: I was needing to set a Spinner's selected item via code, but found when calling the Spinner's setSelection method and passing in the position to set it to, something odd would happen, the closed spinner would appear blank, yet, when clicking on it, the item I've asked to be selected would be correctly located at the top of the spinner.

It looks like a Spinner is not told to redraw when using .setSelection(position), what you have to do is call .setSelection(int position, boolean animate) unless you want your selection to happen silently behind the scenes.

Odd, but easily sorted out.

The incredibly simple solution:

This won't show the fact that the Spinner selection has been set:


spnIngredients.setAdapter(ingredientAdapter);
spnIngredients.setSelection(position);

This will:


spnIngredients.setAdapter(ingredientAdapter);
spnIngredients.setSelection(pos, true);




Hope that helps someone out there.
.. Happy Spinning.

Monday, July 12, 2010

Google App Inventor: Android for Beginners for absolute beginners!

Today Google announced a simple-to-use DIY app maker called Google App Inventor.

Google App Inventor brings Android development to non-programmers, employing a design scheme that relies on visual blocks rather than writing pages of code, the App Inventor -- In true Google style, still in Beta, of course -- has functions for just about anything you can do with an Android handset, including access to GPS and phone functionality.

I can imagine this would be fantastic in classrooms.

more information here: http://appinventor.googlelabs.com/about/
complete this form to apply for access: https://services.google.com/fb/forms/appinventorinterest/




Tre Cool!