Coder Social home page Coder Social logo

hashtaghelper's Introduction

HashTagHelper

This is a library designed for highlighting hashtags ("#example") and catching click on them.

Android_weekly Android Arsenal

#Usage Add this snippet to your project build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
}

Add this snippet to your module build.gradle file:

dependencies {
    compile 'com.github.danylovolokh:hashtag-helper:1.1.0'
}

Getting click events and highligthing hashtags.

private HashTagHelper mTextHashTagHelper;

private TextView mHashTagText;

@Override
protected void onCreate(Bundle savedInstanceState) {

  mHashTagText = (TextView) findViewById(R.id.text);
  mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), new HashTagHelper.OnHashTagClickListener() {
    @Override
    public void onHashTagClicked(String hashTag) {
        
    }
  });
        
  // pass a TextView or any descendant of it (incliding EditText) here.
  // Hash tags that are in the text will be hightlighed with a color passed to HasTagHelper
  mTextHashTagHelper.handle(mHashTagText);
}

Highlighting without click events. Just set "null" instead of HashTagHelper.OnHashTagClickListener;

mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), null);
mTextHashTagHelper.handle(mHashTagText);

By default only digits and letters are valid symbols for hashtag To add support of custom symbols like '_' or '-' you need to specify another parameter when creating HashTagHelper

char[] additionalSymbols = new char[]{
                '_',
                '$'
        };
// If you set additional symbols not only letters and digits will be a valid symbols for hashtag
// Example: "hash_tag_with_underscore_and$dolar$sign$is$also$valid_hashtag"
mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), null, additionalSymbols);
mTextHashTagHelper.handle(mHashTagText);

To get all hashtags from text.

// get all hashtags with "#", example: "#blueeyes"
List<String> allHashTags = mTextHashTagHelper.getAllHashTags(true);

// get all hashtags without "#", example: "blueeyes"
List<String> allHashTags = mTextHashTagHelper.getAllHashTags(false);

#Demo alt tag alt tag

License

Copyright 2015 Danylo Volokh

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

hashtaghelper's People

Contributors

danylovolokh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hashtaghelper's Issues

AdditionalSymbols not working

AdditionalSymbols are never detected as hastag, due to this line of code:
HashTagHelper.java, lie 136:
if(sign == '#'){
startIndexOfNextHashSign = index;

            nextNotLetterDigitCharIndex = findNextValidHashTagChar(text, startIndexOfNextHashSign);

            setColorForHashTagToTheEnd(startIndexOfNextHashSign, nextNotLetterDigitCharIndex);
        }

It only compares with '#'

Not able to include the library in the Android Studio using Gradle

Hi,

I am getting the following error while trying to include it in the project on Android Studio.


Error:Could not GET 'https://jitpack.io/com/github/danylo2006/hashtag-helper/1.1.0/hashtag-helper-1.1.0.pom'. Received status code 401 from server: Repo not found or no access token provided
<a href="toggle.offline.mode">Enable Gradle 'offline mode' and sync project</a>

Regards
Shajeel

How to have the same functionality with @ mentions

I am thinking about using this library in my app. But I also require the library to work with @ mentions . Please tell me where to make changes to achieve functionalities with the @ mentions. Please reply.

Application crashing on onlong press

hello @danylovolokh,
whenever I long press on any hashtag, my application is getting crashed, with the error
Attempt to invoke virtual method 'int android.widget.Editor$SelectionModifierCursorController.getMinTouchOffset()' on a null object reference

Hashtag can not use with autolink

Hi,
Firstly thanks for this perfect library.
My problem is , I can't use hashtag with autolink. So i need to use autolink in textview. f.e www.google.com, http://migros.com.tr.
If I use HashTagHelper.Creator.create(getResources().getColor(R.color.blue), this, null); with autolink. AutoLink doesnt work. How can i solve this problem, please help me

#first_second is not working

Hello Danylo,
Thank you for great effort. I face an issue that if I wrote hashtag of 2 words like this" #first_second, first word only is highlighted . please advise.

Thank you,
Hisham

App crashing in the RecyclerView Adapter

Hi Danylo!

I am showing hash tags in the RecyclerView items and onBindViewHolder method is simple and looks like this:

@Override
    public void onBindViewHolder(TaskViewHolder holder, int position) {

        holder.mTaskTitleTv.setText(mList.get(position).getTitle());

        // pass a TextView or any descendant of it (incliding EditText) here.
        // Hash tags that are in the text will be hightlighed with a color passed to HasTagHelper
        mTextHashTagHelper.handle(holder.mTaskTitleTv);
    }

