Tag Archives: Android

Using MonkeyTalk in AndroidStudio


Let me first introduce what MonkeyTalk is?
Monkey talk is a mobile app testing tool. MonkeyTalk automates real, functional interactive tests for iOS and Android apps – everything from simple “smoke tests” to sophisticated data-driven test suites. Native, mobile, and hybrid app, real devices or simulators. Moreover, automation saves time, it is re-usable, repeatable and so on. Monkey talk is an open source automation tool. It supports both Android and iPhone. It is very easy to learn and its a powerful functional testing tool.

Everything is in the web. So, why am I writing this blog?
Its because recently I have switched to android studio for android development. There are loads of tutorial based in Eclipse but very few based in Android Studio. So, I thought maybe this will be useful for everyone out there who is struggling to use MonkeyTalk in Android Studio.

Requirements for MonkeyTalk are….
1. Android Studio
2. MonkeyTalk IDE which you can download from this link

Procedure :
Before you proceed with this blog, I presume you know how to use android studio and gradle build system.
So, here we start.

There are two important steps to be followed:

Step 1: We build an instrumented apk using Android Studio

Step 2: We perform automation on instrumented apk using MonkeyTalk IDE

How to make an instrumented apk ?

      1. I assume you have a ready project in Android Studio which can be compiled successfully using Android Studio. You can get a test project from this github link.
      2. Now, in your app folder create a new folder named monkey-libs folder. Inside this folder place Monkey Talk Agent as shown in the github.
      3. Then, in your build.gradle file located inside app folder, add the aspectj dependency inside buildscript
         buildscript {  
           repositories {  
             mavenCentral()  
           }  
           dependencies {  
             classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.5'  
           }  
         }  
        
      4. Add aspectj plugin in dependencies of buildscript
      5. Then to activate the aspectj plugin, write the given code in build.gradle file
         apply plugin: 'android-aspectj'  
        
      6. Then compile the Java file with AspectJ plugin by writing the following code in build.gradle file
         dependencies {  
         compile fileTree(dir: 'libs', include: ['*.jar'])  
         monkeytalkCompile fileTree(dir: 'monkey-libs', include: ['*.jar'])  
         }  
        
      7. Thats all you need to do in build.gradle file.
      8. Now we need to create an instrumented apk. To get instrumented apk you need to create a folder name monkeytalk inside src folder. This monkey talk folder must contain AndroidManifest file. Just one AndroidManifest file is required. You can see the sample here .
      9. AndroidManifest file located inside monkeytalk must consist of the following permissions:
         <?xml version="1.0" encoding="utf-8"?>  
         <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
           package="com.georgepapas.monkeytalkdemo" >  
           <uses-permission android:name="android.permission.GET_TASKS"/>  
           <uses-permission android:name="android.permission.INTERNET"/>  
         </manifest>  
        
      10. and thats all. A build instrumented with monkey talk aspects will be generated and installed in the emulator or your device by running the given code:
         ./gradlew clean installMonkeytalk  
        

Hence, you can find an instrumented apk in your device or emulator.

Now, how to run the automation ?

      1.  Start your monkey talk ide.
      2. Go to File > New > MonkeyTalk Project > project name (say TestMonkeyTalk)
      3. Right click on project TestMonkeyTalk > New > Script > ScriptName (say testScript)
      4. Open your device which has debugging mode enabled and start your app.
      5. Press Connect > Android Device.
      6. Then press the Record Button and start using your app. As you finish visiting all the pages of the app, press stop button.
      7. Hence, now you have a Script for automation.
      8. Now, to start the automation, press the play button.

Hence, you have successfully automated your testing procedure.

Good luck with monkey talk.

See you later in the next blog.

Until then Happy Coding 🙂

Advertisements

Uploading Image from Android App to Server ~ Programmatically :)


Its been such a long time ever since I have posted anything into the blog. Well not posting into it does not mean I have not learned anything new in the mean time. Well, I have learned so many different things in this month that I really have not been able to keep track of it. But, I will eventually post about all of them. Today, I am posting about the way we perform image uploading from android app to server. 

I thought uploading image to server was really hard. I researched for whole day. Different blogs were giving different types of suggestions. I was literally going through paralysis analysis. Then,after a day I finally uploaded image to the server. So, I am feeling great and happy right now.

To perform uploading, you will have to first understand the following things:

HTTP is a protocol for exchanging messages between a client and a server. An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection. Request has three parts : a method name – GET or POST method , resources and the version of HTTP being used. Response also has three parts: the HTTP version, a response status code that gives the result of the request, and status code.

GET method : Requests data from a specified resource.

Query strings (name/value pairs) is send in the URL of a GET request. It should only be used to retrieve data

POST method : Submits data to be processed to a specified resource

Query strings (name/value pairs) is sent in the HTTP message body of a POST request.

Requests made through these methods return an instance of HTTPResponse. This instance of HTTPResponse has methods for accessing the response headers ( getHeader()), response info (getStatusCode()), response data (getData(), getInputStream()) and any trailers that might have been sent.

Although the java.net packages provides basic functionality for accessing resources via HTTP, it doesn’t provide the full flexibility or functionality needed by many applications.

So we need following jar file which can be downloaded from here to get an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations-

  • httpcore-4.3.2.jar
  • httpmime-4.3.1.jar

Hence, we have understood how Client-Server communication is handled. Now through the following code snippet, we can upload image to the server:

