11.1 Android with HTML
-
Upload
oum-saokosal -
Category
Education
-
view
155 -
download
0
Transcript of 11.1 Android with HTML
11.1. With HTMLOum Saokosal
Master of Engineering in Information Systems, South Korea855-12-252-752
Hyperlink from Android App
• Like in HTML, you can create a hyperlink from normal text, picture, and other views as well.
• Simple Text Link:<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:autoLink="web" android:text="www.facebook.com"/>
• Custom Text Link:<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"android:onClick="gotoWeb"android:clickable="true"android:text="Click Here"/>
in Java:public void gotoWeb(View v){
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.google.com")); startActivity(intent);}
• Link on Image:<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bird" android:onClick="gotoWeb"/>
in Java:public void gotoWeb(View v){
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.google.com")); startActivity(intent);}
Generate HTML page1. In main.xml:
<WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="150dp" />
2. In java:StringBuffer page = new StringBuffer();page.append("<html><body> Welcome to My");page.append("<a
href='http://kohsantepheapdaily.com.kh'>");page.append("Koh News</a></body></html>"); WebView browser = (WebView)
findViewById(R.id.webView1);browser.loadDataWithBaseURL(null, page.toString(),
"text/html", "UTF-8", null);
//Complete code//please do import yourself
public class WithHTML extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StringBuffer page = new StringBuffer();
page.append("<html><body>" + "Welcome to My <a href='http://kohsantepheapdaily.com.kh'>" + "Koh News</a></body></html>"); WebView browser = (WebView) findViewById(R.id.webView1); browser.loadDataWithBaseURL(null, page.toString(), "text/html",
"UTF-8", null);}
public void gotoWeb(View v){Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);}
}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dip">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="gotoWeb" android:clickable="true" android:text="Click Here" /> <TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="web" android:text="www.facebook.com" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bird" android:onClick="gotoWeb" /> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="150dp" /> </LinearLayout>