본문 바로가기

JAVA/Android

안드로이드 WebView 예제 따라하기

- WebView를 이용해서 홈페이지를 내 App에 보여줌으로써, 내 것처럼 사용 할 수 있다.
- 웹언어를 이용해서 App을 만들 수 있다는 말이 된다.

-Java 소스
public class Ex12_WebVIewActivity extends Activity implements OnClickListener {
WebView mWeb;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mWeb = (WebView) this.findViewById(R.id.web1);
mWeb.setWebViewClient(new WebViewClient());

WebSettings set = mWeb.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);

mWeb.loadUrl("http://www.naver.com");
Button btn = (Button) this.findViewById(R.id.Button1);
btn.setOnClickListener(this);
Button btn2 = (Button) this.findViewById(R.id.Button2);
btn2.setOnClickListener(this);
Button btn3 = (Button) this.findViewById(R.id.Button3);
btn3.setOnClickListener(this);
Button btn4 = (Button) this.findViewById(R.id.Button4);
btn4.setOnClickListener(this);
Button btn5 = (Button) this.findViewById(R.id.Button5);
btn5.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText addr = (EditText) this.findViewById(R.id.etn1);
switch (arg0.getId()) {
case R.id.Button1: // 사용자가 원하는 사이트가기
String url = addr.getText().toString();
mWeb.loadUrl(url);
break;
case R.id.Button2: // 뒤로가기
mWeb.goBack();
break;
case R.id.Button3: // 앞으로가기
if (mWeb.canGoForward()) {
mWeb.goForward();
}
break;
case R.id.Button4:
mWeb.loadUrl("file:///android_asset/test.html");
break;
case R.id.Button5:
mWeb.loadUrl("http://www.google.com");
break;
}
}
}
 
- xml 소스
<?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" >

    <EditText
        android:id="@+id/etn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Go" />

        <Button
            android:id="@+id/Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Back" />

        <Button
            android:id="@+id/Button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Forward" />

        <Button
            android:id="@+id/Button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Local" />

        <Button
            android:id="@+id/Button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Home" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <WebView
            android:id="@+id/web1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</LinearLayout>

- 실행 화면