The app crashes when i reach the bottom of the List and scroll up again.

Complete crash report is:

java.lang.IndexOutOfBoundsException: setSpan (0 ... 9) ends beyond length 5
                                                                                     at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
                                                                                     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
                                                                                     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
                                                                                     at com.volokh.danylo.hashtaghelper.HashTagHelper.setColorForHashTagToTheEnd(HashTagHelper.java:155)
                                                                                     at com.volokh.danylo.hashtaghelper.HashTagHelper.setColorsToAllHashTags(HashTagHelper.java:115)
                                                                                     at com.volokh.danylo.hashtaghelper.HashTagHelper.eraseAndColorizeAllText(HashTagHelper.java:99)
                                                                                     at com.volokh.danylo.hashtaghelper.HashTagHelper.access$100(HashTagHelper.java:27)
                                                                                     at com.volokh.danylo.hashtaghelper.HashTagHelper$1.onTextChanged(HashTagHelper.java:56)
                                                                                     at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
                                                                                     at android.widget.TextView.setText(TextView.java:3816)
                                                                                     at android.widget.TextView.setText(TextView.java:3671)
                                                                                     at android.widget.TextView.setText(TextView.java:3646)
                                                                                     at com.shajeelafzal.quicktasks_app.adapter.TasksAdapter.onBindViewHolder(TasksAdapter.java:72)
                                                                                     at com.shajeelafzal.quicktasks_app.adapter.TasksAdapter.onBindViewHolder(TasksAdapter.java:25)
                                                                                     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
                                                                                     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
                                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
                                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                                                     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                                     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                                     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                                     at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
                                                                                     at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
                                                                                     at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3807)
                                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                                     at android.view.Choreographer.doFrame(Choreographer.java:543)
                                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
01-05 01:31:44.324 14479-14550/com.shajeelafzal.quicktasks_app I/Fabric: Crashlytics report upload complete: 568AD6AD00EE-0001-388F-1CE3EAB0099E.cls

onHashTagClicked not working correctly

I am showing Hash Tags in a TextView in the RecyclerAdaper.

Problem is that the Click Listener on the adapter Item is not working properly when i am using HashTagHelper library. If the user clicks on TextView's text that is not a Hash Tag then the onClick of the Item is not being called.

Inside onBindView method i am showing the Hash Tag on the TextView:

@Override
    public void onBindViewHolder(TaskViewHolder holder, int position) {
        ...
        holder.mTaskTitleTv.setText(mTasksList.get(position).getTitle());
        holder.mTextHashTagHelper.handle(holder.mTaskTitleTv);
    }

And ViewHolder looks like this:

class TaskViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

        ...
        public TaskViewHolder(View itemView) {
            super(itemView);
            findViews(itemView);
            itemView.setOnClickListener(this);
            mTextHashTagHelper = HashTagHelper.Creator.create(mContext.getResources().getColor(R.color.primary), new HashTagHelper.OnHashTagClickListener() {
                @Override
                public void onHashTagClicked(String hashTag) {
                    mContext.startActivity(HashTagTaskActivity.createLauncherIntent(mContext, hashTag));
                }
            });
        }

       @Override
        public void onClick(View v) {
            mListner.onItemClick(v, getLayoutPosition());
        }
}

Different ClickListener for plain text and HashTagText

I have a RecyclerView and I need handle different click listener in row: one for row view and one for HashTag text.

I noticed that if I put an Hashtag click listener, row click listener is not consumed (if clicked on plain text).

So, the first fix is set row click listener to TextView: now, if I click on HashTag both listener are consumed (hashtag click listener before than view On click listener).

In order to avoid this behavior I implemented this listener:


public class FooClickListener implements View.OnClickListener,HashTagHelper.OnHashTagClickListener {
    FooAdapter.FooListener listener;
    FooModel model;
    private boolean consumedHashTagClick = false;

    public FooClickListener(FooAdapter.FooListener listener, FooModel model) {
        this.listener = listener;
        this.model = model;
    }

    @Override
    public void onClick(View view) {
        if(!consumedHashTagClick) listener.onRowClick(model.text);

        consumedHashTagClick = false;
    }

    @Override
    public void onHashTagClicked(String hashTag) {
        consumedHashTagClick = true;
        listener.onHashtagClick(hashTag);
    }
}

Now I can be sure of what listener will be consumed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.