How to detect emails, phone numbers, web URLs, hashtags in Android TextView? : SmartTextView library

Smart Text View

Star On GitHub Follow On GitHub

Many applications in android use TextView that detects and highlight various string patterns like emails, phone numbers, web URLs and even hashtags.

This functionality is very common and and is used extensively and frequently in many app. Thus to ease the process of adding this functionality I have authored an Android library SmartTextView.

Find and Star it on GitHub : https://github.com/priyankvex/SmartTextView

smart_text_view_small

Features

  • Detect emails, mobile numbers, URLs
  • Detect #hash_tags and @mentions
  • Use default intents or set custom callbacks.
  • Set different colors for each pattern.

 

Usage

compile 'com.priyankvex:smarttextview:1.0.1'

In layout xml file

  <com.wordpress.priyankvex.smarttextview.SmartTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="9pt"
        android:id="@+id/textView"
        />

In java Activity or Fragment

mSmartTextView = (SmartTextView) findViewById(R.id.textView);
        mSmartTextView.setEmailColorCode("#3cb371");
        mSmartTextView.setPhoneNumberColorCode("#ff33aa");
        mSmartTextView.setHashTagColorCode("#f37735");
        mSmartTextView.setUrlColorCode("#ffc425");
        mSmartTextView.setMentionColorCode("#57b884");
        mSmartTextView.setDetectMentions(true);
        mSmartTextView.setDetectHashTags(true);
        mSmartTextView.setText(sampleText);
        mSmartTextView.setSmartTextCallback(this);

To use custom callbacks

implements SmartTextCallback
Advertisements

Practice Shapes : Android App To Learn Shapes

Only way to learn something is to learn it by yourself.

Recently I have been playing with Canvas in android and it has been fun! ūüėÄ

As they say knowledge is knowing tomato is a fruit. Wisdom is not putting it in vegetable basket. And best way to persist new gained “wisdom” is to put it in use.

Practice Shapes is a fun app both for kids and grown-up kids (Yes. You!), to test their drawing skills. Though I know drawing on a mobile screen¬†with your fingers is not your favorite thing, but still you will enjoy it! ūüėÄ

Download the app from play store :

 

This app is open source : You can fork it at https://github.com/priyankvex/Practice-Shapes/

 

TECH STACK : 

  • Android¬†SDK
  • Bitmap Images of Shapes

The power lies in not reinventing the wheel, but to use it to move forward.

OPEN SOURCE LIBRARIES USED : 

  1. Universal Image Loader
  2. Sugar ORM
  3. Fancy Buttons

To Create Images : 

To create images for the app on which user draws, i used GIMP. These are just simple images with transparent background.

Right now, there are total of 18 images in the app under 3 difficulty levels.

If you can add more images feel free to do so.

Screenshots of app : 

drawing_ss

hard_shapes_ssscore

Download the app from play store :


Rails Number Helper Port to Android

A polyglot developer will understand, there is always something you like about one language and you wish was available for another language too.

So, recently I started learning rails and found number_helper API very cool and fun to use. It gives you easy API to convert numbers into many formats like number_to_human which will convert 20000 to 20 Thousands or number_to_human_size that will convert 1024 to 1 KB.

That was it I wanted this stuff for Android too. It just makes processing numbers to fit the UI needs so damn easy.

Here is my attempt to port this rails module to Android.

Here is the source code :

https://github.com/priyankvex/Rails-Number-Helper-For-Android

Usage : 

Just copy the module number_helper in your application folder and include module dependency.

Examples : 

 


        // Number to human converter
        NumberToHumanConverterBuilder builder2 = new NumberToHumanConverterBuilder(123456);
        NumberToHumanConverter converter2 = builder2.build();
        try {
            String humanNumber = converter2.convert();
            // OUTPUT : 123.456 Thousands
            Log.d(Config.TAG, humanNumber);
        } catch (InvalidSeparatorException | InvalidDelimiterException
                | InvalidPrecisionException e) {
            e.printStackTrace();
        }

Examples can be found here.
https://github.com/priyankvex/Rails-Number-Helper-For-Android/blob/master/app/src/main/java/com/wordpress/priyankvex/numberhelperdemo/MainActivity.java

Features : 

  • Easy to use API.
  • High utility for UI needs.
  • Small in size.
  • Custom exceptions to fit the needs of Android.

 

Making GCM client in Android and Application server in Ruby on Rails

It is always fun to play with new APIs and tools. GCM is not something I was getting my hands on for the first time but it changed completely since last time I did a project using this.

But the good news is it has changed for good. New service classes makes it easier to write GCM clients for Android, GCM default configuration makes writing test servers and apps to play with very less tedious. In fact my first reaction was this is GCM reincarnated. 

GCM Client and App server
GCM Client and App server

New API : New Party!

You can have a look at the source code here :

https://github.com/priyankvex/GCM-Android-Client-and-Rails-Server

To create Android client I stuck with sample provided in the docs. Sweet and simple to implement. Just little tweaks done to make it fit my needs.

For creating application server i.e the 3rd party server required by the GCM stack, it used Ruby on Rails. In that specifically I used “gcm”¬†gem.

Again not a very complex server but good enough to get a hold of things.

Server features : 

  1. Save incoming device tokens (registration ids).
  2. Web form to send message to all devices registered.
  3. Send welcome notification when device is registered.

Client features (Android App) :

  1. Get GCM token for the device.
  2. Send token to applications server.
  3. Show notification when message is pushed from server.

 

