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 GitHubhttps://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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: