0

I want to show the spanString in Bold style and rest of the string in normal style in TextView. But the whole TextView showed in normal style only. Please help me.

if ((mStoreListValue.get(pos).getStoreStatus().equals("CLOSED"))){

   final Dialog dialog = new Dialog(v.getRootView().getContext(), android.R.style.Theme_Translucent_NoTitleBar);
   dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
   dialog.getWindow().setContentView(R.layout.resclosed_dialog);
   ImageView btn_close =  dialog.findViewById(R.id.close_btn);
   TextView txt_close =  dialog.findViewById(R.id.tv_closed);

   SpannableString spanString = new SpannableString(sname);
   spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

   txt_close.setText(spanString +  " " + mContext.getResources().getString(R.string.closed));

   btn_close.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
              dialog.dismiss();
         }
    });
  dialog.show();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 5
    `spanString + " " + ...` – This is implicitly calling `toString()` on `spanString`, which strips any span info on it. – Mike M. Dec 04 '17 at 11:19
  • 1
    Possible duplicate of [Why doesn't my text show up with style when using SpannableStringBuilder?](https://stackoverflow.com/questions/16060991/why-doesnt-my-text-show-up-with-style-when-using-spannablestringbuilder) – Pavneet_Singh Dec 04 '17 at 11:46

3 Answers3

1

Thats because you just concat SpannableString with String which will be result as toString()(a Normal String with hash code of object). Use a single String with multiple Span.
See the following example.

    String s1="My app";
    String s2= "Close";
    SpannableString spanString = new SpannableString(s1+s2);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, s1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spanString.setSpan(new StyleSpan(Typeface.NORMAL), s1.length(), (s1+s2).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spanString);
ADM
  • 20,406
  • 11
  • 52
  • 83
0

Use this

SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString spanString = new SpannableString(sname);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(spanString);
builder.append(mContext.getResources().getString(R.string.closed));
txt_close.setText(builder);

Instead of this

txt_close.setText(spanString +  " " + mContext.getResources().getString(R.string.closed));
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Using String templating, you can have a string resource: <b> indicates bold, %1$d indicates I am expecting a digit/number so I get (5 min away) in the textview

//xml
<string name="confirm_eta_text"> &lt;b>%1$d min &lt;/b> away</string>

// Java   
mEta.setText(
Compat.fromHtml(String.format(getResources().getString(R.string.confirm_eta_text), 5)));

Android String Formatting

Demilade
  • 513
  • 2
  • 7