-3

I am getting multiple paragraph from server in the form of json object like

MY name is <(>Mohit Kumar<)>. <(>Sachin<)> is my role model. I am <(>12<)> year old. Currently i am in <(>delhi<)> but my hometown is <(>bangalore<)>.

Now I want to remove start tag <(> and end tag <)> and change color of text which is inside those tags in android studio.

How can I achieve this?

Charuක
  • 12,953
  • 5
  • 50
  • 88
gaurav
  • 39
  • 9
  • You should provide the code you have tried and organize better your question, then we can help you in a better way. – Alan Godoi Jan 19 '17 at 03:38
  • @Sagar Aghara when you edit a question please concern about the content before you make suggestions.You tried to remove his requirement – Charuක Jan 19 '17 at 03:49
  • Welcome to StackOverflow! Please read the user guidelines on how to ask a good question before posting a question (http://stackoverflow.com/help/how-to-ask) Thank You – Salman Khakwani Jan 19 '17 at 09:48

3 Answers3

0

Assuming that you get a string like you mentioned,

If you want to color a text you can use <font color='#EE0000'>TextYouWantToColor</font> so you need to replace <(> with <font color='#EE0000'> and <)> with </font>

Example

public class YourActivity extends AppCompatActivity  {

  private  String yourString ;
  private  String yourNewString ;
  private  TextView tv ;
  private  String colorCodeStart = "<font color='#EE0000'>";  // use any color as  your want
  private  String colorCodeEnd = "</font>";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.tv);

        yourString = "MY name is <(>Mohit Kumar<)>. <(>Sachin<)> is my role model. I am <(>12<)> year old. Currently i am in <(>delhi<)> but my hometown is <(>bangalore<)>.";
        yourNewString =  yourString.replace("<(>",colorCodeStart); // <(> and <)> are different replace them with your color code String, First one with start tag
        yourNewString=  yourNewString.replace("<)>",colorCodeEnd); // then end tag
        Log.d("CheckNew",yourNewString);
        tv.setText((Html.fromHtml(yourNewString))); 

    }


}

Note :Html.fromHtml() is deprecated, what is the alternative? Check here

enter image description here

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
0

Replace tags with blank character, like

String s = new String("<(>Change me<)>");
        s=s.replace("<(>","");
        s=s.replace("<)>","");

then, to change TextColor of specific text, use SpannableString

SpannableString string = new SpannableString("Your string");
        string.setSpan(new StyleSpan(Typeface.BOLD), 1, 4, 0);
        string.setSpan(new ForegroundColorSpan(Color.GREEN), 5,string.length(), 0);
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
-1

Assuming you are using a TextView, use this to change the color:

myTextView.setTextColor(Color.RED);
Alan Godoi
  • 657
  • 1
  • 12
  • 39