Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the Html formatted text in the PopUp box using alert dialog builder?

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?

like image 736
Lakshmanan Avatar asked Dec 06 '11 05:12

Lakshmanan


2 Answers

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.

like image 125
Lakshmanan Avatar answered Nov 14 '22 23:11

Lakshmanan


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

like image 23
Pratik Bhat Avatar answered Nov 14 '22 23:11

Pratik Bhat