3

I have a textview which has a long text lines like below:

$hello all my name is Ford
I'm an electronic engineer and so blah blah blah
and something else here
so other texts....
$and this is another line like the first line
and so other normal texts

my question is how can I get just the lines that have the $ symbol on the first of those and set some effects to just those specific lines like Bold and red color.. not other lines...

Any Idea??!
Thanks for your helps!

Edit:
note that my textview texts are dynamic and I cant write code for some specific sentences. I want to write some code as template to every line that for example starts with $ , be bold or something like that.
and also I want to the $ symbole itself; doesn't show in my text view after this proccess! any idea for this?

  • 1
    try using Spannable String : http://stackoverflow.com/questions/26649344/android-use-bold-character-for-part-of-text – Arash GM Feb 09 '15 at 10:59
  • 1
    http://stackoverflow.com/questions/26923782/how-to-make-a-part-of-text-bold-from-arraylist-in-android – Arash GM Feb 09 '15 at 11:00

2 Answers2

2

If you already have the text on String you could use split function on String value and then display your line with html format that make you able to manipulate the size and format here is a sample:

        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 could be like below:

 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));
amin
  • 46
  • 5
  • my textview texts are dynamic and I cant write code for some specific sentences. I want to write some code as template to every line that for example starts with $ , be bold or something like that. any idea for this? – mohammadf255 Feb 09 '15 at 11:44
  • I've test this code and it worked and I really apreciate your help. Thanks a lot :-* – mohammadf255 Feb 09 '15 at 19:31
2

you can also do it like this, using search and replace

String yourValue="$your first line\n $your second line";
String regex = "(\$.*)\n";
String formattedValue = yourValue.replaceAll(regex, "<b>$1</b>\n");
textview.setText(Html.fromHtml(formattedValue));
dlohani
  • 2,511
  • 16
  • 21
  • my textview texts are dynamic and I cant write code for some specific sentences. I want to write some code as template to every line that for example starts with $ , be bold or something like that. any idea for this? – mohammadf255 Feb 09 '15 at 11:43
  • 1
    create a function with these lines `public String formatString(String yourValue){ String regex = "(\$.*)\n"; String formattedValue = yourValue.replaceAll(regex, "$0\n"); return formattedValue; }` each time before setting value to your text view, pass your string to this function hope this helps :) – dlohani Feb 09 '15 at 11:55
  • this is my code: `Intent myIntent = getIntent(); if (myIntent.hasExtra("myExtra")){ TextView mText = (TextView)findViewById(R.id.textView1); mText.setText(myIntent.getStringExtra("myExtra")); } ` these are wrote in my acitivity that contains the textView and this textView is getting its text from another activity with getExtra. my Question is just about this situation.. because I think your suggested code doesnt work because I already set text for this text view. I'll appreciate any suggestion. Thanks.. – mohammadf255 Feb 09 '15 at 15:48
  • Modify your code like this, and include above function your activity `Intent myIntent = getIntent(); if (myIntent.hasExtra("myExtra")){ TextView mText = (TextView)findViewById(R.id.textView1); mText.setText(formatString(myIntent.getStringExtra("myExtra"))); }` – dlohani Feb 10 '15 at 06:54