<?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>stealthcopter.com &#187; java</title>
	<atom:link href="http://www.stealthcopter.com/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Sat, 24 Jul 2010 00:01:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Android: Grouping onClickListeners together by implementation in an activity</title>
		<link>http://www.stealthcopter.com/blog/2010/04/android-grouping-onclicklisteners-together-by-implementation-in-an-activity/</link>
		<comments>http://www.stealthcopter.com/blog/2010/04/android-grouping-onclicklisteners-together-by-implementation-in-an-activity/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 00:06:16 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=857</guid>
		<description><![CDATA[Here is a quick tip for better organisation of your program by grouping all onClickListeners into a simple switch statement. The problem If you have lots of buttons or view that you are linking to OnClickListener events you can quickly end up with some cumbersome code. This can be changed very simply by implementing a [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a quick tip for better organisation of your program by grouping all onClickListeners into a simple switch statement.</p>
<p><strong>The problem</strong><br />
If you have lots of buttons or view that you are linking to OnClickListener events you can quickly end up with some cumbersome code. This can be changed very simply by implementing a method in your activity and switching between View ID&#8217;s. </p>
<p>Below is an example the typical method for creating an OnClickListener for a View.</p>
<pre name="code" class="java">

final ImageButton Ibutton = (ImageButton) findViewById(R.id.button_1);
Ibutton.setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
		Toast.makeText(testActivity.this, "Button 1 pressed ", Toast.LENGTH_SHORT).show();
	}
});

