I have using android alert dialog builder to display the some user message(String) as a pop up box to the user.
Now, My requirement is the message box should be able to support HTML format for this message. Including, there may be a URL/link returned in this user message. Selecting this link should put the current application in the background and popup the phone’s browser and take the user to URL that was in the link. Closing the browser would brings application back to the foreground.
How can I do this? Can i use the same alert dialog builder or any other choice available?
All the above answer will not remove html tag like , etc if the given string contains, I tried to remove all the tags, and this is work fine for me
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Title");
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setMovementMethod(LinkMovementMethod.getInstance());
text.setText(Html.fromHtml("<b>Hello World</b> This is a test of the URL <a href=http://www.example.com> Example</a><p><b>This text is bold</b></p><p><em>This text is emphasized</em></p><p><code>This is computer output</code></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>";));
builder.setView(layout);
AlertDialog alert = builder.show();
and the custom_dialog would be like;
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
The above code will remove all the html tag and shows Example as Click able URL all others in the specified html formatting text.
create a custom alert dialog , follow this link
1.create a layout for your dialog
a. create a textview in that layout
b. Use setText(Html.fromHtml(Source text here))
then do
Linkify.addLinks(Textview, Linkify.ALL)
2.inflate this layout in your alert dialog as shown in the tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With