0

I have text that comes from SQLite database to my onBindViewHolder in this form: Hello I am {Alex}. I want to be able to adjust size or colour of the text inside {} and hide those {} curly braces from output. So final desire to have text like this: Hello I am Alex

For the first time I meet with Regex related thing, can anybody give me short step by step guide how to accomplish this.

I found similar question:

Regular Expression to find a string included between two characters while EXCLUDING the delimiters

But I do not understand how should I handle "(?<=[)(.*?)(?=])" in my case.

Now my onBindViewHolder looks like this:

public void onBindViewHolder(final MyViewHolder holder, final int position) {
    final Question question = questionList.get(position);
    holder.tvQuestion.setText(question.getQuestion());
//  holder.tvQuestion.setTextColor(Color.parseColor("#ff0099cc"));

enter image description here

Alex K
  • 81
  • 9

1 Answers1

1

I am not good with regular expression. But here is an answer to your question that will let you get the desired result (get the expression text and format it accordingly).

public CharSequence getFormattedQuestion(Context context, String originalQues, @ColorRes int colorToSet, @DimenRes int textSize) {

    // First we check if the question has the expression
    if (originalQues == null || !originalQues.contains("{") || !originalQues.contains("}")) {
        return originalQues;
    }

    // Then we break the original text into parts

    int startIndex = originalQues.indexOf("{");
    int endIndex = originalQues.indexOf("}");

    // 1) The text before the expression
    String leftPart = startIndex>0 ? originalQues.substring(0, startIndex) : "";

    // 2) The text after the expression (if there is any)
    String rightPart = endIndex == originalQues.length()-1 ? "" : originalQues.substring(endIndex+1);

    // 3) The expression text
    String midPart = originalQues.substring(startIndex+1, endIndex);

    // 4) Format the mid part with the give color (colorToSet) and size (textSize)
    SpannableString spannableMid = new SpannableString(midPart);
    spannableMid.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, colorToSet)), 0, midPart.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableMid.setSpan(new AbsoluteSizeSpan(context.getResources().getDimensionPixelSize(textSize)), 0, midPart.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


    // check if there is any left part; if so, add it with mid
    CharSequence leftAndMid = leftPart.length()>0 ? TextUtils.concat(leftPart, " ", spannableMid) : spannableMid;

    // Check if there is any right part else return the left and mid
    return rightPart.length()>0 ? TextUtils.concat(leftAndMid, " ", rightPart) : leftAndMid;
}

So basically we are Splitting the original question into 3 parts. Part one, the text before expression. Part 2 the expression text. Part 3 the text after the expression. Then we format the expression text(Part2) with color and size using SpannableString. Then we return a new text by combining all three.
You can then use it like this

CharSequence ques = getFormattedQuestion(holder.itemView.getContext(), question.getQuestion(), R.color.blue, R.dimen.text_size);
holder.tvQuestion.setText(ques);
Siddharth jain
  • 447
  • 1
  • 5
  • 13
  • @AlexK My bad. The issue was with the `leftPart`. I have updated the code. Please check now. – Siddharth jain Oct 07 '18 at 13:00
  • sidhnarth jain, Thank you very much. It makes effect. Now text in recycle view looks like this "Alex}.Alex". Something wrong with part before the "{" it doesn't show anymore. I need to understand your code better. I will write update later. – Alex K Oct 07 '18 at 13:01
  • @AlexK did you try the updated method? Still not working? – Siddharth jain Oct 07 '18 at 13:03
  • @AlexK I have added some explanation. Now you will be able to understand it better. There are comments that describe each step. – Siddharth jain Oct 07 '18 at 13:09
  • Great Man! It is working. As beginner now I am trying to understand the part where you work with the string. Those -1 and +1 confusing me. – Alex K Oct 07 '18 at 13:25
  • Haha! Good, try to clear that by yourself that will help you in long run. If still have any doubts, let me know. – Siddharth jain Oct 07 '18 at 13:55