본문 바로가기

개발도구/aOS - 안드로이드 개발

[안드로이드] webview

웹뷰 활용 : http://croute.me/405



 Activity Code & Layout XML Code

Activity
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package me.croute.webview;
 
import me.croute.R;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
 
 
/**
 * 웹뷰 사용 기본 예제
 *
 * @author croute
 * @since 2011.07.27
 */
public class WebViewExampleActivity extends Activity
{
    private static final String DEFAULT_URL = "http://naver.com";
     
    private ProgressBar mPbProgress;
    private WebView mWvBrowser;
         
     
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view_example_activity);
         
        mPbProgress = (ProgressBar)findViewById(R.id.web_view_example_activity_pb_progress);
        mWvBrowser = (WebView)findViewById(R.id.web_view_example_activity_wv_browser);
         
        mWvBrowser.getSettings().setJavaScriptEnabled(true);
        mWvBrowser.setWebViewClient(new WebViewClient());      
        mWvBrowser.loadUrl(DEFAULT_URL);       
         
        // 웹뷰의 진행 상태를 표시하기 위한 프로그레스바
        mWvBrowser.setWebChromeClient(new WebChromeClient()
        
               public void onProgressChanged(WebView view, int progress)
               
                   if (progress<100)
                   {
                       mPbProgress.setVisibility(ProgressBar.VISIBLE);
                   }
                   else if (progress==100)
                   {
                       mPbProgress.setVisibility(ProgressBar.GONE);
                   }
                   mPbProgress.setProgress(progress); 
               }  
        });
         
    }
 
}



Layout XML
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:background="#FFFFFF" >
    <ProgressBar
        android:id="@+id/web_view_example_activity_pb_progress"
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        style="?android:attr/progressBarStyleHorizontal" />
    <WebView
        android:id="@+id/web_view_example_activity_wv_browser"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>