Sunday, February 13, 2011

TextView background is black when setting colour from xml?


I had a colour defined in a 'colours.xml' file in my project's 'values' folder like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="tvBackground">#337700</color>
</resources>

Which, for those of you who cannot transpose directly from hex, looks like this:

My code looked like this:

   TextView tv = new TextView(getApplicationContext());
       
   tv.setBackgroundColor(R.color.tvBackground);

Seems reasonable doesn't it?

But when I ran my code, my shiny new little TextView came out with the background colour of this:


(that's #000000 in hex, or 'black' btw)

What was going on here?
Turns out, I wasn't using the correct method to set the background colour so that it comes out correctly, what I should have used was either of these:

   tv.setBackgroundResource(R.color.tvBackground);
- or -
   tv.setBackgroundColor(getResources().getColor(R.color.tvBackground));


.. Not especially well documented, but easily fixed.


Anyhow, now my TextView shows with the background colour of:


.. So all is now well with the world. Thought I'd just post this in case it helps someone out there (or myself when I forget next time)


until that next time, bye bye.


Monday, February 7, 2011

Wino the Wine Advisor now updated, new features & grrraphics


I've recently released an update for my Wino the Wine Advisor app,
The new version now allows you to combine tastes and also has some great new wine bottle graphics (even if I do say so myself).

Here are some screenshots:





.. And I have to say, those wine bottle images were an absolute pain to create.

I use Gimp and Inkscape to create all my graphics, in case you were wondering.




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  :)