프로그래밍/안드로이드

안드로이드 - 웹뷰(WebView)로 웹페이지 보여주기

가카리 2015. 8. 15. 23:08
반응형

 

 

 

 

웹뷰는 웹페이지를 보여주는 위젯이다.

 

 

웹뷰를 쓰기위해서는 모바일장비에서 인터넷 액세스를 위해 매니페스트 파일에서 다음을 추가하여야 한다.

 

    <uses-permission android:name="android.permission.INTERNET" />

 

이러한 퍼미션을 추가하지않으면 네트워크 입출력이 항상 실패하게 된다.

 

이러한 점을 유의하고 아래와 같이 매니페스트 파일에 추가하여야한다.

 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.ch13_webview"

android:versionCode="1"

android:versionName="1.0" >

 

<uses-sdk

android:minSdkVersion="19"

android:targetSdkVersion="19" />

 

    <uses-permission android:name="android.permission.INTERNET">

</uses-permission>

 

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

 

<activity

android:name=".WebViewTest"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

 

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

 

</manifest>

 

 

xml파일

activity_web_view_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

      

<EditText

android:id="@+id/address"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="http://www.naver.com"

 

/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

>

 

<Button

android:id="@+id/btngo"

android:layout_width="10px"

android:layout_weight="1"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="GO"/>

<Button

android:id="@+id/btnback"

android:layout_width="10px"

android:layout_weight="1"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Back"

/>

<Button

android:id="@+id/btnforward"

android:layout_width="10px"

android:layout_weight="1"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Ford"

/>

<Button

android:id="@+id/btnlocal"

android:layout_width="10px"

android:layout_weight="1"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="Local"

/>

</LinearLayout>

    <WebView

     android:id="@+id/web"

     android:layout_width="match_parent"

         android:layout_height="match_parent"

         android:focusable="true"

         android:focusableInTouchMode="true"

      

     />

 

</LinearLayout>

 

 

자바파일

WebViewTest.java

package com.example.ch13_webview;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.EditText;

 

 

public class WebViewTest extends Activity {

    WebView mWeb;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_web_view_test);

        

        mWeb = (WebView)findViewById(R.id.web);

        //웹뷰 연결 부분

        mWeb.setWebViewClient(new MyWebClient());

        

        //웹뷰 설정 변경

        WebSettings set = mWeb.getSettings();

        //자바스크립트 사용가능하게함

        set.setJavaScriptEnabled(true);

        //+ - 크기조절 가능하게함.

        set.setBuiltInZoomControls(true);

        //아래것을해야 뒤로가기와 앞으로가기가 잘됨

        set.setCacheMode(WebSettings.LOAD_NO_CACHE);

        

        //첫페이지 설정

        mWeb.loadUrl("http://www.google.com");

    }

 

 

    public void mOnClick(View v){

        switch(v.getId()){

        //에디트텍스트의 값을 읽어와서 웹페이지로

        case R.id.btngo:

            String url;

            EditText addr = (EditText)findViewById(R.id.address);

            url = addr.getText().toString();

            mWeb.loadUrl(url);

            break;

        //뒤로가기 버튼

        case R.id.btnback:

            if(mWeb.canGoBack()){

                mWeb.goBack();

            }

            break;

        //앞으로가기 버튼

        case R.id.btnforward:

            if(mWeb.canGoForward()){

                mWeb.goForward();

            }

        }

    }

    

    class MyWebClient extends WebViewClient{

        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // TODO Auto-generated method stub

            view.loadUrl(url);//웹뷰가 url 받도록

            return true;

        }

    }

    

}

 

 

 

실행 화면

 

 

 
반응형