2

I want my alertDialog look like this enter image description here

My API is 26, and this is my code so far

alertDialog.setTitle("Warning");
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ 9.00 </b></font>",Build.VERSION.SDK_INT));

As you can see I just put 9.00 directly in the String. Right now, I want to make 9.00 a string variable called estCost, which can be changed based on the calculation in my code.

How can I display the "string variable with the color" estCost in the alertDialog.setMessage() ?

I have tried following code

String estCost = calculate(10)  //calculate method will return a double in string.
alertDialog.setTitle("Warning");
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var> estCost </var></b></font>",Build.VERSION.SDK_INT));

But it doesn't work. Please help me ! Thanks.

Amrutha Saj
  • 1,408
  • 16
  • 35
luke lin
  • 51
  • 4

3 Answers3

1

Use spannable text

Spannable myText = new SpannableString("are u sure.....");        
myText.setSpan(new ForegroundColorSpan(Color.RED),7,10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
alertDialog.setMessage(myText);
sarath19
  • 198
  • 1
  • 11
0

You need to concat the value using concatenation operator "+"

alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var>"+ estCost+" </var></b></font>",Build.VERSION.SDK_INT));
Amrutha Saj
  • 1,408
  • 16
  • 35
0

You need to concat the font so use + before the font tag and after the font tag that's all.

Rohit Suthar
  • 967
  • 9
  • 22