Saturday, June 22, 2013

Android Parsing HTML Content Containing Links


Moving ahead from simple HTML View, Here we are going to see how to place a link inside a HTML Content using TextView.
A simple example for how to use links in textview.
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
[/sourcecode]
[sourcecode language="java"]
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class ExampleApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String source = "<b><font color=#ff0000> Html View using TextView"
+ "</font></b><br><br><a href='http://www.AndroidPeople.com'>AndroidPeople.com</a>"
+ "<br><br><a href='http://www.Android.com'>Android.com</a>";
TextView textView = (TextView) findViewById(R.id.TextView01);
textView.setText(Html.fromHtml(source));
// Used to enable links in textview.
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
[/sourcecode]

No comments:

Post a Comment