ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
11.1 Android with HTML
1. 11.1. With HTML
Oum Saokosal
Master of Engineering in Information Systems, South Korea
855-12-252-752
oum_saokosal@yahoo.com
2. 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"/>
3. • 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);
}
4. • 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);
}
5.
6. Generate HTML page
1. 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);
7. //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);
}
}