Archives for 13 Jan,2010

You are browsing the site archives by date.

Android: Interfacing with twitter applications

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 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.

	Intent intent = new Intent("com.twidroid.SendTweet");
	"@stealthcopter Example tweet from android application");
	try {
	catch (ActivityNotFoundException e) {
		/* Handle Exception if Twidroid is not installed */
		Toast.makeText(this, "Twidroid not found.", Toast.LENGTH_SHORT).show();

But what if someone has a different twitter application installed? this can be solved by offering the user a choice of application to open.

	Intent intent = new Intent(Intent.ACTION_SEND);
	"@stealthcopter Example tweet from android application");
	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();

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.

   new AlertDialog.Builder(WordCube.this)
   .setTitle("Get Twitter")
   .setMessage("No twitter Application not found. Goto market and install one now?")
   .setNegativeButton(R.string.dialog_no, null)
                   .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

Where intentMarket() is defined a to open up the market and search for the specified application passed to it as a string.

public void intentMarket(String url){
	Intent i = new Intent(Intent.ACTION_VIEW); 
	Uri u = Uri.parse(url); 
	try {
	} catch (ActivityNotFoundException e) {
		toastMessage("Market not found.");

Below shows two screenshots from the wordcube application. This application is avaliable for android, see here for more information and download link (or visit market on your android device).

twidroid intent screenshot

twidroid intent called from wordcube

choice box after intent

choice box for twitter applications

Update: This is now taken into a complete function in this post.

Read More

Android: Adding styling to a text (bold italic etc..)

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 “boring regular text”

((TextView)this.findViewById("boring regular text \n")

This is good but what if you want to include styling in your text, so bold emphasis or italics. Well it’s very simple we first add the android.text.html include:

import android.text.Html;

and then surround our code in Html.fromHtml() this causes them html styling to modify our text.

((TextView)this.findViewById("excitingand cooltext

Below shows a screenshot of the regular styling and the html styling in an android application.

styled android text

styled and regular android text

Read More