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!

3 Answers 3

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

1
  • Instead of adding like this .content(Html.fromHtml("<b>" + buttonText + "</b> <br> "+val+"</br>")) I added like this!!! .content(Html.fromHtml("<b>" + buttonText + "</b> <br> ")+val+"</br>") this small mistake ate hours of my time!!! thanks to @Ben P , your answer helped me realize this! Commented Dec 8, 2017 at 17:24
1

try this:

 buttonText.setTypeface(null, Typeface.BOLD);
3
  • sorry buttonText is a String variable! typeface can be used on a string variable i guess.. i tried it is showing error Commented Dec 8, 2017 at 16:42
  • Can you elaborate further on your solution? Commented Dec 8, 2017 at 20:59
  • Instead of adding like this .content(Html.fromHtml("<b>" + buttonText + "</b> <br> "+val+"</br>")) I added like this!!! .content(Html.fromHtml("<b>" + buttonText + "</b> <br> ")+val+"</br>") Commented Dec 19, 2017 at 19:50
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

3
  • this one is also not working! i don't know where the mistake is! sharing which code of me will help?! I'm no using any textview here.. Commented Dec 8, 2017 at 16:47
  • 1
    @ranjithstar256 are you using any third party library for dialog? and want to set content text bold am i right?
    – Munir
    Commented Dec 8, 2017 at 16:53
  • Yes! I'm using this! github.com/afollestad/material-dialogs thanks for asking the right question! Commented Dec 8, 2017 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.