does anyone know whether is it possible to display a dialog with html code on android device? I would be more than thankful for any tip about it.
What I want to di is design a html layout and display it in a dialog inside the custom applicati开发者_如何学运维on.
Thanks!
WebView is definitely an option since it is essentially a web browser in a box, but like Mark said, it's a bit heavyweight and will cause a noticeable delay.
TextViews can handle basic HTML too, and if you're just looking for some simple formatting (bold, italics, color), then using the standard text view is the way to go.
It's important to know the difference between a String and a CharSequence (and Spannable) - Strings do not have HTML support, and if you grab a string from the resources (through Context.getString()), it will automatically strip out all HTML code. So you need to use Context.getText()).
In any case, here is an example:
new AlertDialog.Builder(this)
.setTitle("HTML Example")
.setMessage(Html.fromHtml("<b>Bold text</b> <i>and italics</i>"))
.setPositiveButton("Sweet")
.create()
.show();
Expanding upon JRL's answer, you can use AlertDialog.Builder
and its setView()
to put a WebView
in the main area of the dialog. Note, though, that if you are not using WebView
anywhere else in your application, the first time you use it, it will take a second or so to initialize, which may make your dialog sluggish.
If all you need is rudimentary HTML formatting (bold, italics, color), TextView
takes a Spannable
, and the Html
class in Android can convert from HTML source to a Spannable
for use with TextView
.
Not sure what you're trying to do, but WebView
handles HTML.
精彩评论