Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get special character with word and its click event

i have a 3 String like this:

"@Username: Deliverd your order",
"YOU got trophy: KING OF COINS",
"There is a package waiting for you to pick up from #surat to #mumbai",

what i wanted to do is get username and city name in different color with its click event.

what i m able to achive is get username by splitting to ":" character. but i dont know how to get city name and click event of both.

In city name only last city color is changing, how to change both city name color and get its click event.

this is what i tried:

if (notifications.getTitle().contains(":")) 
{
    String[] username = notifications.getTitle().split(":");
    String uname = getColoredSpanned(username[0] + ":", "#ff7505");
    String txt = getColoredSpanned(username[1], "#000000");
    holder.txtTitle.append(Html.fromHtml(uname +" " + txt));
    holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance());
} 
else if (notifications.getTitle().contains("#"))
{
     Matcher matcher = 
            Pattern.compile("#\\s(\\w+)").matcher(notifications.getTitle());
     i=0;
     while (matcher.find())
     {
           place.add(i, matcher.group(1));
           i++;
     }
     String place1 = getColoredSpanned("#" + place.get(0), "#237BCD");
     String place2 = getColoredSpanned("#" + place.get(1), "#237BCD");
     places1 = notifications.getTitle().replace("#" + place.get(0), place1);
     places1 = notifications.getTitle().replace("#" + place.get(1), place2);
     holder.txtTitle.setText(Html.fromHtml(places1));
}
else
{
    holder.txtTitle.setText(notifications.getTitle());
}

private String getColoredSpanned(String text, String color) {
    String input = "<font color=" + color + ">" + text + "</font>";
    return input;
}

and this is what i get as output:

enter image description here

and this is what i really expected:

enter image description here

like image 237
Sagar Chavada Avatar asked Sep 05 '17 09:09

Sagar Chavada


3 Answers

I think you have done with username and facing problem with click on city so I have given answer with click on city name.

Thanks to @Vinay for given some hint.

Please check below code.

public void setSpan() {
        String test = "There is a package waiting for you to pick up from #surat to #mumbai";
        SpannableString spannable = new SpannableString(test);
        final Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(test);
        while (matcher.find()) {
            final String city = matcher.group(1);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View textView) {
                    Toast.makeText(mActivity, city, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                    ds.setColor(Color.RED);
                }
            };
            int cityIndex = test.indexOf(city) - 1;
            spannable.setSpan(clickableSpan, cityIndex, cityIndex + city.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        mTextViewNotification.setText(spannable);
        mTextViewNotification.setMovementMethod(LinkMovementMethod.getInstance());
    }

Output Screenshot:

enter image description here

like image 66
Niranj Patel Avatar answered Oct 24 '22 04:10

Niranj Patel


use Regex for this.

String str= "There is a package waiting for you to pick up from #surat to #mumbai";

Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(str);
while (matcher.find()) {
  System.out.println(matcher.group(1));
}

output will be:

surat 
mumbai
like image 4
vinay6kr Avatar answered Oct 24 '22 05:10

vinay6kr


To extract the final description with clickable hashtags, add a hidden LinearLayout in your list item layout:

<LinearLayout
        android:id="@+id/layoutDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:orientation="horizontal"
        android:visibility="gone" />

Modify your code to separate the hashtags with dynamic TextViews and add them back in the LinearLayout:

if (notifications.getTitle().contains(":")) {
        String[] username = notifications.getTitle().split(":");
        String pre_username = getColoredSpanned(username[0] + ":", "#ff7505");
        String post_username = getColoredSpanned(username[1], "#000000");
        holder.txtTitle.append(Html.fromHtml(pre_username + " " + post_username));
        holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance());
    }
    else if (notifications.getTitle().contains("#")) {
        layoutDescription.setVisibility(View.VISIBLE);

        Matcher matcher = Pattern.compile("#\\s(\\w+)").matcher(notifications.getTitle());

        List<String> place = new ArrayList<>();
        int i = 0;
        while (matcher.find()) {
            place.add(i, matcher.group(1));
            i++;
        }

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5, 0, 5, 0); // (left, top, right, bottom)

        TextView mHashTagA = new TextView(this);
        mHashTagA.setLayoutParams(layoutParams);
        mHashTagA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Clicked on HashTag A", Toast.LENGTH_SHORT).show();
            }
        });

        TextView mSeparator = new TextView(this);
        mSeparator.setLayoutParams(layoutParams);
        mSeparator.setText("to");

        TextView mHashTagB = new TextView(this);
        mHashTagB.setLayoutParams(layoutParams);
        mHashTagB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Clicked on HashTag B", Toast.LENGTH_SHORT).show();
            }
        });

        TextView mDescription = new TextView(getApplicationContext());
        mDescription.setTextColor(Color.parseColor("#343434"));
        mDescription.setLayoutParams(layoutParams);

        String place1 = getColoredSpanned("#" + place.get(0), "#237BCD");
        mHashTagA.setText(Html.fromHtml(place1));

        String place2 = getColoredSpanned("#" + place.get(1), "#237BCD");
        mHashTagB.setText(Html.fromHtml(place2));

        String without_hash = notifications.getTitle().split("#")[0];
        mDescription.setText(without_hash);

        layoutDescription.addView(mDescription);
        layoutDescription.addView(mHashTagA);
        layoutDescription.addView(mSeparator);
        layoutDescription.addView(mHashTagB);
    } else {
        layoutDescription.setVisibility(View.GONE);
        holder.txtTitle.setText(notifications.getTitle());
    }

The final output,

enter image description here

like image 4
Prokash Sarkar Avatar answered Oct 24 '22 05:10

Prokash Sarkar