private class ImageUploader extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String result = "";

			// Client-side HTTP transport library
			HttpClient httpClient = new DefaultHttpClient();

			// using POST method
			HttpPost httpPostRequest = new HttpPost(imagePostUrl);
			try {

				// creating a file body consisting of the file that we want to
				// send to the server
				FileBody bin = new FileBody(imageFile);

				/**
				 * An HTTP entity is the majority of an HTTP request or
				 * response, consisting of some of the headers and the body, if
				 * present. It seems to be the entire request or response
				 * without the request or status line (although only certain
				 * header fields are considered part of the entity).
				 * 
				 * */
				MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
				multiPartEntityBuilder.addPart("images[1]", bin);
				httpPostRequest.setEntity(multiPartEntityBuilder.build());

				// Execute POST request to the given URL
				HttpResponse httpResponse = null;
				httpResponse = httpClient.execute(httpPostRequest);

				// receive response as inputStream
				InputStream inputStream = null;
				inputStream = httpResponse.getEntity().getContent();

				if (inputStream != null)
					result = convertInputStreamToString(inputStream);
				else
					result = "Did not work!";
				return result;
			} catch (Exception e) {

				return null;
			}

			// return result;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			uploadStatus.setText("Uploading image to server");
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			uploadStatus.setText(result);
		}

	}

	private static String convertInputStreamToString(InputStream inputStream)
			throws IOException {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(inputStream));
		String line = "";
		String result = "";
		while ((line = bufferedReader.readLine()) != null)
			result += line;

		inputStream.close();
		return result;

	}

References:

http://www.jmarshall.com/easy/http/

http://www.innovation.ch/java/HTTPClient/getting_started.html

http://hc.apache.org/downloads.cgi

http://stackoverflow.com/questions/20284542/upload-photo-using-httppost-multipartentitybuilder

http://stackoverflow.com/questions/18964288/upload-a-file-through-an-http-form-via-multipartentitybuilder-with-a-progress

Relationship of Android with Thread


A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions. It controls what executes in what order.

When using threads, CPU gives you the illusion that it’s doing multiple computations at the same time. It does that by spending a bit of time on each computation.

Threads are like two people using the same computer, who don’t have to share data explicitly but must carefully take turns. Conceptually, threads are just multiple worker bees buzzing around in the same address sapce. Each thread has its own stack, its own instructor pointer(aka program counter), etc. , but all threads in a process share the same memory.

You can kill one thread and others will still be running.

Android’s user interface thread

1.1 Main Thread
Android modifies the user interface and handles input events from one single user interface thread. This thread is also called the main thread.

Android collects all events in a queue and process an instance of the looper class.

xlooper_messagequeue10.png.pagespeed.ic.bCJly9aOKA

Is Main Thread enough for processing large data in Android ?


No, it’s not. If only main thread is used then all code of an Android application runs in the main thread and every statement is executed after each other.
If your perform the long lasting operation, for example accessing data from the internet, the application blocks until the corresponding operation has finished.

Then, what is the solution ?
To provide a good user experience, all potentially slow running operation in an Android application should run asynchronously, e.g. via some way of concurrency constructs of the Android framework. This includes all potential slow operations, like network, file and database access and complex calculations.

Android support the usage of the Thread class to perform asynchronous processing.

How to use threads in Android to obtain concurrency ?
Android provides android.os.Handler class or the AsyncTasks classes to handle concurrency.

1. android.os.Handler:
Handler class is used to register to a thread and provide a simple channel to send data to this thread. A Handler object registers itself with the thread in which it is created.
To use a handler, you have to subclass it and override the handleMessage() method to process messages.
Your thread can post messages via the sendMessage(Message) method or via the sendEmptyMessage() method to the Handler object.

For example:
The following code demonstrates the usage of a Handler via a View.
Assume your activity uses the following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="10"
        android:padding="4dip" >
    </ProgressBar>

   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
      </TextView>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startProgress"
        android:text="Start Progress" >
    </Button>

</LinearLayout> 

With the following the ProgressBar get updated once the users presses the Button.

public class ProgressTestActivity extends Activity {
  private ProgressBar progress;
  private TextView text;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progress = (ProgressBar) findViewById(R.id.progressBar1);
    text = (TextView) findViewById(R.id.textView1);

  }

  public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        for (int i = 0; i <= 10; i++) {
          final int value = i;
           doFakeWork();
          progress.post(new Runnable() {
            @Override
            public void run() {
              text.setText("Updating");
              progress.setProgress(value);
            }
          });
        }
      }
    };
    new Thread(runnable).start();
  }

  // Simulating something timeconsuming
  private void doFakeWork() {
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

} 

2. AsyncTask:
The AsyncTask class encapsulates the creation of background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.

How to use AsyncTask ?

To use AysncTask, you must subclass it. AsyncTask uses generics and varargs. The parameters are the following

 AsyncTask <TypeOfVarArgParams , ProgressValue , ResultValue> 

An AsyncTask is started via the

execute()

method. This method calls the

doInBackground()

and the

onPostExecute()

method.
TypeOfVarArgParams is passed into the doInBackground() method as input, ProgressValue is used for progress information and and ResultValue must be returned from doInBackground() method and is passed to onPostExecute() as a parameter.

The doInBackground() method contains the coding instruction which should be performed in a background thread. This method runs automatically in a separate thread.

The onPostExecute() method synchronizes itself again with the user interface thread and allows it to be updated. This method is called by the framework once the doInBackground() method finishes.

The following code demonstrates how to use the AsyncTask class to download the content of a webpage.

Create the following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/readWebpage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Load Webpage" >
    </Button>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Placeholder" >
    </TextView>

</LinearLayout> 

Add the android.permission.INTERNET permission to your AndroidManifest.xml file.

Change your activity to the following:

