Sunday, January 23, 2011

How to get images dynamically


Lets say you're storing items in a slqite db for your new, soon-to-be-award-winning application, and one of the columns you're storing is the name of the image to associate with that item.

How on earth do you reference it from within your app so you can display it?
Good question! Here's how:


//first get a reference to the ImageView in our XML layout file we want to display the image in..

ImageView imgVw = (ImageView) customView.findViewById(R.id.imgVwInXML);

//Next check that there is actually an image file name in the db column:

if (OurDBCursor.getString(OurDBCursor.getColumnIndex("imageName")) != null) 
{
 
//if there is a name for the image, get that name..

  myImageName  = OurDBCursor.getString(OurDBCursor.getColumnIndex("imageName"));  
 

//get a reference to the image (located in our drawable folder in our project):
          
  int resID = getResources().getIdentifier("com.BlueMongo.Test:drawable/" + myImageName, null, null);  
 

//assign the resouce with that ID to our ImageView:
               
  imgVw.setImageResource(resID);
}

.. And that's it.

Hope that helps. Have a great day.

Sunday, November 21, 2010

Finding the users language settings via code



We've seen before how easy it is to reference difference resources based on the users language settings, but that was more-or-less managed by the Android framework itself.

What if you want to programmatically make decisions in your code based on what the user's language and local settings are?

Turns out, it's very easy, and here's how:
(this example assumes you have a TextView in your layout.main called tvLocale)

package com.Bluemongo.LanguageTest;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class LanguageTest extends Activity 
{
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
        
  TextView tvLocale = (TextView) findViewById(R.id.tvLocale);
        
       if (Locale.getDefault().getLanguage().equals("en")) 
        {tvLocale.setText("you're speaking English");}

        if (Locale.getDefault().getLanguage().equals("de")) 
        {tvLocale.setText("you're speaking German");}
        
        if (Locale.getDefault().getLanguage().equals("fr")) 
        { tvLocale.setText("you're speaking French");}   
        
  }
}


.. Told you it was easy.

Sunday, October 24, 2010

Wine Advisor, my first paid app is now available on the Android Market.

Wine Advisor, my first paid app is now available on the Android Market. And it's only $1.99! Bargain!


Discover what wines go with your favourite foods with Wine Advisor, the premier wine & food pairing assistant on Android.

Choose your food and find wine pairing suggestions, including a brief description of the wine. 

Great in restaurants or the grocery store. 

10,000 possible combinations!


Screenshots:








If you can, please support this blog and consider buying it, I'll be your best friend and answer all your Android questions first  :)

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.