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..!!
            Asked
            
        
        
            Active
            
        
            Viewed 2,075 times
        
    1
            
            
        - 
                    Why don't you make your arraylist hold objects that contain the text you hold as a string, and then whether or not they are bold as a boolean? – DreadHeadedDeveloper Nov 14 '14 at 05:55
- 
                    while creating a view in your adapter you can make a check on the value of array list item if the condition passes then make your text bold accordingly. – Key_coder Nov 14 '14 at 05:55
- 
                    or better yet, if(text.equals(me)){//make bold} – DreadHeadedDeveloper Nov 14 '14 at 05:56
- 
                    @DreadHeadedDeveloper I dont want to set complete text bold. I want a fixed part like in Me: Hello, only Me: should be bold. – Ruchir Nov 14 '14 at 05:57
3 Answers
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
- 
                    
- 
                    @user3519241 chech this link http://stackoverflow.com/questions/2116162/how-to-display-html-in-textview – duggu Nov 14 '14 at 06:06
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