public class ReadWebpageAsyncTask extends Activity {
  private TextView textView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.TextView01);
  }

  private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";
      for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse execute = client.execute(httpGet);
          InputStream content = execute.getEntity().getContent();

          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
          String s = "";
          while ((s = buffer.readLine()) != null) {
            response += s;
          }

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
    }
  }

  public void onClick(View view) {
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.vogella.com" });

  }
} 

If you run your application and press your button then the content of the defined webpage is read in the background. Once this process is done your TextView is updated.

Handler Vs AsyncTask

handler vs AsyncTask
handler vs AsyncTask

WebP – enabling faster, smaller and more beautiful images


For an average app, image account for 69% of transferred bytes for mobile sites. The size of the image is growing fast because its a HiDPI world.

Hence, use WebP image format in your app.

Why?

WebP image format uses improved data compression. It uses both lossy and lossless modes.

Never heard of webp??

– WebM video format uses VP8 video codec.

-WebP is derived from VP8, essentially a key frame..

-WeP{P,M} are open-source, royalty free formats , open-sourced by Google in 2010. It has BSD-style license.

History of WebP….

– Initial release in 2010

Lossy compression of true-color graphics

-August, 2012

Lossless compression support

Transparency(alpha channel) support

-April, 2013

Color profile, animation , metadata

Now, ready for general purpose use, on the web and elsewhere!

Why should I use webp image format in my app?

In the age of Expensive Data Plans, it saves your bandwidth. Your app will have to download fewer bytes, it completes loading much earlier compared to the JPEG page.

How do I create a WebP file?

● Download WebP converter (Linux, OSX, Windows)
○ cwebp -q 80 image.png -o image.webp
○ dwebp image.webp -o image.png
● Download WebP Codec for Windows (Photo Viewer, Explorer, Office 2010+, …)
● Download Photoshop plugin (by Telegraphics)
● Download GIMP plugin
● ImageMagick, Pixelmator, XnView, IrfanView, GDAL, JPEGView have native support for
WebP
● Java, .NET, Flash, Python, Ruby, PHP bindings available to libwebp…
● img2webp.net online tool

WebP on Android !

Google+ on Android

● Photos and images comprise the vast majority of bytes
● On average, got 50% byte savings with WebP!
● Saving many terabytes of bandwidth per day…
● Saving our users money each time they use the app!

Android

Native library — all versions of Android

static {
System.loadLibrary("webp");
}
private Bitmap webpToBitmap(byte[] encoded) {
int[] width = new int[] { 0 };
int[] height = new int[] { 0 };
byte[] decoded = libwebp.WebPDecodeARGB(encoded, encoded.length, width, height);
int[] pixels = new int[decoded.length / 4];
ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
return Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888);
}

webp-android-backport  for Android <4.0

For Novice Android Developer: Display Google Maps in Android Devices


Google Map is one of the search website of Google that allows us

  • to map out public transit directions,
  • get the map and landmark of the place that we wish to visit by creating our own maps
  • add lines and markers for more comprehensive and detailed in the Google Map box
  • get real time traffic information
  • view streets and identify locations by navigating through Google getting a street leve image of any city around the world

All we have to do is to type the places that we wish to go and visit, and the whole search will come out making it easier for us to view our search result.

There are so many advantages of embedding Google Maps into our Android Application. Google provides an easy way to integrate Google Maps into our Android Applications. To integrate Google Maps into our Android Applications, we need to work with Google Maps Android API v2

  • Download and configure the Google Play services SDK. The Google Maps Android API is distributed as part of this SDK.
  • Obtain an API key. To do this, you will need to register a project in the Google APIs Console, and get a signing certificate for your app.
  • Specify settings in the Application Manifest.
  • Add a map to a new or existing Android project.

If you dont know how the above steps need to be carried out, then this tutorial is just for you:

Step 1:

First of all, Download and configure the Google Play services SDK, we have to follow the given steps:

Install Google Play Services From : Android SDK Manager > extras > Google Play Services

Google Play services SDK_02

Step 2:

Import the library file google-play-services_lib by following the given step:
a. File > Other > Android > Android Project From Existing Code…
b. path to sdk/extras/google/google_play_services/libproject/google-play-services_lib
c. click finish

Import-Projects-in-Eclipse

Step 3:

Create a New Android Application Project  and add the google play services as library in our project:

location_in_google_map_v2_link_library

Step 4:

To access the Google Maps servers with the Maps API, we have to add a Maps API key to our application.
The key is free, we can use it with any of our applications that call the Maps API, and it supports an unlimited number of users.We can obtain a Maps API key from the Google APIs Console by providing your application’s signing certificate and its package name.

Obtaining a key for your application requires several steps. These steps are outlined here, and described in detail in the following sections.

Step 1
Retrieve information about your application’s certificate.This can be done by running the following command in terminal….

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey 
-storepass android -keypass android

After running this code, you will get following data in your terminal:

Alias name: androiddebugkey
Creation date: Jun 12, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 51b8444f
Valid from: Wed Jun 12 15:35:07 NPT 2013 until: Fri Jun 05 15:35:07 NPT 2043
Certificate fingerprints:
     MD5:  C5:47:79:D0:8A:F7:8E:BB:A1:72:4A:8E:35:89:EB:D9
     SHA1: 8B:6F:B4:2D:0E:56:51:53:35:C3:DB:82:3C:9C:87:B9:04:33:6D:38
     Signature algorithm name: SHA1withRSA
     Version: 3

SHA-1 fingerprint: The fingerprint is a unique text string generated from the commonly-used SHA-1 hashing algorithm. Because the fingerprint is itself unique, Google Maps uses it as a way to identify your application.

