Implementing #hashtags in android application

Multiple Hashtags

Hashtags have become very popular in social networking websites and apps. But these can also be used in many other ways in your own apps, for example making searchable categories in your e-commerce app or inserting tag clouds, etc.

This example will show you how to implement hashtags in your application using Linkify. We’ll add messages with hashtags to a global list and then when the user clicks on any hashtag, we’ll show all the messages which has the selected hashtag in it.

Note: – Although this tutorial shows how to implement hashtags, it is also very useful to understand how to make any text act as a hyperlink.


Let’s start by creating a new Android application project by the name HashTags with package name com.sourabhsoni.hashtags

New Application Project

Once the project is created successfully, we’ll create the layout of our opening activity. Open activity_main.xml (or your main activity if you have changed the name) and create an EditText (to enter new message), a Button (adds message to the list on click) and a ListView (shows all the messages with clickable hashtags). Copy and paste this code into activity_main.xml:-

Note:- It is a good practise to save all string constants in strings.xml, but for easier understanding in this example, we’ll just write the string instead of reference to string.xml resource.

Activity_main should look something like this:-

MainActivity

Before we go ahead, let’s see what Linkify is. The Linkify class in the SDK lets us specify a regular expression to match, and a scheme to prepend. The scheme is a string which forms a Content URI (with the matched text added to it) to allow data to be looked up. For example, in our case, we want to look for a regular expression match for a hashtag i.e. a word containing letters and digits and underscore which starts with a hash. Linkify can then turn this matched word and the scheme to a URI like com.sourabhsoni.hashtags.tagdetailsactivity/#HashTag which can be used to open a new activity from a content provider. Linkify can be passed any TextView in your application, and will take care of creating the links and enabling their “clickability” for you.

We will create a custom Linkify and pass it to the TextView in our ListView. The regular expression that matches hashtag can be defined as:-

[#]+[A-Za-z0-9-_]+\b

Which effectively says “starting with one or more Hash find at least one either an uppercase letter or a lowercase letter or a digit or underscore until next word boundary (\b)”.

We also need to specify what Linkify needs to do when it finds a hashtag. To do this, we’ll specify a scheme which then be appended with matched hashtag and used as content URI. We define this scheme as

com.sourabhsoni.hashtags.tagdetailsactivity/

Now that we have some knowledge of Linkify and the regular expression to be used for finding a hashtag, let’s write the code behind the activity which adds the message typed in EditText to the list when the user taps on add button. Open MainActivity.java (or your main activity java class if you have given a different name) and write the following code:-

When the user clicks on a HashTag in our app, Linkify converts them to intent links. It calls the VIEW action using the content: URI associated with the link. For Android to match the content URI, we need to define a content provider and an activity registered in the system with an intent-filter that matches both the VIEW action, and the MIME type for the data represented by the content URI. To define the content provider, create a new class called TagProvider.java and paste the following code into it.

We also need to define one more activity which will be called when a user taps on a HashTag. Create a new android layout xml file called as activity_tag_details.xml and paste the following code into it.

Add this activity and the provider class to the manifest and also specify intent filters. Your AndroidManifest.xml should look something like this:-

Now, we know that when a user taps on a HashTag in any of the message, android knows which content provider to use and which activity needs to be called. In the Tag Details activity, we need will get the hashtag appended in the end of our scheme (content://com.sourabhsoni.hashtags.tagdetailsactivity/) and we need to extract that hashtag to use it for finding related messages from the global list in our MainActivity. So here is how we’ll implement the java side of our Tag details activity:-

This completes our code for implementing HashTags, let’s run it and check if it works as expected. It should look something like this:-

MainActivity_screenshot

Tag Details Screenshot

Linkify is a very useful class and can be used for many different purposes. It is heavily used in apps which need to show email addresses or phone numbers or web links. Combined with the power of content providers and regular expressions, Linkify can be used to improve user experience significantly.

Source Code

I have uploaded the entire project on github , you can download it from there and verify your code.