1

For Eg :

Follow @SoccerBible, RT this & if Ronaldo scores https://bit.ly/133/com tonight we'll give away a pair of Nike Clash Vapor 8's to one lucky winner! #RonaldoComp

In my listview component. Where i am showing list of tweets. I have colored the @,# and urls in the tweet text. The all are dynamically clickable.

My question is how can i make the non-colored text as clickable. I have used Linkify.addLinks and Pattern API to make them clickable. But i wanted the non @,# and url text to be clicakble without adding any color to it.

Thanks, dominic

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
dominic
  • 308
  • 2
  • 6
  • 17
  • Write a click event of the TextView as well to do that, – MKJParekh Jun 22 '12 at 06:54
  • There is no prob in writing a click event for the textView.Then that comes to the point all the textViews will be redirected to the same event. This has to be dyanamic. – dominic Jun 22 '12 at 07:41
  • then in onClick..get id as `view.getId()` and write switch..case for each id. to redirect to differnt screen – MKJParekh Jun 22 '12 at 08:27
  • There is only one textView for all the tweets. getView repeats them for the list. I wanna trap the onItemClick event for the listview. That does not work coz i have implemented the Linkify and Pattern as i said above. – dominic Jun 22 '12 at 08:47
  • I have also done the same in one of my app, as there was a requirement to show same tweet list with linkify..you just check [this link](http://www.orangeapple.org/post/14809024490/android-custom-hyperlinked-textview) and I am sure your problems will be solved. – MKJParekh Jun 22 '12 at 08:59
  • Hey Thanks, this works fine. Still where can the non highlighted text or non colored event be trapped? – dominic Jun 22 '12 at 09:17

1 Answers1

5

For twitter app, I had also the same requirement to click on handles of tweets and also click on the text of tweet to get Tweet detail.

For that I have used this LinkEnabledTextView and that become easy for me to do the task.

I just added the class in my project and then in List Item instead simple TextView I used object/instance of the LinkEnabledTextView.

There is a complete demo in above link, Please check that.

EDIT: Adapter Code Specially check onTextLinkClick() and then holder.twtdata.setOnClickListener()

    private static class EfficientAdapter extends BaseAdapter implements TextLinkClickListener {
        private LayoutInflater mInflater;
        static int pos =0;
        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return timelines.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listtimerow, null);
                holder = new ViewHolder();
                holder.twtdata = (LinkEnabledTextView) convertView
                        .findViewById(R.id.twtdata);
                holder.twtnm = (TextView) convertView
                        .findViewById(R.id.twthandle);
                holder.twtimg = (ImageView) convertView
                        .findViewById(R.id.avatar);
                holder.twtdt = (TextView) convertView
                        .findViewById(R.id.created);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
               holder.twtdata.setOnTextLinkClickListener(this);
                holder.twtdata.gatherLinksForText(timelines[position]);
                holder.twtdata.setTextColor(Color.BLACK);
                holder.twtdata.setLinkTextColor(Color.BLUE);

                MovementMethod m = holder.twtdata.getMovementMethod();
                if ((m == null) || !(m instanceof LinkMovementMethod)) {
                    if (holder.twtdata.getLinksClickable()) {
                        holder.twtdata.setMovementMethod(LinkMovementMethod.getInstance());
                    }
                }

            if (bmpimg1[position] != null)
                holder.twtimg.setImageBitmap(bmpimg1[position]);
            holder.twtnm.setText(twitterhandles[position]);
            Date credate = new Date(created[position]);

            String dt = credate.getDate() + " "
                    + getMonthName(credate.getMonth());
            holder.twtdt.setText(dt);

            holder.twtdata.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    pos = position;
                    if(linkselected==true)
                        return;
                    childSelected = true;

                    Log.i("mention data", timelines[position]);
                    Intent textintent = new Intent(ctx, TimelineRe.class);
                    textintent.putExtra("userid", userid[position]);
                    textintent.putExtra("nm", twitterhandles[position]);
                    textintent.putExtra("msg", timelines[position]);
                    textintent.putExtra("pos", position);
                    textintent.putExtra("frm", "t");
                    textintent.putExtra("img", bmpimg1[position]);

                    if (urlentities[position] != null
                            && dpurlentities[position] != null) {
                    textintent.putExtra("urlentity", urlentities[position]);
                    textintent.putExtra("dpurlentity", dpurlentities[position]);
                    }
                    textintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    ctx.startActivity(textintent);
                }
            });
            holder.twtnm.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    childSelected = true;
                    Intent iconintent = new Intent(ctx, TweetRe.class);
                    iconintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    iconintent.putExtra("userid", userid[position]);
                    iconintent.putExtra("pos", position);
                    iconintent.putExtra("frm", "t");
                    iconintent.putExtra("img", bmpimg1[position]);
                    ctx.startActivity(iconintent);
                }
            });
            holder.twtimg.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    childSelected = true;
                    Intent iconintent = new Intent(ctx, TweetRe.class);
                    iconintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    iconintent.putExtra("userid", userid[position]);
                    iconintent.putExtra("pos", position);
                    iconintent.putExtra("frm", "t");
                    iconintent.putExtra("img", bmpimg1[position]);
                    ctx.startActivity(iconintent);
                }
            });
            return convertView;

        }
        @Override 
        public int getItemViewType(int position) { 
            return position; 
        } 

        @Override 
        public int getViewTypeCount() { 
            return timelines.length;
        } 
        static class ViewHolder {

            TextView twtdt;
            LinkEnabledTextView twtdata;
            ImageView twtimg;
            TextView twtnm;
        }

        public void onTextLinkClick(View textView, String clickedString)
        {
            if (isWiFiConnected == false) {
                Toast.makeText(ctx,
                        "No Internet Connection \nPlease Check and Retry",
                        Toast.LENGTH_SHORT).show();

                return;
            }
            android.util.Log.v("Hyperlink clicked is :: " + clickedString, "Hyperlink clicked is :: " + clickedString);
            if(clickedString.charAt(0)=='#')
            {
                linkselected=true;
                childSelected=true;
                Intent reintent = new Intent(ctx,
                        Search.class);
                reintent.putExtra("frm", "l");
                reintent.putExtra("keyword",clickedString.substring(1, clickedString.length()) );
                reintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ctx.startActivity(reintent);
            }
            else if(clickedString.charAt(0)=='@')
            {
                linkselected=true;
                childSelected=true;
                Intent iconintent = new Intent(ctx, TweetRe.class);
                iconintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                iconintent.putExtra("snm", clickedString.substring(1,clickedString.length()));
                iconintent.putExtra("frm", "l");
//              iconintent.putExtra("userid", userid[pos]);
//              iconintent.putExtra("pos", pos);

//              iconintent.putExtra("img", bmpimg2[pos]);
                ctx.startActivity(iconintent);
            }
            else if(clickedString.charAt(0)=='h')
            {
                linkselected=true;
                childSelected=true;
                Intent iconintent = new Intent(ctx, ShowLink.class);
                iconintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                iconintent.putExtra("url","http://www."+dpurlentities[pos]);
                ctx.startActivity(iconintent);

            }
        }
    }
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Hi, I resolved the code it runs but gives a error on the line. list.setAdapter(adapter); Ive created the object of the adapter class as EfficientAdapter adapter = new EfficientAdapter(getBaseContext()); on the onCreate() method. – dominic Jun 22 '12 at 10:24
  • http://stackoverflow.com/questions/11154701/android-listview-with-twitter-tweets-with-dynamic-click-events – dominic Jun 22 '12 at 10:46
  • now check, Ive added them, the data is printed in the log. But while on the line setAdapter it says there is an error. – dominic Jun 22 '12 at 11:00
  • holder.tweet_text = (LinkEnabledTextView) convertView .findViewById(R.id.tweet_text); It shows an error on this line particular. – dominic Jun 22 '12 at 12:27
  • did your changed in XML from TextView to LinkEnabledTextView for the tweet_text , you need to replace TextView with LinkEnabledTextView in XML Layout file also. – MKJParekh Jun 22 '12 at 12:28
  • ohhh finally it ran. This is how it looks http://img85.imageshack.us/img85/4871/captureoe.jpg – dominic Jun 22 '12 at 12:38
  • I must tell you..try to change yourself as rather more cool and calm person, then this kind of problems you will be able to solve yourself more easily..you seem to do hurry much more,and if any query ping me in this room http://chat.stackoverflow.com/rooms/1531/casual-chat – MKJParekh Jun 22 '12 at 12:42
  • dude ive already set the color white. It showed up the same that way. Cant chat dont have enuf points. – dominic Jun 22 '12 at 12:56
  • @MKJParekh: please I have a similar question. http://stackoverflow.com/questions/36900437/what-parameter-should-i-pass-check-gatherlinksfortext. Please, can you take a look at it? – X09 Apr 28 '16 at 21:57