SHA1: 8B:6F:B4:2D:0E:56:51:53:35:C3:DB:82:3C:9C:87:B9:04:33:6D:38

Step 2:

Create a new project in the Google APIs Console.

To create a new project for Google Maps Android API,select the project name in the upper left hand corner and then click Create.

api1

Step 3:

Then, select Services from the left navigation bar and add the Google Maps Android API v2 as a service for the project by turning it on.

maps_on

This displays the Google Maps Android API Terms of Service. If you agree to the terms of service, click the checkbox below the terms of service, then click Accept. This returns you to the list of APIs and services.

Step 4:

You’re now ready to get a Maps API key.

To get the key:

  • Navigate to your project in the Google APIs Console.
  • In the left navigation bar, click API Access.
  • In the resulting page, click Create New Android Key….
  • In the resulting dialog, enter the SHA-1 fingerprint , that we obtained from Step 1 and after semicolon ( ; ) add your package name

sha-1
The Google APIs Console responds by displaying Key for Android apps (with certificates) followed by a forty-character API key, for example: AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
Copy this key value. You will use it in the next step.

Step 6:

Finally, you can add your key to your application and begin development.

The easiest way to test that your application is configured correctly is to add a simple map. We will have to make changes in two files: main.xml,manifest.xml and MainActivity.java. Please note that the code below is only useful for testing our settings in an application targeting Android API 12 or later, This code should not be used in a production application.

To add the key to your application, your manifest should consist of the following fields:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

   package="com.example.demomapproject"

   android:versionCode="1"

   android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="16" />

    <!-- Because version 2 of the Google Maps Android API requires OpenGL ES version 2
     External services can detect this notification and act accordingly.
     So, we need to add this in our manifest -->

    <uses-feature

        android:glEsVersion="0x00020000"

        android:required="true"/>

    <!--Permissions that give the application access to
         Android system features and to the Google Maps servers.-->

    <permission

        android:name="com.example.demomapproject.permission.MAPS_RECEIVE"

        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.demomapproject.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <!--
     The following two permissions are not required to use

     Google Maps Android API v2, but are recommended.
    -->

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

     <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

     <!--
    From here, the Maps API reads the key value and passes it to the Google Maps server,
         which then confirms that you have access to Google Maps data.

        This element sets the key com.google.android.maps.v2.API_KEY to the value API_KEY
         and makes the API key visible to any MapFragment in your application. 
    -->    

        <meta-data

            android:name="com.google.android.maps.v2.API_KEY"

            android:value="AIzaSyA5idXLlKzifjzXIO_j9tQ7CAVYEZuH-10" />

        <activity

            android:name="com.example.demomapproject.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

Your main.xml should consist of something like this

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

Your MainActivity.Java should consist of something like this

package com.example.mapdemo;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Build and run your application. You should see a map. If you don’t see a map, confirm that you’ve completed all of the steps appearing earlier in this document.

Happy Coding

🙂

TextView HyperLink – Change Color and Remove Underline


Sometimes, we need to display hyperlink in textview.

This hyperlink can of following format:

<strong><a href=”\&quot;index.php?option=com_socialapp&amp;task=profile.viewprofile&amp;userid=42\&quot;”>admin</a> is attending <a href=”_QQQ_/jsocial_app/index.php?option=com_socialapp&amp;view=events&amp;task=viewevent&amp;eventid=137&amp;Itemid=238_QQQ_”>Alchemist Rocks</a>.</strong>

Which will be seen in the web view as displayed below

Screenshot-Mozilla Firefox (Private Browsing)

How do we create such text in android textview ?

Follow the given steps and you will successfully create a textview whose links will be displayed as shown in the webview:

1. Create a textview in xml

  <TextView  
       android:id="@+id/profileFeedTitle"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_alignParentTop="true"  
       android:autoLink="web" //link the content of web  
       android:textColorLink="#576586" //change the color of the link  
       android:textColor="#555555" />  

2. In your activity, initialize the String and textview as given in the following code snippet

 String webLinkText = <a href="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png"><img src="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png" alt="Screenshot-Mozilla Firefox (Private Browsing)" width="293" height="254" class="alignnone size-full wp-image-291" /></a>  
 TextView linkTextView = (TextView) findViewById(R.id.profileFeedTitle);  
 holder.profileFeedTitle.setText(Html.fromHtml(webLinkText)));  

3. Run your project.

When you run your project, you will see a textview similar to the give figure:

tt_1

Here, in the given figure, you will see the user names in blue color, which is just as we want……..
But, we dont need the underline. To remove underline

i. Create a new class named as URLSpanNoUnderline as shown in the given code snippet:

 package com.example;  
 import android.text.TextPaint;  
 import android.text.style.URLSpan;  
 public class URLSpanNoUnderline extends URLSpan {  
      public URLSpanNoUnderline(String p_Url) {  
           super(p_Url);  
      }  
      public void updateDrawState(TextPaint p_DrawState) {  
           super.updateDrawState(p_DrawState);  
           p_DrawState.setUnderlineText(false);  
      }  
 }  

ii. Go to your activity and replace the code

 String webLinkText = <a href="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png"><img src="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png" alt="Screenshot-Mozilla Firefox (Private Browsing)" width="293" height="254" class="alignnone size-full wp-image-291" /></a>  
 TextView linkTextView = (TextView) findViewById(R.id.profileFeedTitle);  
 holder.profileFeedTitle.setText(Html.fromHtml(webLinkText)));  

