Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created May 5, 2019 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/b363a40ae174044724c8b8dc96a8e08c to your computer and use it in GitHub Desktop.
Save codingwithsara/b363a40ae174044724c8b8dc96a8e08c to your computer and use it in GitHub Desktop.
How to display HTML text in AlertDialog
package com.codingwithsara.myapplication;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String message = "<p>Thank you for watching this video!</p>" +
"<p><b>My Website</b> : <a href=\"https://codingwithsara.com\">Tap Here</a></p>";
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Hello")
.setMessage(Html.fromHtml(message))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
})
.show();
((TextView) alertDialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment