1

In my android app, I am retrieving data from mysql database, storing them in arraylist and displaying them in a listview. Now I want to make a perticular String bold, i.e. if in my arraylist, there's data something like, "Me: hello.." then in listview it should display as " Me: hello.. " . Here "Me:" will be first string in list but don't know in which row. I am not getting idea how to do this. Please someone help me..!!

Ruchir
  • 1,086
  • 4
  • 24
  • 48

3 Answers3

4

Try below code:

if(array.get(i).contain("Me")){
    String value = array.get(i).split(":")[0];
    String value1 = array.get(i).split(":")[1];
    String main_value = "<b>"+value +":</b> "+ value1;

    txt.setText( Html.fromHtml(main_value));
}
duggu
  • 37,851
  • 12
  • 116
  • 113
1

Try to use custom funcation to make section of text to be bold :

Example :

getTextWithBoldSection("Me: hello..","Me:");

public SpannableStringBuilder getTextWithBoldSection(String fulltext, String boldText){

        SpannableStringBuilder builder=new SpannableStringBuilder();

        if(boldText.length() > 0 && !boldText.trim().equals("")){
            int startingIndex = fulltext.indexOf(boldText);
            int endingIndex = startingIndex + boldText.length();
         
            if(startingIndex < 0 || endingIndex <0){
                return builder.append(fulltext);
            }
            else if(startingIndex >= 0 && endingIndex >=0){
                builder.append(fulltext);
                builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0);
            }
        }else{
            return builder.append(fulltext);
        }

        return builder;
    }
Community
  • 1
  • 1
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
1

Try using under mentioned code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView mTV = (TextView) findViewById(R.id.tv);
    String mString = "Me: hello..";
    mTV.setText(boldText(mString,0,2));
}
private Spannable boldText(String mText, int mStart, int mEnd) {

    Spannable WordtoSpan = new SpannableString(mText);
    WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), mStart, mEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return WordtoSpan;
}
}
Vibhor Chopra
  • 647
  • 4
  • 14