by the following code snippet:

 String webLinkText = <a href="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png"><img src="https://prativas.files.wordpress.com/2013/05/screenshot-mozilla-firefox-private-browsing.png" alt="Screenshot-Mozilla Firefox (Private Browsing)" width="293" height="254" class="alignnone size-full wp-image-291" /></a>  
 TextView linkTextView = (TextView) findViewById(R.id.profileFeedTitle);  
 //set user name in blue color and remove underline from the textview 
Spannable spannedText = Spannable.Factory.getInstance().newSpannable(
				Html.fromHtml(webLinkText));
Spannable processedText = removeUnderlines(spannedText);
if (holder.profileFeedTitle != null) {
	holder.profileFeedTitle.setText(processedText);
}  

iii. then, add the given function in your activity:



/**
 * Removes URL underlines in a string by replacing URLSpan occurrences by
 * URLSpanNoUnderline objects.
 *
 * @param p_Text A Spannable object. For example, a TextView casted as
 *               Spannable.
 */

 public static Spannable removeUnderlines(Spannable p_Text) {  
           URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);  
           for (URLSpan span : spans) {  
                int start = p_Text.getSpanStart(span);  
                int end = p_Text.getSpanEnd(span);  
                p_Text.removeSpan(span);  
                span = new URLSpanNoUnderline(span.getURL());  
                p_Text.setSpan(span, start, end, 0);  
           }  
           return p_Text;  
      }  

Then, run your code, you will get the perfect textview as shown in the given screenshot:

tt2

How did the underline disappeared ? What did the given class and function do ?

Heres the explanation :

As you may already know, the TextView object has a property named android:autoLink that creates HTML or email links automatically. You also got properties to change link colors such as android:textColorLink and android:textColorHighlight. The only missing option many of us would like to have is a way to remove the underline under the link itself.

Sadly, removing the underline seems to be somewhat “hard” to achieve. So i had added the given a news class and a new function.

Modify the Views of RelativeLayout in XML through CODE


right

We have developed the above layout using the following xml code:

1:  <RelativeLayout  
2:      android:id="@id/productHeader"  
3:      android:layout_width="wrap_content"  
4:      android:layout_height="wrap_content"  
5:      android:layout_below="@id/whiteView"  
6:      android:background="@drawable/product_detail_item_bg"  
7:      android:paddingBottom="5dp"  
8:      android:paddingLeft="14dp"  
9:      android:paddingRight="14dp"  
10:      android:paddingTop="5dp" >  
11:    
12:      <LinearLayout  
13:        android:id="@id/productMainImage"  
14:        android:layout_width="wrap_content"  
15:        android:layout_height="wrap_content"  
16:        android:layout_alignParentRight="true"  
17:        android:orientation="vertical"  
18:        android:paddingBottom="5dp"  
19:        android:paddingLeft="14dp"  
20:        android:paddingRight="14dp"  
21:        android:paddingTop="5dp" >  
22:    
23:        <ImageView  
24:          android:id="@id/primaryImageProduct"  
25:          android:layout_width="wrap_content"  
26:          android:layout_height="wrap_content"  
27:          android:layout_marginBottom="8dp"  
28:          android:src="@drawable/ic_launcher_icon" />  
29:    
30:        <TextView  
31:          android:id="@id/productMainImageTitle"  
32:          style="@style/SubTitleBold"  
33:          android:layout_width="wrap_content"  
34:          android:layout_height="wrap_content"  
35:          android:text="Exhibitor"  
36:          android:textSize="12sp" />  
37:      </LinearLayout>  
38:    
39:      <RelativeLayout  
40:        android:id="@+id/productInfoLayout"  
41:        android:layout_width="wrap_content"  
42:        android:layout_height="wrap_content"  
43:        android:layout_alignParentLeft="true"  
44:        android:layout_alignParentTop="true" >  
45:    
46:        <TextView  
47:          android:id="@id/productTitle"  
48:          style="@style/ListTextStyleHeading"  
49:          android:layout_width="wrap_content"  
50:          android:layout_height="wrap_content"  
51:          android:text="Samsung Galaxy SII"  
52:          android:textSize="14dp" />  
53:    
54:        <TextView  
55:          android:id="@id/productPriceLabel"  
56:          style="@style/SubTitleBold"  
57:          android:layout_width="wrap_content"  
58:          android:layout_height="wrap_content"  
59:          android:layout_below="@id/productTitle"  
60:          android:text="Price: " />  
61:    
62:        <TextView  
63:          android:id="@id/productPriceValue"  
64:          style="@style/SubTitleBold"  
65:          android:layout_width="wrap_content"  
66:          android:layout_height="wrap_content"  
67:          android:layout_below="@id/productTitle"  
68:          android:layout_toRightOf="@id/productPriceLabel"  
69:          android:text="Rs.199" />  
70:    
71:        <TextView  
72:          android:id="@id/productStallLabel"  
73:          style="@style/SubTitleBold"  
74:          android:layout_width="wrap_content"  
75:          android:layout_height="wrap_content"  
76:          android:layout_below="@id/productPriceLabel"  
77:          android:text="Stall No: " />  
78:    
79:        <TextView  
80:          android:id="@id/productStallValue"  
81:          style="@style/SubTitleBold"  
82:          android:layout_width="wrap_content"  
83:          android:layout_height="wrap_content"  
84:          android:layout_below="@id/productPriceValue"  
85:          android:layout_toRightOf="@id/productStallLabel"  
86:          android:text="# 201" />  
87:    
88:        <RatingBar  
89:          android:id="@id/productRatingBar"  
90:          style="@style/starRatingBar"  
91:          android:layout_width="wrap_content"  
92:          android:layout_height="wrap_content"  
93:          android:layout_below="@id/productStallLabel"  
94:          android:layout_marginBottom="8dp"  
95:          android:layout_marginTop="2dp"  
96:          android:numStars="5"  
97:          android:stepSize="1.0" />  
98:      </RelativeLayout>  
99:    </RelativeLayout>  

 

