Reputation: 1663
I know there's a lot of questions on here about reading local HTML files, but I haven't found any that satisfy my needs.
I would like to have a DialogFragment pop up, show a message with a link in it, click that link and have a local HTML file read (preferably from the default web browser).
What I have so far:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setMessage(Html.fromHtml("I have read and agree to the " + "<a href=\"file:///android_asset/privacy.html\">Terms and Conditions</a>" + " and the " + "<a href=\"http://www.samsung.com/global/business/mobile/info/privacy.html\">Privacy Policy</a>"));
alertDialogBuilder.setCancelable(false);
//rest of code...
}
The second link which points to the online version works like a charm. The first link should point to a local privacy.html file in my assets folder. When clicking on the first link, an error occurs:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/privacy.html (has extras) }
Anyone have any tips/advice for this situation? I've tried multiple different HTML files, all with the same result. Am I going to have to use my own WebView for something like this? Seems like there should be a way to ask Android to view a local HTML file from the default browser when clicking on a link...
Upvotes: 1
Views: 1563
Reputation: 380
myTextView.setText(Html.fromHtml(readTxt()));
//This function will return string which you can set in your textview. And that String have html codes so use Html.fromHtml
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.My_html_file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
}
this one is already answered
Upvotes: 1
Reputation: 1241
Technically it won't work because Link is html tag. It will work in webview. Still you want to do this with textview you have customise your code Try this code :
public class CustomLinkMovementMethod extends LinkMovementMethod
{
private static Context movementContext;
private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();
public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_UP)
{
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0)
{
String url = link[0].getURL();
if (url.startsWith("https"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Link was clicked", Toast.LENGTH_LONG).show();
} else if (url.startsWith("tel"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
} else if (url.startsWith("mailto"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
}
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
public static android.text.method.MovementMethod getInstance(Context c)
{
movementContext = c;
return linkMovementMethod;
}
This should be called from the textview in the following manner:
textViewObject.setMovementMethod(CustomLinkMovementMethod.getInstance(context));
Upvotes: 3