How to display HTML text in a PopUp field using the alert dialog?

I have a dialog alert builder for Android to display some custom message (String) as a popup for the user.

Now, my requirement is a message box that must support the HTML format for this message. In particular, this user message may contain a URL / link. Selecting this link should put the current application in the background and pop up in the phone browser and display the user at the URL that was in the link. Closing the browser will bring the application back to the fore.

How can i do this? Can I use the same dialog box designer or any other available option?

+6
source share
3 answers

All of the above answers will not delete the html tag, for example, etc., if this line contains, I tried to delete all the tags, and this works 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 custom_dialog will look like this:

 <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> 

The above code will remove the entire html tag and display the example as "Click capable URL" to all others in the specified html formatting text.

+10
source

create a custom notification dialog, follow this link

1.create a layout for your dialogue

a. create a text view in this layout

b. Use setText(Html.fromHtml(Source text here)) then do

 Linkify.addLinks(Textview, Linkify.ALL) 

2. Paste this layout in the alerts dialog as shown in the tutorial

+3
source

Try using the Spannable class from android.text.Spannable

 Spannable span = Spannable.Factory.getInstance().newSpannable("html text"); textView.setText(span); 
0
source

Source: https://habr.com/ru/post/903165/


All Articles