We have another class which can use the same layout. But with slight modification. The image must be at the right hand side of the  screen and the details must be at the left hand side of the screen . The modified layout must look as follows:
left

It would be very ineffective if we create another layout file for such a small purpose. We can change our layout dynamically through codes. The given code snippet will switch the position of the view

1:    
2:      Boolean layoutNeedsModification = true;  
3:           LinearLayout productMainImage = (LinearLayout) viewGroup  
4:                                      .findViewById(R.id.productMainImage);  
5:           RelativeLayout productInfoLayout = (RelativeLayout) viewGroup  
6:                                      .findViewById(R.id.productInfoLayout);  
7:    
8:    
9:       if (layoutNeedsModification) {  
10:                             RelativeLayout.LayoutParams params = new           RelativeLayout.LayoutParams(  
11:                                               RelativeLayout.LayoutParams.WRAP_CONTENT,  
12:                                               RelativeLayout.LayoutParams.WRAP_CONTENT);  
13:                             params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,  
14:                                               RelativeLayout.TRUE);  
15:    
16:                             productMainImage.setLayoutParams(params);  
17:    
18:                             params = new RelativeLayout.LayoutParams(  
19:                                               RelativeLayout.LayoutParams.WRAP_CONTENT,  
20:                                               RelativeLayout.LayoutParams.WRAP_CONTENT);  
21:                             params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,  
22:                                               RelativeLayout.TRUE);  
23:    
24:                             productInfoLayout.setLayoutParams(params);  
25:    
26:                    }  

 

As you can see, this is what you have to do:

  1. Create a RelativeLayout.LayoutParams object.
  2. Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don’t require values.
  3. Set the parameters to the view (in this case, to each button).

References:
Reference No. 1

Part -3: Handling User Interaction in App Widgets and Update Widgets Respectively


This app widget tutorial is with respect to the app widget present in Nepal Khabar present in the Google play store.

In the previous tutorials, I have written about

Basics of and App Widget
Multiple Sized Widdget

How to update an App Widget?

To update appwidget, we will use IntentService. IntentService is a special kind of service which performs an action and shuts itself down, once the action is complete. It is superb form small, periodic operations. IntentService can be used within the WidgetProvider class. The give code snippets will clarify this:

 public class OurWidget extends AppWidgetProvider {  
      public void onUpdate(Context context,  
                android.appwidget.AppWidgetManager appWidgetManager,  
                int[] appWidgetIds) {  
           int appWidgetId = INVALID_APPWIDGET_ID;  
           if (appWidgetIds != null) {  
                int N = appWidgetIds.length;  
                if (N == 1) {  
                     appWidgetId = appWidgetIds[0];  
                }  
           }  
           Intent intent = new Intent(context, UpdateWidgetService.class);  
           intent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);  
           intent.setAction("DO NOTHING ACTION");  
           context.startService(intent);  
      }  
      /**  
       * static class does not need instantiation UpdateWidgetService is a Service  
       * that identifies the App Widgets , instantiates AppWidgetManager and calls  
       * updateAppWidget() to update the widget values  
       */  
      public static class UpdateWidgetService extends IntentService {  
           public UpdateWidgetService() {  
                super("UpdateWidgetService");  
           } 
@Override
		protected void onHandleIntent(Intent intent) {
			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(this);

			int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
					INVALID_APPWIDGET_ID);
			if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
				updateAppWidget(appWidgetManager, incomingAppWidgetId,
						intent.getAction());

			}
		} 
 }  

This service must me started from the onUpdate method of our AppWidgetProvider. onUpdate is called when the widget gets added to the homescreen.
onHandleIntent(): called each time IntentService starts. The service shuts down once we exit this method.
This service is declared in manifest as follows:

 <service android:name="OurWidget.UpdateWidgetService"/>  

Now, lets update our widget using onHandleIntent():
In order to handle user interaction with an App Widget, the following tasks must be performed:

    1 . Set a unique click handler for each App Widget control
    2. Have the click handler send a command to a registered receiver
    3. Process the command received and perform any action necessary
    4. Update the App Widget to reflect the changes

