Java – How to get a specific row with a specific symbol in a TextView

How to get a specific row with a specific symbol in a TextView… here is a solution to the problem.

How to get a specific row with a specific symbol in a TextView

I have a textview which has a very long line of text as shown below:

$hello My name is Ford
I’m an electronics engineer, so wait, wait, blah blah

There is something else
So other texts….
$ This is another line like the first line
and so on other plain texts

My question is how can I get only those lines that have the $ symbol on the first line and set some effects only for those specific lines, like bold and red… And not other lines .

.

Any ideas??!
Thanks for your help!

Edit:
Note that my textview text is dynamic and I can’t write code for some specific sentences. I want to write some code for each line as a template, such as starting with $, bold, or something like that.
I also want $ symbole itself; Not showing up in my TextView after this process! Any ideas?

Solution

If you already have text on a String, you can use the split function on the String value and then display your line in html format to enable you to manipulate size and formatting, here is an example:

        String stringValue="$your first line\n $your second line";
        String[] Splited=stringValue.split("$");
        first line is in the Splited[0] and second line is in the Splited[1]
        textview.setText(Html.fromHtml("<h2>Splited[0]</h2><br><p>Spl_j[Z B1]</p>"));;

Update:
Your template might look like this:

 String[] Splited=stringValue.split("\n");
 String msg="";           
    for(int i=0; i<Splited.length; i++){
       if(Splited[i].startsWith("$"))
          msg=msg+"<b>"+Splited[i]+"</b>";
       else
          msg=msg+Splited[i];

}
textview.setText(Html.fromHtml(msg));

Related Problems and Solutions