1

Very new to coding, just a few weeks learning after work here and there. I'm trying to build an app and am making progress but hit a wall. I want just two words to be bold in my child item. Any help? This is in my java file with details for the expandable list.

 Appliances.add("The first word of each paragraph\nis what I would like to be bold faced");

3 Answers3

0
final SpannableStringBuilder sb = new SpannableStringBuilder("The first word of each paragraph, what I would like to be bold faced.");
// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); 
// make all characters of the second and sixth word Bold 
sb.setSpan(bss, 4, 9, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make word 'first' bold
sb.setSpan(bss, 23, 32, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make word 'paragraph' bold

sb should produce the string - The first word of each paragraph, what I would like to be bold faced.

Then use the string as you want:

Appliances.add(sb.toString());
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
0

There is a easiest solution for it, Just add your string in string.xml file of your value folder under "res" folder of android studio.

for make two words bold just add this string to above stirng.xml file.

<b>The</b> first word of each paragraph\nis <b>what</b> I would like to be bold faced

and then use it like.

    Appliances.add(R.string.strinname);
Asif Ghanchi
  • 236
  • 3
  • 11
0

Use below class i.e, yourTextView.setText(new RichTextView(sentence).setBold(wordWichYouwantBold))

You can find another useful method inside :) Enjoy!!!

import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.view.View;

/**
 * Created by RajeshKushvaha on 19-11-16
 */


//.setText(text, TextView.BufferType.SPANNABLE); to textView if not work
public class RichTextView extends SpannableString {
private String syntax;

public RichTextView(String syntax) {
    super(syntax);
    this.syntax = syntax;
}

public RichTextView setTextColor(String word, int color) {
    setSpan(new ForegroundColorSpan(color), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

public RichTextView setSize(String word, float howMuch) {
    setSpan(new RelativeSizeSpan(howMuch), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

public RichTextView setStrikeOut(String word) {
    setSpan(new StrikethroughSpan(), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

public RichTextView setUrl(String word, String redirectUrl) {
    setSpan(new URLSpan(redirectUrl), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

public RichTextView setBold(String word) {
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    setSpan(boldSpan, syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

//setMovementMethod(LinkMovementMethod.getInstance()); after or before call
public RichTextView setClickable(String word, final setOnLinkClickListener listener) {
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            if (listener != null) {
                listener.onLinkClicked();
            }
        }
    };
    setSpan(clickableSpan, syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return this;
}

public interface setOnLinkClickListener {
    public void onLinkClicked();
}
}
Community
  • 1
  • 1
Rajesh
  • 2,618
  • 19
  • 25