The following code snippets will clarify the above points:
The app widget is updated and user interaction is handled through the given class:

 public class OurWidget extends AppWidgetProvider {  
      public static String ACTION_WIDGET_TITLE_CLICK = "Action_widget_title_click";  
      public static String ACTION_WIDGET_CONTENT_CLICK = "Action_widget_conent_click";  
      /**  
       * Called in response to the ACTION_APPWIDGET_UPDATE broadcast when this  
       * AppWidget provider is being asked to provide RemoteViews for a set of  
       * AppWidgets. Override this method to implement your own AppWidget  
       * functionality.  
       */  
      public void onUpdate(Context context,  
                android.appwidget.AppWidgetManager appWidgetManager,  
                int[] appWidgetIds) {  
           int appWidgetId = INVALID_APPWIDGET_ID;  
           if (appWidgetIds != null) {  
                int N = appWidgetIds.length;  
                if (N == 1) {  
                     appWidgetId = appWidgetIds[0];  
                }  
           }  
           Intent intent = new Intent(context, UpdateWidgetService.class);  
           intent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);  
           intent.setAction("DO NOTHING ACTION");  
           context.startService(intent);  
      }  
      /**  
       * static class does not need instantiation UpdateWidgetService is a Service  
       * that identifies the App Widgets , instantiates AppWidgetManager and calls  
       * updateAppWidget() to update the widget values  
       */  
      public static class UpdateWidgetService extends IntentService {  
           public UpdateWidgetService() {  
                super("UpdateWidgetService");  
           }  
           @Override  
           protected void onHandleIntent(Intent intent) {  
                AppWidgetManager appWidgetManager = AppWidgetManager  
                          .getInstance(this);  
                int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,  
                          INVALID_APPWIDGET_ID);  
                if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {  
                     updateOneAppWidget(appWidgetManager, incomingAppWidgetId,  
                               intent.getAction());  
                }  
           }  
           /**  
            * For the random passcode app widget with the provided ID, updates its  
            * display with a new passcode, and registers click handling for its  
            * buttons.  
            */  
           private void updateOneAppWidget(AppWidgetManager appWidgetManager,  
                     int appWidgetId, String whichButton) {  
                RemoteViews views = new RemoteViews(this.getPackageName(),  
                          R.layout.appwidget_layout);  
                onWidgetHeaderClick(views, appWidgetId);  
                onWidgetContentClick(views, appWidgetId);  
                bindDataToRemoteView(views, listOfQuedMessage.get(count));  
                //3. Process the command received and perform any action necessary  
                if (whichButton.equals(ACTION_WIDGET_VIEW_CLICK)) {  
                     views.setTextViewText(R.id.widgetHeader, "So you clicked Widget Title");                      
                } else if (whichButton.equals(ACTION_WIDGET_TITLE_VIEW_CLICK)) {  
                     views.setTextViewText(R.id.widgetContent, "So you clicked Widget Header");  
                }  
                appWidgetManager.updateAppWidget(appWidgetId, views);//4. Update the App Widget to reflect the changes  
           }  
           public void bindDataToRemoteView(RemoteViews views, Message message) {  
                views.setTextViewText(R.id.widgetHeader, "Widget Title");  
                views.setTextViewText(R.id.widgetContent, "Widget Header : Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh ");  
           }  
           //1. Set a unique click handler for each App Widget control  
           private void onWidgetHeaderClick(RemoteViews views, int appWidgetId) {  
                Intent btnNextIntent = new Intent(this, this.getClass());  
                btnNextIntent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);  
                btnNextIntent.setAction(ACTION_WIDGET_TITLE_CLICK);  
                PendingIntent btnNextPendingIntent = PendingIntent.getService(this,  
                          0, btnNextIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
                views.setOnClickPendingIntent(R.id.widgetHeader,  
                          btnNextPendingIntent);  
           }  
           private void onWidgetContentClick(RemoteViews views, int appWidgetId) {  
                Intent btnPrevIntent = new Intent(this, this.getClass());  
                btnPrevIntent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);  
                btnPrevIntent.setAction(ACTION_WIDGET_CONTENT_CLICK); //2. Have the click handler send a command to a registered receiver  
                PendingIntent btnNextPendingIntent = PendingIntent.getService(this,  
                          0, btnPrevIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
                views.setOnClickPendingIntent(R.id.widgetContent,  
                          btnNextPendingIntent);  
           }  
      }  
 }  

The layout for our App Widget:

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/full_widget"  
   android:layout_width="fill_parent"  
   android:layout_height="140dp"  
   android:layout_gravity="center_vertical"  
   android:layout_margin="20dp"  
   android:orientation="vertical" >  
   <TextView  
     android:id="@+id/widgetHeader"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:background="@android:color/black"  
     android:paddingLeft="5dp"  
     android:paddingRight="5dp"  
     android:text="TEST"  
     android:textColor="#FFFFFF"  
     android:textSize="14sp"  
     android:textStyle="bold" />  
   <View  
     android:layout_width="fill_parent"  
     android:layout_height="1dp"  
     android:background="#FFFFFF" />  
   <TextView  
     android:id="@+id/widgetContent"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:background="@android:color/black"  
     android:paddingLeft="5dp"  
     android:paddingRight="5dp"  
     android:text="Test"  
     android:textColor="#6F7981"  
     android:textSize="14sp"  
     android:textStyle="bold" />  
 </LinearLayout>  

The sample project is located in github.

My next tutorial will be about, Multiple instance of same app widget
References:
Styling Android
Developers Forum

Part -2: Creating Multiple Sized App Widgets


This app widget tutorial is with respect to the app widget present in Nepal Khabar present in the Google play store.

After App Widget Basics, lets learn how to add multiple sized app widget for our application. To create multiple sized appwidgets, we must create following things:

1.As we have defined appwidgetprovider receiver in manifest file in After App Widget Basics, add Receiver definition for small, medium and large receiver tag in manifest file
2. Create three app widget provider info, i.e. the xml file for small, medium and large app widget in res > xml file and include it in the meta-data of each appwidget provider
3. Create appwidgetprovider class for small, medium and large app widget

How to define receiver for multiple app widgets in receiver file ?
The following code snippets demonstrates how it can be done:
Receiver for large widget

  <receiver  
       android:name="WidgetProviderLarge"  
       android:icon="@drawable/ic_launcher"  
       android:label="@string/large_widget_name"  
        >  
       <intent-filter>  
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
       </intent-filter>  
       <meta-data  
         android:name="android.appwidget.provider"  
         android:resource="@xml/appwidgetproviderinfo_large" />  
     </receiver>  