final ImageButton Ibutton2 = (ImageButton) findViewById(R.id.button_2);
Ibutton2.setOnClickListener(new OnClickListener() {
	 public void onClick(View v) {
		Toast.makeText(testActivity.this, "Button 2 pressed", Toast.LENGTH_SHORT).show();
	}
});
</pre>
<p><strong>The Solution</strong><br />
We can get our activity to handle the OnClick events itself, we do this we implementing OnClickListener:</p>
<pre name="code" class="java">
public class testActivity extends Activity implements OnClickListener {
</pre>
<p>If your write this in eclipse you can hover over the error this produces and click Add unimplemented methods to automatically create a stub for the next part.</p>
<div id="attachment_858" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/04/android-grouping-onclicklisteners-together-by-implementation-in-an-activity/onclicklistner/" rel="attachment wp-att-858"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/onclicklistner-600x115.png" alt="eclipse auto suggesting to implement the onclicklistener method" title="eclipse auto suggesting to implement the onclicklistener method" width="600" height="115" class="size-medium wp-image-858" /></a><p class="wp-caption-text">eclipse auto suggesting to implement the onclicklistener method</p></div>
<p>We now create our buttons similar to before, but for the setOnClickListener we pass this as an argument, so that our implemented method is called.</p>
<pre name="code" class="java">

ImageButton Ibutton = (ImageButton) findViewById(R.id.button_1);
Ibutton.setOnClickListener(this);      

ImageButton Ibutton2 = (ImageButton) findViewById(R.id.button_2);
Ibutton2.setOnClickListener(this);      
</pre>
<p>We now create the implemented method for our activity (onClick) and we use a switch statement to find which View fired the onclick event and preform an action accordingly.</p>
<pre name="code" class="java">

@Override
public void onClick(View v) {
	switch(v.getId()){
	case R.id.button_1:
		// action to preform on button 1
		Toast.makeText(testActivity.this, "Button 1 pressed ", Toast.LENGTH_SHORT).show();
		break;
	case R.id.button_2:
		// action to preform on button 1
		Toast.makeText(testActivity.this, "Button 2 pressed ", Toast.LENGTH_SHORT).show();
		break;
	}
}		
</pre>
<p>And it&#8217;s as simple as that!<br />
Feel free to post comments or questions</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/04/android-grouping-onclicklisteners-together-by-implementation-in-an-activity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android: Context menu example (on long press, gridview)</title>
		<link>http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/</link>
		<comments>http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 10:41:44 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=582</guid>
		<description><![CDATA[I received several requests regarding how I created a context menu (the menu activated on a long press) using a gridview and how to call functions such as saving a sound file from this. So I have created a quick example to explain this: Tutorial To implement a context menu (long press menu) you first [...]]]></description>
			<content:encoded><![CDATA[<p>I received several requests regarding how I created a context menu (the menu activated on a long press) using a gridview and how to call functions such as saving a sound file from this. So I have created a quick example to explain this:</p>
<p><strong>Tutorial</strong></p>
<p>To implement a context menu (long press menu) you first need to include the following imports:</p>
<pre name="code" class="java">
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
</pre>
<p>We start a very simple project, with the layout having a single button named button_example:</p>
<div id="attachment_854" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/cm1/" rel="attachment wp-att-854"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/cm1.png" alt="Android context menu example" title="Android context menu example" width="320" height="484" class="size-full wp-image-854" /></a><p class="wp-caption-text">Android context menu example</p></div>
<p>We then use registerForContextMenu in the onCreate of the activity to tell android that we want this view to create a menu when it is long pressed. This is not limited to buttons, this will work for other views too. You must register each view that you want to have associated with the context menu.</p>
<pre name="code" class="java">
Button btn = (Button) findViewById(R.id.button_example);
registerForContextMenu(btn);
</pre>
<p>You then need to override the onCreateContextMenu method to create the menu:</p>
<pre name="code" class="java">
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Context Menu");
		menu.add(0, v.getId(), 0, "Action 1");
		menu.add(0, v.getId(), 0, "Action 2");
	}
</pre>
<div id="attachment_852" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/cm2/" rel="attachment wp-att-852"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/cm2.png" alt="android context menu example" title="android context menu example" width="320" height="484" class="size-full wp-image-852" /></a><p class="wp-caption-text">android context menu example</p></div>
<p>And override onContextItemSelected to preform the action when an option is selected from this menu:</p>
<pre name="code" class="java">
    @Override
	public boolean onContextItemSelected(MenuItem item) {
       	if(item.getTitle()=="Action 1"){function1(item.getItemId());}
    	else if(item.getTitle()=="Action 2"){function2(item.getItemId());}
    	else {return false;}
	return true;
	}
</pre>
<p>function1 and function2 are just place-holders at the moment that toast a message when they are used. In the above example we are choosing between Action 1 and Action 2 which define which function will be run when an item is selected. We have passed the ID of the view through the context menu and into this function so we can tell what the user was pressing when the context menu was created.</p>
<div id="attachment_853" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/cm3/" rel="attachment wp-att-853"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/cm3.png" alt="calling a function after a context menu item was pressed" title="calling a function after a context menu item was pressed" width="320" height="484" class="size-full wp-image-853" /></a><p class="wp-caption-text">calling a function after a context menu item was pressed</p></div>
<p><strong>Examples</strong></p>
<p>If you create a gridview and give each item a unique ID then you can use this method to preform actions based on each item. This is the method I use for my soundboard applications to save the sounds to the SD card as a notification or ringtone:</p>
<div id="attachment_855" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/cssb_cm/" rel="attachment wp-att-855"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/cssb_cm.png" alt="counterstrike 1.6 soundboard context menu" title="counterstrike 1.6 soundboard context menu" width="320" height="482" class="size-full wp-image-855" /></a><p class="wp-caption-text">counterstrike 1.6 soundboard context menu</p></div>
<div id="attachment_856" class="wp-caption aligncenter" style="width: 331px"><a href="http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/utsb_cm/" rel="attachment wp-att-856"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/04/utsb_cm.png" alt="Unreal Tournament soundboard context menu" title="Unreal Tournament soundboard context menu" width="321" height="481" class="size-full wp-image-856" /></a><p class="wp-caption-text">Unreal Tournament soundboard context menu</p></div>
<p><strong>Source</strong></p>
<p>Below is the source code:</p>
<pre name="code" class="java">
package com.contextmenu.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.button_example);
        registerForContextMenu(btn);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Context Menu");
		menu.add(0, v.getId(), 0, "Action 1");
		menu.add(0, v.getId(), 0, "Action 2");
	}

    @Override
	public boolean onContextItemSelected(MenuItem item) {
       	if(item.getTitle()=="Action 1"){function1(item.getItemId());}
    	else if(item.getTitle()=="Action 2"){function2(item.getItemId());}
    	else {return false;}
	return true;
	}

    public void function1(int id){
    	Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
    }
    public void function2(int id){
    	Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Supporting multiple phones and screensizes in your android applications</title>
		<link>http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 22:38:33 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=655</guid>
		<description><![CDATA[When releasing an android application it is often desirable to release your application to the largest amount of users as possible. By developing an application with android 1.5 as the target, later versions are automatically supported (1.6, 2.0, and 2.1). However different screen-sizes were introduced with android 1.6 so by default an application will not [...]]]></description>
			<content:encoded><![CDATA[<p>When releasing an android application it is often desirable to release your application to the largest amount of users as possible. By developing an application with android 1.5 as the target, later versions are automatically supported (1.6, 2.0, and 2.1). However different screen-sizes were introduced with android 1.6 so by default an application will not support smaller screens (larger screens are automatically supported). </p>
<p><strong>Android Versions Marketshare</strong><br />
The image below shows the current market share for the different version of android:</p>
<div id="attachment_660" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/android-1-6-users-access-the-android-market-most-2/" rel="attachment wp-att-660"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/02/Android-1-6-Users-Access-the-Android-Market-Most-2.jpg" alt="Android version percentages" title="Android_version_percentages" width="400" height="400" class="size-full wp-image-660" /></a><p class="wp-caption-text">Android version marketshare</p></div>
<p><code></p>
<table>
<tr>
<td>Android Platform</td>
<td>Percentage of Devices</td>
</tr>
<tr>
<td>1.1</td>
<td>0.3%</td>
</tr>
<tr>
<td>1.5</td>
<td>27.7%</td>
</tr>
<tr>
<td>1.6</td>
<td>54.2%</td>
</tr>
<tr>
<td>2.0</td>
<td>2.9%</td>
</tr>
<tr>
<td>2.0.1</td>
<td>14.8%</td>
</tr>
</table>
<p></code></p>
<p>Devices with small screens include the following phones T-Mobile G1, Samsung I7500, and the HTC Tattoo. Unfortunately I cannot find any statistics regarding the percentage of small screen android users (if you can please let me know). However as the fix is very simple it seems stupid not to.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.<br />
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p><strong>Adding Support</strong><br />
To add support for different screen sizes we need to add a supports-screens tag in our android_manifest.xml. This should look like the following:</p>
<pre name="code" class="xml">
  &lt;supports-screens
          android:largeScreens="true"
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="false" /&gt;
</pre>
<p>This tells android that the application should work on large, normal and small screens. Note that only setting small to false will prevent it from been visible in the market to small screened phones, for normal and large it will simply change the method of which android interprets the display of this application. anyDensity  fix me</p>
<p>However in order to use the supports-screens tag the target of your application must be android 1.6 (or Sdk version 4). We can allow our program which is now targetted for android 1.6 still be avaliable for android 1.5 (Sdk version 3) by adding android:minSdkVersion=&#8221;3&#8243; to our uses-sdk tag.</p>
<pre name="code" class="xml">
&lt;uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/&gt;
</pre>
<p>Now your application can be built for 1.6 supporting smaller screen sizes and still work on 1.5. </p>
<p><strong>Demonstration</strong><br />
Below are screenshots from two AVD&#8217;s, one with a normal screen size, and one with a small screen size:</p>
<div id="attachment_691" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/android_small_screen/" rel="attachment wp-att-691"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/02/android_small_screen-600x388.png" alt="android small screen wordcube" title="android_small_screen" width="600" height="388" class="size-medium wp-image-691" /></a><p class="wp-caption-text">small screen android AVD running wordcube</p></div>
<div id="attachment_692" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/android_small_medium/" rel="attachment wp-att-692"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/02/android_small_medium-600x502.png" alt="android medium screen wordcube" title="android_medium_screen" width="600" height="502" class="size-medium wp-image-692" /></a><p class="wp-caption-text">Medium screen android AVD running wordcube</p></div>
<p><strong>Note: Eclipse bug</strong><br />
Unfortunately eclipse currently does not acknowledge minSdkVersion and will not give you the option to test your application in any android virtual devices (AVD&#8217;s), while this is not fixed you can manually install the package into your AVD. For this you need to know the name of your emulator instance, to do this first run the AVD and then use the followin command to find out the name.</p>
<pre name="code" class="bash">
$adb devices
List of devices attached
emulator-5554   device
</pre>
<p>So we can see that the emulator instance I need is called emulator-5554. We can now use adb to install a package into this emulator. The below example shows installing wordcubefree from the bin folder in my project to my AVD using adb.</p>
<pre name="code" class="bash">
adb -s emulator-5554 install ~/android/workspace/WordCubeFree/bin/WordCubeFree.apk
1019 KB/s (39683 bytes in 0.038s)
Can't dispatch DDM chunk 46454154: no handler defined
Can't dispatch DDM chunk 4d505251: no handler defined
        pkg: /data/local/tmp/WordCubeFree.apk
Success
</pre>
<p>Note that this will fail if the application is already installed and you will need to uninstall the application before using this command (if anyone knows a switch to force install or alternative method please let me know)</p>
<p><strong>Going Further</strong><br />
It is possible to delve into this is more detail by providing layouts for small, medium and large screen sizes. This can be explored in more detail in the <a href="http://developer.android.com/guide/practices/screens_support.html">google dev support guide</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/supporting-multiple-phones-and-screensizes-in-your-android-applications/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to tell android which volume (media/ringtone etc.) should be controlled by your app</title>
		<link>http://www.stealthcopter.com/blog/2010/02/how-to-tell-android-which-volume-mediaringtone-etc-should-be-controlled-by-your-app/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/how-to-tell-android-which-volume-mediaringtone-etc-should-be-controlled-by-your-app/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 21:14:39 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=602</guid>
		<description><![CDATA[In my android apps I was getting annoying problems that whilst playing sound the volume buttons would control the media volume, but when no sound was playing they would control the ringtone volume. I found the following by trial and error, hopefully this post will help people with the same problem. This is done by [...]]]></description>
			<content:encoded><![CDATA[<p>In my android apps I was getting annoying problems that whilst playing sound the volume buttons would control the media volume, but when no sound was playing they would control the ringtone volume. I found the following by trial and error, hopefully this post will help people with the same problem.</p>
<p>This is done by placing a call of setVolumeControlStream in the onCreate part of your activity which takes on of the following values</p>
<ul>
<li>AudioManager.STREAM_MUSIC</li>
<li>AudioManager.STREAM_RING</li>
<li>AudioManager.STREAM_ALARM</li>
<li>AudioManager.STREAM_NOTIFICATION</li>
<li>AudioManager.STREAM_SYSTEM</li>
<li>AudioManager.STREAM_VOICECALL</li>
</ul>
<p>Below shows the code required to set your applications default volume control to any of the above values:</p>
<p><strong>Media Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
</pre>
<p><strong>Ringtone Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_RING);
</pre>
<p><strong>Alarm Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
</pre>
<p><strong>Notification Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
</pre>
<p><strong>System Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);
</pre>
<p><strong>Voicecall Volume</strong></p>
<pre name="code" class="java">
this.setVolumeControlStream(AudioManager.STREAM_VOICECALL);
</pre>
<p>I imagine this is also possible via and XML settings, if anyone knows of this I&#8217;d be very grateful to know how!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/how-to-tell-android-which-volume-mediaringtone-etc-should-be-controlled-by-your-app/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Android: Saving a sound file to SD from resource and setting as ringtone</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-saving-a-sound-file-to-sd-from-resource-and-setting-as-ringtone/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-saving-a-sound-file-to-sd-from-resource-and-setting-as-ringtone/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 23:26:46 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=559</guid>
		<description><![CDATA[Quite a few people have been asking how to save a file to the SD card in order to register it as a ringtone. The following example creates a function that will save a resource to the SD card (ie: from R.raw.soundfile to /sdcard/media/audio/ringtones/soundfile.wav) and register it as a ringtone. I have split this example [...]]]></description>
			<content:encoded><![CDATA[<p>Quite a few people have been asking how to save a file to the SD card in order to register it as a ringtone. The following example creates a function that will save a resource to the SD card (ie: from R.raw.soundfile to /sdcard/media/audio/ringtones/soundfile.wav) and register it as a ringtone. </p>
<p>I have split this example into two parts, the first part goes through the code a section at a time with a brief explanation of what it does, the second half is just the code that you can copy and paste and then edit to your hearts content.</p>
<p><strong>Parts</strong></p>
<p>We first setup our function to return a boolean depicting if we have failed or if we are successful. We accept in an integer which corresponds to the raw sound file.</p>
<pre name="code" class="java">
public boolean saveas(int ressound){

}
</pre>
<p>So this function would be called in the following fashion:</p>
<pre name="code" class="java">saveas(R.raw.soundfile);</pre>
<p>or utilising its boolean return:</p>
<pre name="code" class="java">
if (saveas(R.raw.soundfile)){
// Code if successful
}
else
{
// Code if unsuccessful
}
</pre>
<p>The following chunk of code creates an inputstream from the raw sound resource and loads it into a buffer. We add in the mandatory try/catch clause around these operations and return false if an exception is raised (to indicate failure to the rest of our program and to prevent trying to continue act upon this sound).</p>
<pre name="code" class="java">
 byte[] buffer=null;
 InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
 int size=0;

 try {
  size = fIn.available();
  buffer = new byte[size];
  fIn.read(buffer);
  fIn.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }
</pre>
<p>The following saves the buffer to a file on the SD card. It first ensures the folder exists and if not it is created. Then as before the writing operations are surrounded with try/catches</p>
<pre name="code" class="java">
 String path="/sdcard/media/audio/ringtones/";
 String filename="examplefile"+".wav";

 boolean exists = (new File(path)).exists();
 if (!exists){new File(path).mkdirs();}

 FileOutputStream save;
 try {
  save = new FileOutputStream(path+filename);
  save.write(buffer);
  save.flush();
  save.close();
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  return false;
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }
</pre>
<p>The following code sends an intent to tell the Media Scanner that we have added a new file, and sets up its properties in the media database:</p>
<pre name="code" class="java">
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

 File k = new File(path, filename);

 ContentValues values = new ContentValues();
 values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
 values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
 values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
 values.put(MediaStore.Audio.Media.IS_ALARM, true);
 values.put(MediaStore.Audio.Media.IS_MUSIC, false);

 //Insert it into the database
 this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
</pre>
<p><strong>Final code</strong><br />
Putting all this code together gives us our final functions:</p>
<pre name="code" class="java">
public boolean saveas(int ressound){
 byte[] buffer=null;
 InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
 int size=0;

 try {
  size = fIn.available();
  buffer = new byte[size];
  fIn.read(buffer);
  fIn.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }

 String path="/sdcard/media/audio/ringtones/";
 String filename="examplefile"+".wav";

 boolean exists = (new File(path)).exists();
 if (!exists){new File(path).mkdirs();}

 FileOutputStream save;
 try {
  save = new FileOutputStream(path+filename);
  save.write(buffer);
  save.flush();
  save.close();
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  return false;
 } catch (IOException e) {
  // TODO Auto-generated catch block
  return false;
 }    

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

 File k = new File(path, filename);

 ContentValues values = new ContentValues();
 values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
 values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
 values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
 values.put(MediaStore.Audio.Media.IS_ALARM, true);
 values.put(MediaStore.Audio.Media.IS_MUSIC, false);

 //Insert it into the database
 this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

 return true;
}
</pre>
<p>Comments, suggestions etc. are always welcome. Hope this has been helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-saving-a-sound-file-to-sd-from-resource-and-setting-as-ringtone/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Android: Opening a webpage in your app using Intents</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-opening-a-webpage-in-your-app-from-using-intents/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-opening-a-webpage-in-your-app-from-using-intents/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 21:20:01 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=545</guid>
		<description><![CDATA[Opening up a website from an application in android is very simple thanks to &#8220;Intents&#8221;. An Intent is a request to android use an application to preform a task. The code below shows a very simple example of launching a browser to go to the wordcube website. Context context = getApplicationContext(); String url = "http://www.stealthcopter.com/wordcube"; [...]]]></description>
			<content:encoded><![CDATA[<p>Opening up a website from an application in android is very simple thanks to &#8220;Intents&#8221;. An Intent is a request to android use an application to preform a task. The code below shows a very simple example of launching a browser to go to the wordcube website.</p>
<pre name="code" class="java">
Context context = getApplicationContext();
String url = "http://www.stealthcopter.com/wordcube";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);
try {
  // Start the activity
  startActivity(i);
} catch (ActivityNotFoundException e) {
  // Raise on activity not found
  Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
}
</pre>
<p>As pointed out in the comments, the context can be replaced by the activity itself, such as TestApp.this. eg:</p>
<pre name="code" class="java">
Toast toast = Toast.makeText(TestApp.this, "Browser not found.", Toast.LENGTH_SHORT);</pre>
<p>We have surrounded the activity with a try/catch which will be raised if android cannot find an application that will accept this intent, in this case a web-browser. It is highly unlikely that an android phone will not have a web-browser installed but it is a good practise to get into.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-opening-a-webpage-in-your-app-from-using-intents/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android: Requesting root access in your app</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:01:11 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=457</guid>
		<description><![CDATA[This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been &#8216;rooted&#8217; (hacked to allow superuser permissions). Process p; try { // [...]]]></description>
			<content:encoded><![CDATA[<p>This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been &#8216;rooted&#8217; (hacked to allow superuser permissions). </p>
<pre name="code" class="java">
Process p;
try {
   // Preform su to get root privledges
   p = Runtime.getRuntime().exec("su"); 

   // Attempt to write a file to a root-only
   DataOutputStream os = new DataOutputStream(p.getOutputStream());
   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");

   // Close the terminal
   os.writeBytes("exit\n");
   os.flush();
   try {
      p.waitFor();
           if (p.exitValue() != 255) {
        	  // TODO Code to run on success
              toastMessage("root");
           }
           else {
        	   // TODO Code to run on unsuccessful
        	   toastMessage("not root");
           }
   } catch (InterruptedException e) {
      // TODO Code to run in interrupted exception
	   toastMessage("not root");
   }
} catch (IOException e) {
   // TODO Code to run in input/output exception
	toastMessage("not root");
}
</pre>
<p>Where my &#8220;toastMessage&#8221; is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root access:</p>
<div id="attachment_458" class="wp-caption aligncenter" style="width: 332px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_root_access.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_root_access.png" alt="android root access dialog" title="android_root_access" width="322" height="486" class="size-full wp-image-458" /></a><p class="wp-caption-text">android request root access dialog</p></div>
<p><strong>Ref</strong><br />
<a href="http://www.anddev.org/viewtopic.php?p=33450">anddev.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Android: Improving Interfacing with twitter applications</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-improving-interfacing-with-twitter-applications/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-improving-interfacing-with-twitter-applications/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 15:03:52 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=447</guid>
		<description><![CDATA[This post builds upon the ideas shown in this post to create a complete function that will do the follow: Accept a string input for the message Try to open twidroidpro to send this tweet If failed, try to open twidroid to send this tweet If failed, bring up a list of applications that can [...]]]></description>
			<content:encoded><![CDATA[<p>This post builds upon the ideas shown in <a href="http://www.stealthcopter.com/blog/2010/01/android-interfacing-with-twitter-applications/">this post</a> to create a complete function that will do the follow:</p>
<ul>
<li>Accept a string input for the message</li>
<li>Try to open twidroidpro to send this tweet</li>
<li>If failed, try to open twidroid to send this tweet</li>
<li>If failed, bring up a list of applications that can send this information</li>
<li>Return a boolean indicating success of displaying the intent</li>
</ul>
<p><strong>Code</strong></p>
<pre name="code" class="java">
public boolean intentTwitter(String message){
 // Boolean to show if we succeeded or not
 // we assume we did until proven otherwise.
 boolean success=true;

 //Try twidroidpro first
 Intent intent = new Intent("com.twidroidpro.SendTweet");
 intent.putExtra("com.twidroidpro.extra.MESSAGE", message);
 intent.setType("application/twitter");
   try{
    startActivityForResult(intent, 1);
   }
   catch(ActivityNotFoundException e){
    success=false;
   }

 // Then twidroid if we failed
 if (!success){
  success=true;
  intent = new Intent("com.twidroid.SendTweet");
  intent.putExtra("com.twidroid.extra.MESSAGE", message);
  intent.setType("application/twitter");
    try{
     startActivityForResult(intent, 1);
    }
    catch(ActivityNotFoundException e){
     success=false;
    }
 }

 //Then send general intent if we failed again
 if (!success){
  success=true;
   try {
    intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, d);
    intent.setType("application/twitter");
    startActivity(Intent.createChooser(intent, null));
   } catch (ActivityNotFoundException e) {
    success=false;
   }
 }
// return indicating if we were successful in bringing up an intent
// of some description
return success;
}
</pre>
<p><strong>Example</strong><br />
Below is a screenshot from the wordcube (available on market) application using this function to make the application interact with twitter.</p>
<div id="attachment_429" class="wp-caption aligncenter" style="width: 345px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_twidroid.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_twidroid.png" alt="twidroid intent screenshot" title="android_twidroid" width="335" height="492" class="size-full wp-image-429" /></a><p class="wp-caption-text">twidroid intent called from wordcube</p></div>
<p><strong>What&#8217;s next?</strong><br />
In a future post I will be demonstrating how to interact with the android market, this can be use in conjunction with something similar to above to try and run a program and if the program is not installed the user can be taken to the market to download a program.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-improving-interfacing-with-twitter-applications/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Android: Counter-Strike 1.6 SoundBoard released</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-counter-strike-1-6-soundboard-released/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-counter-strike-1-6-soundboard-released/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 01:15:08 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=433</guid>
		<description><![CDATA[A soundboard is a very simple application to make, and I was given the idea to make a counter-strike 1.6 version by a friend. It seemed like a good idea as no one had made one yet, and it&#8217;s quite hard to find something that hasn&#8217;t already been done before. The buttons are aligned in [...]]]></description>
			<content:encoded><![CDATA[<p>A soundboard is a very simple application to make, and I was given the idea to make a counter-strike 1.6 version by a friend. It seemed like a good idea as no one had made one yet, and it&#8217;s quite hard to find something that hasn&#8217;t already been done before.</p>
<div class="wp-caption aligncenter" style="width: 190px"><img alt="counter-strike 1.6 soundboard promo image" src="http://www.stealthcopter.com/android/apps/counterstrikesb/icon.png" title="counter-strike 1.6 soundboard promo image" width="180" height="120" /><p class="wp-caption-text">counter-strike 1.6 soundboard</p></div>
<p>The buttons are aligned in a grid view, with the buttons made transparent so that the background can be seen.</p>
<div class="wp-caption aligncenter" style="width: 330px"><img alt="screenshot of counter-strike 1.6 soundboard android application" src="http://www.stealthcopter.com/android/apps/counterstrikesb/ss1.png" title="counter-strike 1.6 soundboard android application 1" width="320" height="480" /><p class="wp-caption-text">screenshot of counter-strike 1.6 soundboard</p></div>
<div class="wp-caption aligncenter" style="width: 330px"><img alt="another screenshot of counter-strike 1.6 soundboard android application" src="http://www.stealthcopter.com/android/apps/counterstrikesb/ss2.png" title="counter-strike 1.6 soundboard android application 2" width="320" height="480" /><p class="wp-caption-text">screenshot of counter-strike 1.6 soundboard</p></div>
<p>I am very pleased with the final look of the app, however it was getting poor reviews due to people wanting to save the sounds for use as a ringtone. To alleviate this problem I have updated the app to have this option as a context menu avaliable by long pressing on a button. This will then save the file on the sd card and update the mediastore to tell android a new ringtone and notification has been added.</p>
<p>The code to load a ringtone into android from an application is shown below. If you see any problems with this code, or have an improvements please let me know!</p>
<pre name="code" class="java">
File k = new File(path, filename);

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, filesnames[pos]);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
</pre>
<p>Ratings, reviews, feedback and feature requests are most welcome.</p>
<p><a href="http://www.stealthcopter.com/android/apps/counterstrikesb/counterstrikesb.apk">Download Link</a></p>
<p><a href="market://details?id=com.sc.counterstrikesb">Android Market Link</a> (For android phones)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-counter-strike-1-6-soundboard-released/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Android: Interfacing with twitter applications</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-interfacing-with-twitter-applications/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-interfacing-with-twitter-applications/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 13:59:29 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=422</guid>
		<description><![CDATA[Below are some code snippets for sending messages to twitter from your application by utilizing a twitter application. The following code will create a new intent(a request to android for something to happen) for the twidroid application and pass it the message we wish to send. It is important to set the type of the [...]]]></description>
			<content:encoded><![CDATA[<p>Below are some code snippets for sending messages to twitter from your application by utilizing a twitter application.</p>
<p>The following code will create a new intent(a request to android for something to happen) for the twidroid application and pass it the message we wish to send. It is important to set the type of the intent as it will fail without it (from at least android 1.5). It will then start the activity and we use a try/catch encase twidroid is not found.</p>
<pre name="code" class="java">
	Intent intent = new Intent("com.twidroid.SendTweet");
	intent.putExtra("com.twidroid.extra.MESSAGE",
	"@stealthcopter Example tweet from android application");
	intent.setType("application/twitter");
	try {
		startActivity(intent);
	}
	catch (ActivityNotFoundException e) {
		/* Handle Exception if Twidroid is not installed */
		Toast.makeText(this, "Twidroid not found.", Toast.LENGTH_SHORT).show();
	}
</pre>
<p>But what if someone has a different twitter application installed? this can be solved by offering the user a choice of application to open.</p>
<pre name="code" class="java">
	Intent intent = new Intent(Intent.ACTION_SEND);
	intent.putExtra(Intent.EXTRA_TEXT,
	"@stealthcopter Example tweet from android application");
	intent.setType("application/twitter");
	try {
		startActivity(Intent.createChooser(intent, null));
	}
	catch (ActivityNotFoundException e) {
		/* Handle Exception if no suitable apps installed */
		Toast.makeText(this, "No suitable apps found.", Toast.LENGTH_SHORT).show();
	}
</pre>
<p>By combining these two solutions we can make the choice dialog only pop up if the system cannot find the twidroid application. This can be then taken even further by asking the user if they wish to visit the market place to install a twitter application if none is found.</p>
<pre name="code" class="java">
   new AlertDialog.Builder(WordCube.this)
   .setTitle("Get Twitter")
   .setMessage("No twitter Application not found. Goto market and install one now?")
   .setIcon(R.drawable.logo)
   .setNegativeButton(R.string.dialog_no, null)
                   .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	intentMarket("market://search?q=twitter");
                }
            })
   .show();
</pre>
<p>Where intentMarket() is defined a to open up the market and search for the specified application passed to it as a string.</p>
<pre name="code" class="java">
public void intentMarket(String url){
	Intent i = new Intent(Intent.ACTION_VIEW);
	Uri u = Uri.parse(url);
	i.setData(u);
	try {
		startActivity(i);
	} catch (ActivityNotFoundException e) {
		toastMessage("Market not found.");
	}
}
</pre>
<p>Below shows two screenshots from the <a href="http://www.stealthcopter.com/wordcube">wordcube</a> application. This application is avaliable for android, see <a href="http://www.stealthcopter.com/android">here</a> for more information and download link (or visit market on your android device).</p>
<div id="attachment_429" class="wp-caption aligncenter" style="width: 345px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_twidroid.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_twidroid.png" alt="twidroid intent screenshot" title="android_twidroid" width="335" height="492" class="size-full wp-image-429" /></a><p class="wp-caption-text">twidroid intent called from wordcube</p></div>
<div id="attachment_431" class="wp-caption aligncenter" style="width: 339px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_choice_intent.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/android_choice_intent.png" alt="choice box after intent" title="android_choice_intent" width="329" height="491" class="size-full wp-image-431" /></a><p class="wp-caption-text">choice box for twitter applications</p></div>
<p><strong>Update:</strong> This is now taken into a complete function in <a href="http://www.stealthcopter.com/blog/2010/01/android-improving-interfacing-with-twitter-applications/">this post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-interfacing-with-twitter-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android: Adding styling to a text (bold italic etc..)</title>
		<link>http://www.stealthcopter.com/blog/2010/01/android-adding-styling-to-a-text-bold-italic-etc/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/android-adding-styling-to-a-text-bold-italic-etc/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 10:40:23 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=416</guid>
		<description><![CDATA[Settings the text in android is done by finding the object you wish to change, and then using its setText property. Below shows my code updating a textview by its id score_text to say &#8220;boring regular text&#8221; ((TextView)this.findViewById(R.id.score_text)).setText("boring regular text \n") This is good but what if you want to include styling in your text, [...]]]></description>
			<content:encoded><![CDATA[<p>Settings the text in android is done by finding the object you wish to change, and then using its setText property. Below shows my code updating a textview by its id <em>score_text</em> to say &#8220;boring regular text&#8221;</p>
<pre name="code" class="java">
((TextView)this.findViewById(R.id.score_text)).setText("boring regular text \n")
</pre>
<p>This is good but what if you want to include styling in your text, so bold emphasis or italics. Well it&#8217;s very simple we first add the android.text.html include:</p>
<pre name="code" class="java">
import android.text.Html;
</pre>
<p>and then surround our code in <em>Html.fromHtml()</em> this causes them html styling to modify our text.</p>
<pre name="code" class="java">
((TextView)this.findViewById(R.id.score_text)).setText(Html.fromHtml("<big><b>exciting</b></big><small><i>and cool</i></small>text"
</pre>
<p>Below shows a screenshot of the regular styling and the html styling in an android application.</p>
<div id="attachment_417" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/text.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/text.png" alt="styled android text" title="android styled text" width="320" height="194" class="size-full wp-image-417" /></a><p class="wp-caption-text">styled and regular android text</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/android-adding-styling-to-a-text-bold-italic-etc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
