Easy Video Chooser Library for Android

After I wrote the “Easy Image Chooser library for Android“, I thought I could easily extend this library to handle other things as well. For now, I have implemented the option of choosing/recording a video. And in the future, I am planning to extend this library for choosing other types of files.

Choosing videos, is again very similar to how the images are handled. Using this library, you would be also able to choose videos that reside in your Picasa albums on your phone. And this should work for all OSes and devices.

After processing, the library would return a ChosenVideo object, which would contain the original path to the video file, and 2 differently-sized thumbnails which you can directly use as a preview.

Here is how to use this library.

  • For capturing a video using the camera

videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_CAPTURE_VIDEO);

  •  For choosing a video from the gallery

videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_PICK_VIDEO);

  • On Activity Result, write this

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.REQUEST_PICK_VIDEO ||
requestCode == ChooserType.REQUEST_CAPTURE_VIDEO)) {
videoChooserManager.submit(requestCode, data);

public void onVideoChosen(final ChosenVideo video) {
runOnUiThread(new Runnable() {
public void run() {
if (video != null) {
// Use the video
// video.getFilePathOriginal();
// video.getFileThumbnail();
// video.getFileThumbnailSmall();

public void onError(final String reason) {
runOnUiThread(new Runnable() {
public void run() {
// Show error message

Here’s the link to the Github project. Do let me know your thoughts and feedbacks, and also any bugs if you come across. I will try to fix them as soon as possible.

Easy Image Chooser Library for Android

In almost all the Android apps that I have worked on, there has been a requirement for choosing an image or taking a snap and using the device’s camera.

Taking a snap, is rather straightforward to implement, but choosing an image from your gallery is sometimes a hard nut to crack. And to implement this correctly, you would always end up writing a lot of code. Assuming that you want to target all OSes, devices, folders etc.

For example, choosing a picture from the Camera folder of your phone is nice and easy. But, if you want to have the user chose an image from one of his picasa web albums, which he has synced on his phone, you will find a dead-end. Well, almost.

For myself, I must have at-least rewritten the same code, multiple number of times. Of course, the platform should help us in achieving this seemingly straightforward feature with as less code as possible. But, right now, it’s not possible.

So, I thought of creating a library which anyone could integrate within one’s own app, without really worrying about the nitty-gritties and the bugs, to implement or add this feature into his app. And there’s more. Most of the time, you would need a scaled down version of the chosen image to show a preview or use it in a listview. This library would give you 3 sizes, as of now.

  1. Original Size
  2. Thumbnail Size
  3. Thumbnail Smaller Size
The sizes are dynamically calculated, based on the original size of the image. It’s pretty rough here, but I am looking forward to improve that part.
This version of the library is pretty basic. But, I would be working on improving this library to add more functionality in the future.
You can find more information about this library here.
By using this library in the current state, you could handle all these cases/special cases with just a few lines of code. An example of this is shown below.
1. For choosing an image from gallery

imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_PICK_PICTURE);

2. For capturing a picture using your camera

imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_CAPTURE_PICTURE);

3. On Activity result, do this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.
requestCode == ChooserType.
imageChooserManager.submit(requestCode, data);

4. Implement the ImageChooserListener interface and override these methods:

public void onImageChosen(final ChosenImage image) {
runOnUiThread(new Runnable() {
public void run() {
if (image != null) {
// Use the image
// image.getFilePathOriginal();
// image.getFileThumbnail();
// image.getFileThumbnailSmall();
public void onError(final String reason) {
runOnUiThread(new Runnable() {
public void run() {
// Show error message

That’s all you need to code. And the library takes care of handling all kinds of images, and also generates 2 thumbnails which you could directly use. Let me know if you need to add any new features or if you find a bug. I will try to address those as soon as humanly possible. If you would like to contribute to this project, drop me a mail.

Using Custom fonts on Android

I had to do this for a project that I am working on, and I felt that it wasn’t possible to achieve this. Well, there are several examples on the internet regarding this and nothing seemed to be working for me. So, in this post, we will see how to use custom fonts for your application on Android.

Few things before we start off:

  • Not all fonts are compatible with Android
  • You need to package the ttf files with your apk
  • It’s obviously a little bit of extra work

So for this example, we have a TextView and a Button with different fonts. To be able to use your custom font everywhere, ie, on all the TextView and Button widgets in your app, you will have to extend these classes to create your own TextView and Button classes. I have named them as MyTextView and MyButton. And then, I can use these buttons in my layout xml files, with the fully-qualified name of my custom classes.

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

        android:layout_marginTop=”10dip” android:id=”@+id/textView”
        android:layout_width=”fill_parent” android:layout_height=”wrap_content”
        android:text=”@string/hello” android:textSize=”20sp”></com.beanie.samples.customfont.MyTextView>

        android:layout_marginTop=”10dip” android:id=”@+id/textView”
        android:layout_width=”fill_parent” android:layout_height=”wrap_content”
        android:text=”@string/hello” android:textSize=”20sp”></com.beanie.samples.customfont.MyButton>

In both these classes, we have a method called init() which is called from all the constructors. The method is just 3 line long.

        if (!isInEditMode()) {
                  Typeface tf = Typeface.createFromAsset(getContext().getAssets(), “fonts/ds_digib.ttf”);

Simple!!! You might notice that there is an if condition. This is not required, but it does help when you are preparing your layouts on eclipse, and you need to keep checking if everything’s fine. This method, isInEditMode() returns true while eclipses tries to render the view from the XML.

This is what the documentation says:

Indicates whether this View is currently in edit mode. A View is usually in edit mode when displayed within a developer tool. For instance, if this View is being drawn by a visual user interface builder, this method should return true. Subclasses should check the return value of this method to provide different behaviors if their normal behavior might interfere with the host environment. For instance: the class spawns a thread in its constructor, the drawing code relies on device-specific features, etc. This method is usually checked in the drawing code of custom widgets.

If you don’t put this condition, the layout editor will complain about not being able to set the TypeFace. So, in the layout editor, you will see your TextView and Button widgets with the default font. But, when you run your app on an emulator or a device, you can see the goodness of your custom fonts.

The source code for the sample project can be found here. You can find 3 different fonts (ttf file) in the assets folder to play with.

Android Market on Emulator (2.2)

It’s been some time that I had tried getting the Market up and running on a 2.2 a.k.a Froyo emulator. On a previous post, I had described how to setup an emulator (1.5 and 1.6) with the Market app. Today, I got a comment on that post which points to a 2.2 image that does the same. You just have to download that image file and follow exactly the same steps to set it up.

This is the link to the system.img file. Thanks Anonymous for your contribution. I have tried this, and it works. Here’s a link (David’s Blog) that anonymous referred to. He has described all the steps to create this system image needed to put the Market app on emulator.

A few things: 
– This image will only work on a 2.2 emulator
– While creating the emulator, you should specify the cache partition size as 96 MB.

Check out the previous post for more details.

There are however a few limitations that I found with this hack.

  1. The Android Market app doesn’t seem to update itself to the new UI that we currently have on our phones.
  2. There are only a limited number of apps that show up on the Market. (No Angry Birds for example)

Custom AutoComplete for Android

The default custom AutoCompleteTextView is quite a nice widget. But if you want to extend it’s functionality, you will need to write your own custom widget. As an example, if you wish to have a EditText for a “To” address field as any email application has, where you want to collect multiple selections from the list that pops up, you will need to extend the AutoCompleteTextView class and write your own small little widget. It’s very simple to create one that will suit your need.
So here we go.
1. Class CustomAutoComplete.java

   1: package com.beanie.example.widgets;
   2: import android.content.Context;
   3: import android.text.TextUtils;
   4: import android.util.AttributeSet;
   5: import android.widget.AutoCompleteTextView;
   7: public class CustomAutoComplete extends AutoCompleteTextView {
   8:     private String previous = "";
   9:     private String seperator = ";";
  10:     public CustomAutoComplete(final Context context, final AttributeSet attrs, final int defStyle) {
  11:         super(context, attrs, defStyle);
  12:         this.setThreshold(0);
  13:     }
  14:     public CustomAutoComplete(final Context context, final AttributeSet attrs) {
  15:         super(context, attrs);
  16:         this.setThreshold(0);
  17:     }
  18:     public CustomAutoComplete(final Context context) {
  19:         super(context);
  20:         this.setThreshold(0);
  21:     }
  22:     /**
  23:      * This method filters out the existing text till the separator
  24:      * and launched the filtering process again
  25:      */
  26:     @Override
  27:     protected void performFiltering(final CharSequence text, final int keyCode) {
  28:         String filterText = text.toString().trim();
  29:         previous = filterText.substring(0,filterText.lastIndexOf(getSeperator())+1);
  30:         filterText = filterText.substring(filterText.lastIndexOf(getSeperator()) + 1);
  31:         if(!TextUtils.isEmpty(filterText)){
  32:             super.performFiltering(filterText, keyCode);
  33:         }
  34:     }
  35:     /**
  36:      * After a selection, capture the new value and append to the existing
  37:      * text
  38:      */
  39:     @Override
  40:     protected void replaceText(final CharSequence text) {
  41:         super.replaceText(previous+text+getSeperator());
  42:     }
  43:     public String getSeperator() {
  44:         return seperator;
  45:     }
  46:     public void setSeperator(final String seperator) {
  47:         this.seperator = seperator;
  48:     }
  49: }

This class is the main widget class that extends thet AutoCompleteTextView. You have to override 2 methods,

protected void replaceText(final CharSequence text)

protected void performFiltering(final CharSequence text, final int keyCode)

2. You main layout file (main.xml)

   1: &lt;?xml version="1.0" encoding="utf-8"?&gt;
   2: &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:orientation="vertical" android:layout_width="fill_parent"
   4:     android:layout_height="fill_parent"&gt;
   5:     &lt;com.beanie.example.widgets.CustomAutoComplete android:layout_width="fill_parent"
   6:         android:layout_height="wrap_content" android:id="@+id/autocomplete"/&gt;
   7: &lt;/LinearLayout&gt;

3. Now we test it. This is your activity class.

   1: package com.beanie.example;
   2: import android.app.Activity;
   3: import android.os.Bundle;
   4: import android.widget.ArrayAdapter;
   5: import com.beanie.example.widgets.CustomAutoComplete;
   7: public class TestAutoComplete extends Activity {
   8:     /** Called when the activity is first created. */
   9:     @Override
  10:     public void onCreate(Bundle savedInstanceState) {
  11:         super.onCreate(savedInstanceState);
  12:         setContentView(R.layout.main);
  13:         CustomAutoComplete myAutoComplete = (CustomAutoComplete)findViewById(R.id.autocomplete);
  14:         ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_dropdown_item_1line);
  16:         adapter.add("aaaa");
  17:         adapter.add("abaa");
  18:         adapter.add("acaa");
  19:         adapter.add("adaa");
  20:         adapter.add("aaba");
  21:         adapter.add("aaca");
  22:         adapter.add("aaba");
  23:         adapter.add("aaae");
  25:         myAutoComplete.setAdapter(adapter);
  26:     }
  27: }

1Voila, you are done. Here’s a screenshot of our Custom Auto-Complete Text view at work.

You can also change the separator from the default “;” to any other character like a “,” or anything else.
On the adapter, you call the method setSeparator(String any);
Now you have your own custom auto-complete widget for Android.

For this particular example, however, Android provides you a widget by default. MultiAutoCompleteTextView is specifically designed to handle such kind of input.