Receiver for medium widget

  <receiver  
       android:name="WidgetProviderMedium"  
       android:icon="@drawable/ic_launcher"  
       android:label="large_medium"  
        >  
       <intent-filter>  
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
       </intent-filter>  
       <meta-data  
         android:name="android.appwidget.provider"  
         android:resource="@xml/appwidgetproviderinfo_medium" />  
     </receiver>  

Receiver for medium widget

  <receiver  
       android:name="WidgetProviderSmall"  
       android:icon="@drawable/ic_launcher"  
       android:label="small_widget" >  
       <intent-filter>  
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
       </intent-filter>  
       <meta-data  
         android:name="android.appwidget.provider"  
         android:resource="@xml/appwidgetproviderinfo_small" />  
     </receiver>   

While defining each appwidget receiver in manifest file, we must keep in mind

    android:name for each widget have to be different. If we provide same name to both the receivers then, only one widget will be visible in widgets list.
    we can use the same layout or different layout. Basically this is as per our requirement.

How to create appwidget provider info in xml for each app widget ?
The following code snippets demonstrates how it can be done:
app widgetprovider info for large widget in xml.

 <?xml version="1.0" encoding="utf-8"?>  
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
   android:initialLayout="@layout/layout_appwidget_large"  
   android:minHeight="115dp"  
   android:minWidth="250dp"  
   android:updatePeriodMillis="21600000" ><!-- 6hrs -->  
 </appwidget-provider>  

app widgetprovider info for large widget in xml

 

How to create appwidget provider class for each app widget ?
The following code snippets demonstrates how it can be done:
app widgetprovider class for large widget

 public class WidgetProviderLarge extends AppWidgetProvider {   
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,   
         int[] appWidgetIds) {   
       this.context = context;   
       Log.d("onUpdate", "called");   
       for (int widgetId : appWidgetIds) {   
         updateAppWidget(context, appWidgetManager, widgetId);   
       }   
    }   
 }  

app widgetprovider class for small widget

 public class WidgetProviderSmall extends AppWidgetProvider {   
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,   
         int[] appWidgetIds) {   
       this.context = context;   
       Log.d("onUpdate", "called");   
       for (int widgetId : appWidgetIds) {   
         updateAppWidget(context, appWidgetManager, widgetId);   
       }   
    }   
 }  

Check out the sample project in github .

Notice: if you want to find the label of appwidget from your configuration activty(optiona), then the following code snippet will help you:

      AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(  
                     getBaseContext()).getAppWidgetInfo(mAppWidgetId);  
           String appWidgetLabel = providerInfo.label;  

My next tutorial will be about Handling User Interaction in Widgets
Since then, happy coding 😀

Custom Layout in ActionBarSherlock


Today, we are going to create a IcsSpinner withing the ActionBarSherlock in Android. The snapshot is given below:
device-2013-02-05-141413

First of all, in res > menu > create menu_configuration_activity with following codes

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
3:    <item  
4:      android:id="@+id/menuConfigLanguageSelector"  
5:      android:actionLayout="@layout/layout_config_menu"  
6:      android:icon="@drawable/ic_setting"  
7:      android:showAsAction="always"  
8:      android:title="language"/>  
9:    <item  
10:      android:id="@+id/menuConfigDone"  
11:      android:icon="@drawable/ic_done"  
12:      android:showAsAction="always"  
13:      android:title="OK"/>  
14:  </menu>  

Here, in line number 5, we have assigned a custom layout. This custom layout is created in res > layout > layout_config_menu with following contents:

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <com.actionbarsherlock.internal.widget.IcsSpinner xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:id="@+id/languageSelectSpinner"  
4:    android:layout_width="wrap_content"  
5:    android:layout_height="wrap_content" />  

Then in the activity, where you want to place this actionbar, do as given below:

1:  @Override  
2:       public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {  
3:            getSupportMenuInflater().inflate(R.menu.menu_configuration_activity,  
4:                      menu);  
5:            MenuItem itemLanguageSelector = menu  
6:                      .findItem(R.id.menuConfigLanguageSelector);  
7:            IcsSpinner icsSpinner = (IcsSpinner) itemLanguageSelector  
8:                      .getActionView();  
9:          Context subContext = getSupportActionBar().getThemedContext();  
10:            ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(  
11:                      subContext, R.array.language_selection,  
12:                      com.actionbarsherlock.R.layout.sherlock_spinner_item);  
13:            list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);  
14:            icsSpinner.setAdapter(list);  
15:            icsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
16:                 @Override  
17:                 public void onItemSelected(IcsAdapterView<?> parent, View view,  
18:                           int position, long id) {  
19:                      //do something with the selected item  
20:                 }  
21:                 @Override  
22:                 public void onNothingSelected(IcsAdapterView<?> parent) {  
23:                      Log.i("Test click", "Inside on nothing selected");  
24:                 }  
25:            });  
26:            return super.onCreateOptionsMenu(menu);  
27:       };  
28:       @Override  
29:       public boolean onOptionsItemSelected(MenuItem item) {  
30:            if (item.getTitle().toString().equals("OK")) {  
31:                 if (selectedCategory != null && sourceAndLanguage != null) {  
32:                      try {  
33:                           // initAppWidget();  
34:                           saveTheUserValueInPref(selectedCategory, sourceAndLanguage);  
35:                           showAppWidget();  
36:                      } catch (Exception e) { // TODO: handle exception }  
37:                      }  
38:                 }  
39:            }  
40:            return super.onOptionsItemSelected(item);  
41:       }  

Dont forget to extend sherlockactivity in your activity to implement actionbarsherlock
Now, your IcsSpinner is ready to be used in ActionBarSherlock 😀
Happy Coding 😀