2

I want to make the text stored inside a string variable to bold.

String buttonText = t.getText().toString(); String val = map.get(buttonText);

          new MaterialDialog.Builder(LearningMode.this)
                    .iconRes(BalloonColor.get(2))
                    .limitIconToDefaultSize() 
                    .title("Meaning")

                    .content(buttonText+"\n"+val)

                    .positiveText("Continue")
                    .cancelable(false)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            mover.resume();
                        }
                    })

                    .show();

at .content(buttonText+"\n"+val) i want the string stored inside this to become bold. i have tried html.fromhtml it is not working

like this Html.fromHtml(" < b > "+buttonText+" < / b >")

added space in the above as it is converting to bold

and other methods like declaring in strings.xml with < b > wont work here as i'm dealing it programmatically here!

ultimately when the string button text is displayed in the output sholud be bold.

i have already referred this Set TextView text from html-formatted string resource in XML

Update:
There is no textview used.

Someone please help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

2

The MaterialDialog.Builder class's content(CharSequence) method should work just fine for making text bold, as long as you pass in some CharSequence that supports bold text. I know you said Html.fromHtml() isn't working, but in my tests it works perfectly. Here's a really simple activity that's a proof of concept:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();

        String myString = "test";

        new MaterialDialog.Builder(this)
                .title("Hello world")
                .content(Html.fromHtml("this is a <b>" + myString + "</b>"))
                .positiveText("OK")
                .show();
    }
}

enter image description here

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Instead of adding like this .content(Html.fromHtml("" + buttonText + "
    "+val+"")) I added like this!!! .content(Html.fromHtml("" + buttonText + "
    ")+val+"") this small mistake ate hours of my time!!! thanks to @Ben P , your answer helped me realize this!
    – AndroidManifester Dec 08 '17 at 17:24
1

try this:

 buttonText.setTypeface(null, Typeface.BOLD);
2hTu2
  • 378
  • 4
  • 19
1

You may use SpannableString for it. say you ave a TextView called txt then the code is:

 SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
 StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);

 sb.setSpan(bss, 0, sb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

 txt.setText(sb);

Updated

As I seen in material dialog library it has bug for that so try below code

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


        return new MaterialDialog.Builder(getActivity())
                .title(R.string.about)
                .positiveText(R.string.dismiss)
                .content(fromHtml(getString(R.string.about_body)))
                .contentLineSpacing(1.6f)
                .build();


    }
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
    Spanned result;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
    } else {
        result = Html.fromHtml(html);
    }
    return result;
}
}

for more https://github.com/afollestad/material-dialogs/issues/1290

Munir
  • 2,548
  • 1
  • 11
  • 20