Paint App in Android

Recently I was learning about Canvas and Custom views in Android and to put the theory in practice I made a pretty simple Paint App. 

Source code of the app can be found https://github.com/priyankvex/Paint-App-Android.

Screenshot of the app
Screenshot of the app

 

The App is pretty simple with features
1.  Create Drawings (Obvious one :P).
2.  Select different brush sizes.
3.  Eraser with different sizes.
4.  Color pallet.
5.  Save drawings in Gallery.

Source code of the app can be found https://github.com/priyankvex/Paint-App-Android.

Brownie points if you can guess the character I drew! ūüėÄ

Making an OCR app for Android using Tesseract.


Star on GitHub

Recently I was playing with OCR library by google called as “Tesseract” (cool name for a library!).

App in action.
App in action.

 

Screenshot_2015-08-29-23-08-51

 

 

 

 

 

 

 

 

It was a fun experience. This post shows how you can make a simple OCR app in Android using Tesseract.

We will be using Tess-Two a fork of Tesseract with some additional tools like Liptonica which is an image processing library.

If you want an even easier way to get started with OCR on Android you can try this library built by me. Easy OCR Library. Usage instructions are in the ReadMe.md file there.

Anyways, moving forward I am using Android Studio on Ubuntu 64 bit machine here.

Step 1 : 

Clone the library Tess-Two.


git clone git://github.com/rmtheis/tess-two tess

Step 2 :

Now we need to build  the library.

For building we will need Android NDK.


cd tess
cd tess-two
ndk-build
android update project --path .
ant release

Building may take some time so be patient. Don’t press ctrl+c too soon ūüėõ .

Step 3  :

Yay! Time to use the library in Android Project.

Copy the tess/tess-two folder into the root folder of your application project.

Step 4 : 

In the tess-two folder you just pasted. Add build.gradle file as Android Studio uses gradle build system.

Add following gradle script in the file.


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 22
    }

    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
    }
}

Step 5 : 

Add the following line in project.settings file.

include ':tess-two'

Step 6 :

Now we have successfully included the Tess-Two library in our project and we are ready to use it.

First we need to capture the picture itself. You can use something like this code sample taken from Easy OCR Library.


public void takePicture(){
        Intent e = new Intent("android.media.action.IMAGE_CAPTURE");
        this.filePathOriginal = FileUtils.getDirectory(this.directoryPathOriginal) + File.separator + Calendar.getInstance().getTimeInMillis() + ".jpg";
        e.putExtra("output", Uri.fromFile(new File(this.filePathOriginal)));

        startActivity(e);
    }

Or you can find the code here.

We will also downscale the image a little so that the recognition is fast.

You  can use following code sample from again Easy OCR Library


 private Bitmap getBitmapFromPath() {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 4;
        Bitmap bitmap = BitmapFactory.decodeFile(this.filePath, bmOptions);
        return bitmap;
    }

Step 7 :

Final step. Recognize the text using the library API.


 private String scanImage(){
        TessBaseAPI baseApi = new TessBaseAPI();
        Log.d(Config.TAG, "Data path : " + FileUtils.getDirectory(this.directoryPath));
        baseApi.init(FileUtils.getDirectory(this.directoryPath) + "/", this.trainedDataCode);
        baseApi.setImage(this.mBitmap);
        String recognizedText = baseApi.getUTF8Text();
        baseApi.end();

        return recognizedText;
    }


Again I would recommend using the Easy OCR Library if you are having facing any problem.

That library has many features :

  1. Very easy setup.
  2. Handles all the image processing part in a background thread.
  3. Provides very interface with relative callbacks for the functions of the library.

 

How to build Tesseract OCR library for Android Studio?

Star On GitHub

If you ever tried to create an OCR app for Android you must have stumbled upon the OCR library by Google Tesseract. And then the problems began.

To use the library in your project you first need to build it. But building the library to be compatible with gradle, which is the new build system for Android projects is little not so easily stated anywhere in the library manual.

When I tried to build the library, it took me freaking 9 hours to figure all the how tos?

So, here I am helping you to save your precious hours. (Don’t waste these watching late night infomercials for god’s sake! ).

Here we go.

Step 1 : 

The first step. Download the NDK. That is used to build the library.

Download it from here. It is around 300+ MB so keep your net plan nourished.

Step 2 : 

Better way to go is to use  a fork of Tesseract, Tess-Two. Tess-Two can be found on GitHub.

Execute following commands to build the library Tess-Two using NDK.


git clone git://github.com/rmtheis/tess-two tess
cd tess
cd tess-two
ndk-build
android update project --path .
ant release

We can live without building eyes-two for this time.

Building will take few minutes. After you have successfully built tess-two, give yourself a little treat. You are almost done.

Step 3 :

Now you are ready to use the library in your Android project.

Copy the tess-two folder (tess/tess-two), in the main folder of the application project.

Suppose name of your application is “MyApp” copy the folder at¬†“MyApp/”.

Step 4 :

Now its time to play the trick. The library was build using ANT. But Android projects use Gradle these days. Interesting…

We need to add a “build.gradle” file at location “tess-two/”.

The build file can be found here.

Then include following line in project.settings file.

include ':tess-two'

Step 5 : 

Build the project and you are just one step away from being done.

Step 6 : 

The most important step. After completing steps 1-5,¬†throw your hands up in the air!¬†ūüėÄ ūüėÄ You are now done!

Ready to harness the power of OCR.

 


https://buttons.github.io/buttons.js