Search code examples
androidtextviewstringbuilderexpandable

Perform StringBuilder setText method and ExpendableTextView setText method with the same holder


Based on the answer given on this post,

Android - Expandable TextView with Animation

I want to be able to use my function `

         holder.expandableTextView.setText(wallContent);

            SpannableStringBuilder ssb = SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged);
            holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
            holder.expandableTextView.setText(ssb);

`

This is SharefolioSpannableHandler class `

public class SharefolioSpannableHandler {

    public static SpannableStringBuilder getSpannableStringBuilder(final Context context, String text, List<Account> persons, List<Company> companies) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);

        if (persons.size() != 0) {
            for (int i = 0; i < persons.size(); i++) {
                final Account person = persons.get(i);
                final String personName = persons.get(i).firstname;

                if (text.contains(personName)) {
                    int start = text.indexOf(personName) - 1;
                    int end = start + personName.length() + 1;
                    ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ClickableSpan() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                             Intent intent = new Intent(context, LoadUserWallActivity.class);
                             intent.putExtra(LoadUserWallActivity.TAG, person);
                             context.startActivity(intent);
                        }
                    }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        } 

        if (companies.size() != 0) {
            for (int i = 0 ; i < companies.size() ; i++) {
                final Company company = companies.get(i);
                final String companyCode = companies.get(i).companyCode;

                if (text.contains(companyCode)) {

                    int start = text.indexOf(companyCode) - 1;
                    int end = start + companyCode.length() + 1;
                    ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ClickableSpan() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, CompanyActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable("company", company);
                            intent.putExtras(b);
                            context.startActivity(intent);
                        }
                    }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return ssb;
    }

}
`

I am currently having some issues as the ExpendableTextView which is a holder already has a setText but how to include the StringBuilder function is giving me problems. If i run as shown above, while it does create the Expandable TextView, it doesnt peform what should be done by the StringBuilder in terms of links or tags.

Does anyone have any clue on how to join both functions together?

Thanks Kingsley


Solution

  • I found out my issue. I just needed to return the output of my StringBuilder in my ExpendableTextView so instead of this

    holder.expandableTextView.setText(wallContent);
    
                SpannableStringBuilder ssb = SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged);
                holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
                holder.expandableTextView.setText(ssb);
    

    I did this

    holder.expandableTextView.setText(wallContent);         
    holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
    holder.expandableTextView.setText(SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged));
    

    Worked perfectly for me.