-1

I am making an app based on json web service .I am facing some problem ,I have a string and I have to split it and then make clickable .Let me clear my problem:

->string which I have something like: "#outfit#fashion#color" or "#outfit" or "#outfit is really awsome."

->Now I want to change the color to blue of the substrings: #outfit,#fashion,#color etc...and rest substring will black.

->Also I want to make it clickable For Example : #outfit will click and fetch all the data related to #outfit tag. Same as the tag concept works.

->I have many types of string and I want logic will work in every condition:

  • "#outfit#fashion#color"
  • "#outfit"
  • "#outfit is really awsome."
  • "check my new #dress ,the brand #new #outfit.Isn't #cool? "

Thanks in advance.Just want to make all tags will be blue colored and clicked.Please describe me with full code as my logic is not so good..

userAndroid
  • 586
  • 2
  • 9
  • 26
  • I have tried simple spliting manner to split the string on the basis of hash ...but I think it will not work in all cases.Also want to blue colored the tags. – userAndroid Nov 26 '14 at 10:14
  • @userAndroid in what case does spilt not work? – KOTIOS Nov 26 '14 at 10:15
  • don't know ..as I told you that I am not good in logic..Simple split will not work split with for loop to check that another # is remaining ornot will work..and I am unable to do that..that's why I am asking – userAndroid Nov 26 '14 at 10:18
  • basically you need to use `.spilt` function on string and make string click like `text.setText(Html.fromHtml("" + "+splitString[0]+ "))` – KOTIOS Nov 26 '14 at 10:22

4 Answers4

2

You can use SpannableString

SpannableString ss = new SpannableString("Hi this is #Naveen. I'll meet #Peter in the evening.. Would you like to join #Sam??");
        ClickableSpan clickableSpanNaveen = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for naveen
            }
        };
        ClickableSpan clickableSpanPeter = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for peter
            }
        };
        ClickableSpan clickableSpanSam = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Do Stuff for sam
            }
        };

        ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
        contentTextView.setText(ss);
        contentTextView.setMovementMethod(LinkMovementMethod.getInstance());

I copy this code from here

But you must find a way how to detect the beginning and the ending of each span.

  • For the beginning you can loop the string and use indexOf("#"), and store those indexes in array for example.
  • For the ending maybe you use the previous array (where you stored indexes of #) and try to find the first space " " after him, using indexOf(" ", // index of "#" //) . (Notice: this logic only work if there is no spaces in all your clickable string)
Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
0

Hi think you can should use ClickableSpan.. let me explain you throug example.. myString ="Click here to read more"

i want to make "here" word clickable (with blue colored)..

    SpannableString ReadMore = new SpannableString(myString);

            ClickableSpan clickReadMore = new ClickableSpan() {
                @Override
                public void onClick(View textView) {

                    //do your stuff here when "here" is clicked
                }
            };

//  below 6 is start index of clickable string and 10 is end index of clickable string
            ReadMore.setSpan(clickReadMore, 6, 10,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            myTextView.setText(ReadMore);
            myTextView.setMovementMethod(LinkMovementMethod
                    .getInstance());

Using above example you split your strings and make them clickable using seperate ClickableSpan

iMDroid
  • 2,108
  • 1
  • 16
  • 29
0

Here is a way you could process the input to prepare it for making hyperlinks from the tags.

import java.util.ArrayList;
import java.util.regex.*;

public class Hashtag {  
    public static void main(String[] args) {
        String input = "#outfit#fashion#color\n"
            + "#outfit\n"
            + "#outfit is really awsome.\n"
            + "check my new #dress, the brand #new #outfit. Isn't it #cool?\n";

        Pattern pattern = Pattern.compile("#([a-zA-Z]+)");
        Matcher matcher = pattern.matcher(input);

        // will store the parsed input string
        ArrayList<Text> parsed = new ArrayList<Text>();

        // populate the ArrayList
        int previousPosition = 0;
        while (matcher.find()) {
            String previousText = input.substring(previousPosition, matcher.start());
            if (!previousText.isEmpty()) {
                parsed.add(new Text(previousText));
            }
            parsed.add(new Tag(matcher.group(1)));
            previousPosition = matcher.end();
        }
        String trailingText = input.substring(previousPosition, input.length());
        if (!trailingText.isEmpty()) {
            parsed.add(new Text(trailingText));
        }

        // the input string 
        for (Text t : parsed) {
            if (t instanceof Tag) {
                Tag tag = (Tag)t;

                /* Here you would do some special handling of the tags to make them clickable
                 * tag.getTag(); can be used to get the contents of tag. For example, the 
                 * for a "#OutFit" tag the getTag() method returns "outfit"
                 */

                System.out.print("[" + tag + "]");
            } else {
                System.out.print(t);
            }
        }
    }

    public static class Text {
        private String text;

        Text(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return text;
        }
    }

    public static class Tag extends Text
    {
        private String tag;
        Tag(String tag) {
            super("#" + tag);
            this.tag = tag.toLowerCase();
        }

        public String getTag() {
            return tag;
        }
    }
}

The above code yields the following output. It puts the tags in square brackets to demonstrate that the extraction worked.

[#outfit][#fashion][#color]
[#outfit]
[#outfit] is really awsome.
check my new [#dress], the brand [#new] [#outfit]. Isn't it [#cool]?

From this point it should be easy to display the text and make the tags clickable.

zkxs
  • 55
  • 1
  • 6
0

After a long struggle I have found wonderful tutorial:

how-to-implment-hashtags-and-callouts-in-android

@Rami's answer is good for a static string but this tutorial is for dynamic data. Hope it will help for someone.

userAndroid
  • 586
  • 2
  • 9
  • 26