<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coderholic &#187; blackberry</title>
	<atom:link href="http://www.coderholic.com/category/blackberry/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coderholic.com</link>
	<description>Addicted to Development</description>
	<lastBuildDate>Tue, 31 Aug 2010 09:19:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Blackberry OS 4.5 &#8220;Connection not writeable&#8221; Error</title>
		<link>http://www.coderholic.com/blackberry-os-45-connection-not-writeable-error/</link>
		<comments>http://www.coderholic.com/blackberry-os-45-connection-not-writeable-error/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 01:09:14 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[blackberry]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=105</guid>
		<description><![CDATA[When deploying an application that worked fine on Blackberry OS 4.2 I was surprised to come across the following error message on devices running version 4.5 of the OS: Connection not writeable The error was shown when trying to create a HTTP connection, using my getWebData method. I tracked the problem down to the following [...]]]></description>
			<content:encoded><![CDATA[<p>When deploying an application that worked fine on Blackberry OS 4.2 I was surprised to come across the following error message on devices running version 4.5 of the OS:</p>
<blockquote><p><strong>Connection not writeable</strong></p></blockquote>
<p>The error was shown when trying to create a HTTP connection, using my <a href="http://www.coderholic.com/blackberry-webbitmapfield/">getWebData method</a>. I tracked the problem down to the following line:</p>
<pre name="code" class="java">
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
</pre>
<p>Here I&#8217;m creating a read only connection (by passing the Connector.READ argument), which should be fine because I don&#8217;t want to send any data over the connection. However, on Blackberry OS version 4.5 creating a read-only HTTP connection will result in the &#8220;not writeable&#8221; error, even if you don&#8217;t explicitly try writing to the connection. </p>
<p>The simple fix is to create the connection with both read and write access:</p>
<pre name="code" class="java">
connection = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/blackberry-os-45-connection-not-writeable-error/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resizing a Bitmap on the Blackberry</title>
		<link>http://www.coderholic.com/resizing-a-bitmap-on-the-blackberry/</link>
		<comments>http://www.coderholic.com/resizing-a-bitmap-on-the-blackberry/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 21:13:34 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[blackberry]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=68</guid>
		<description><![CDATA[The RIM Blackberry API makes it really simple to display bitmap images on the screen. Once you&#8217;ve added your image as a resource file the code is as simple as the following two lines: BitmapField bmp = new BitmapField(Bitmap.getBitmapResource("image.png")); add(bmp); However, if you need to display a resized version of the image you&#8217;ll soon notice [...]]]></description>
			<content:encoded><![CDATA[<p>The RIM Blackberry API makes it really simple to display bitmap images on the screen. Once you&#8217;ve added your image as a resource file the code is as simple as the following two lines:</p>
<pre name="code" class="java">
	BitmapField bmp = new BitmapField(Bitmap.getBitmapResource("image.png"));
	add(bmp);
</pre>
<p>However, if you need to display a resized version of the image you&#8217;ll soon notice that the <a href="http://www.blackberry.com/developers/docs/4.1api/net/rim/device/api/system/Bitmap.html">Bitmap class</a> doesn&#8217;t have any methods for performing such an action. Don&#8217;t worry though, the <a href="http://www.blackberry.com/developers/docs/4.1api/net/rim/device/api/system/EncodedImage.html">EncodedImage class</a> does, and also provides a method to return the required Bitmap object. Below is the code to scale an image to half its original size and then add it to the current screen:</p>
<pre name="code" class="java">
	EncodedImage ei = EncodedImage.getEncodedImageResource("image.png");
	ei.setScale(2); // divide size by this number
	BitmapField bmp = new BitmapField(ei.getBitmap());
	add(bmp);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/resizing-a-bitmap-on-the-blackberry/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Blackberry Custom Button Field</title>
		<link>http://www.coderholic.com/blackberry-custom-button-field/</link>
		<comments>http://www.coderholic.com/blackberry-custom-button-field/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 18:25:50 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[blackberry]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.dowling.me.uk/blog/blackberry-custom-button-field/</guid>
		<description><![CDATA[The Blackberry&#8217;s ButtonField doesn&#8217;t allow much customisation of how it looks. It is grey when not selected, and when selected it is highlighted. The highlight colour depends on the current theme. The image below shows a ButtonField in the unselected state, and in the selected state with different themes. Because the ButtonField doesn&#8217;t allow you [...]]]></description>
			<content:encoded><![CDATA[<p>The Blackberry&#8217;s ButtonField doesn&#8217;t allow much customisation of how it looks. It is grey when not selected, and when selected it is highlighted. The highlight colour depends on the current theme. The image below shows a ButtonField in the unselected state, and in the selected state with different themes.</p>
<p style="text-align: center"><img src="http://www.coderholic.com/wp-content/uploads/2008/03/std-button1.png" alt="Blackberry ButtonFields" /></p>
<p>Because the ButtonField doesn&#8217;t allow you to customise its appearance you must create your own custom field if you wish to do so. My CustomButtonField class below is almost identical to the standard ButtonField, only it allows you to specify the the highlight colour. It also displays the text in white even when the button isn&#8217;t selected</p>
<pre class="java" name="code">
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;

public class CustomButtonField extends Field
{

	private int backgroundColour = Color.GRAY;
	private int highlightColour;
	private int fieldWidth;
	private int fieldHeight;
	private String text;
	private int padding = 8;

	public CustomButtonField(String text, int highlightColour)
	{
		super(Field.FOCUSABLE);
		this.text = text;
		this.highlightColour = highlightColour;
		Font defaultFont = Font.getDefault();
		fieldHeight = defaultFont.getHeight() + padding;
		fieldWidth = defaultFont.getAdvance(text) + (padding * 2);
		this.setPadding(2, 2, 2, 2);
	}

	protected boolean navigationClick(int status, int time)
	{
		fieldChangeNotify(1);
		return true;
	}

	protected void onFocus(int direction)
	{
		backgroundColour = highlightColour;
		invalidate();
	}

	protected void onUnfocus()
	{
		backgroundColour = Color.GRAY;
		invalidate();
	}

	public int getPreferredWidth()
	{
		return fieldWidth;
	}

	public int getPreferredHeight()
	{
		return fieldHeight;
	}

	protected void layout(int arg0, int arg1)
	{
		setExtent(getPreferredWidth(), getPreferredHeight());
	}

	protected void drawFocus(Graphics graphics, boolean on)
	{

	}

	protected void fieldChangeNotify(int context)
	{
		try
		{
			this.getChangeListener().fieldChanged(this, context);
		}
		catch (Exception e)
		{}
	}

	protected void paint(Graphics graphics)
	{
		graphics.setColor(backgroundColour);
		graphics.fillRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
		graphics.setColor(Color.GRAY);
		graphics.drawRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
		graphics.setColor(Color.WHITE);
		graphics.drawText(text, padding - 1, padding / 2 + 1);
	}
}</pre>
<p>You can see the CustomButtonFieldin the image below, which shows it unselected and also selected with a highlight colour of 0&#215;990099.</p>
<p style="text-align: center"><img src="http://www.coderholic.com/wp-content/uploads/2008/03/custom-button.png" alt="Blackberry CustomButtonField" /></p>
<p>If you want to go beyond changing the colours of your buttons then Jonathan Fisher has written up details of a custom <a href="http://www.jonathanhfisher.co.uk/b2/?p=40">BigButtonField</a> that  uses images, allowing you to create some really great looking buttons.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/blackberry-custom-button-field/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Blackberry WebBitmapField</title>
		<link>http://www.coderholic.com/blackberry-webbitmapfield/</link>
		<comments>http://www.coderholic.com/blackberry-webbitmapfield/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 21:06:04 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[blackberry]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.dowling.me.uk/blog/blackberry-webbitmapfield/</guid>
		<description><![CDATA[The standard Blackberry API provides lots of useful UI components, known as fields. The BitmapField can be used to display an image. Recently I&#8217;ve needed to display images from the web, so I created the WebBitmapField class. Anyone who&#8217;s done any Blackberry programming knows that getting data off the web can be a bit of [...]]]></description>
			<content:encoded><![CDATA[<p>The standard <a href="http://www.blackberry.com/developers/docs/4.2.1api/index.html">Blackberry API</a> provides lots of useful UI components, known as fields. The BitmapField can be used to display an image. Recently I&#8217;ve needed to display images from the web, so I created the WebBitmapField class.</p>
<p>Anyone who&#8217;s done any Blackberry programming knows that getting data off the web can be a bit of a pain. It must be done in a background thread, which must then pass the result back to the UI thread. I&#8217;ve created a simple method called getWebData that handles all of this for me. The result is passed back to a WebDataCallback interface:</p>
<pre name="code" class="java">
public interface WebDataCallback
{
	public void callback(String data);
}</pre>
<p>getWebData is a static method in my Utils class:</p>
<pre name="code" class="java">
public static void getWebData(final String url, final WebDataCallback callback) throws IOException
{
	Thread t = new Thread(new Runnable()
	{
		public void run()
		{
			HttpConnection connection = null;
			InputStream inputStream = null;

			try
			{
				connection = (HttpConnection) Connector.open(url, Connector.READ, true);
				inputStream = connection.openInputStream();
				byte[] responseData = new byte[10000];
				int length = 0;
				StringBuffer rawResponse = new StringBuffer();
				while (-1 != (length = inputStream.read(responseData)))
				{
					rawResponse.append(new String(responseData, 0, length));
				}
				int responseCode = connection.getResponseCode();
				if (responseCode != HttpConnection.HTTP_OK)
				{
					throw new IOException("HTTP response code: "
							+ responseCode);
				}

				final String result = rawResponse.toString();
				UiApplication.getUiApplication().invokeLater(new Runnable()
				{
					public void run()
					{
						callback.callback(result);
					}
				});
			}
			catch (final Exception ex)
			{
				UiApplication.getUiApplication().invokeLater(new Runnable()
				{
					public void run()
					{
						callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());
					}
				});
			}
			finally
			{
				try
				{
					inputStream.close();
					inputStream = null;
					connection.close();
					connection = null;
				}
				catch(Exception e){}
			}
		}
	});
	t.start();
}</pre>
<p>The WebBitmapField that makes use of the getWebData method is below. All you need to do is pass a URL to the constructor and it&#8217;ll load the image:</p>
<pre name="code" class="java">
public class WebBitmapField extends BitmapField implements WebDataCallback
{
	private EncodedImage bitmap = null;

	public WebBitmapField(String url)
	{
		try
		{
			Util.getWebData(url, this);
		}
		catch (Exception e) {}
	}

	public Bitmap getBitmap()
	{
		if (bitmap == null) return null;
		return bitmap.getBitmap();
	}

	public void callback(final String data)
	{
		if (data.startsWith("Exception")) return;

		try
		{
			byte[] dataArray = data.getBytes();
			bitmap = EncodedImage.createEncodedImage(dataArray, 0,
					dataArray.length);
			setImage(bitmap);
		}
		catch (final Exception e){}
	}
}</pre>
<p>Hopefully the WebBitmapField class will be of use to some Blackberry developers. Feel free to use it in your applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/blackberry-webbitmapfield/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
	</channel>
</rss>
