반응형

WebView objects allow you to display web content as part of your activity layout but lack some of the features of fully-developed browsers. A WebView is useful when you need increased control over the UI and advanced configuration options that will allow you to embed web pages in a specially-designed environment for your app.

In most cases, Android recommends using a standard web browser, like Chrome, to deliver content to the user. 


크롬이나 다른 웹브라우저와 같은 다양한 기능은 없지만 WebView를 사용해 앱 안에서 간단히 인터넷 웹 페이지를 디스플레이 할 수 있다.


<AndroidManifest.xml>

1
2
3
4
5
6
7
8
9
10
    <uses-permission android:name="android.permission.INTERNET"/>
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">



<MainActivity.java>

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
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    WebView webView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editText);
        webView = findViewById(R.id.webView);
 
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // Tells the WebView to enable JavaScript execution.
        webSettings.setBuiltInZoomControls(true);   // Sets whether the WebView should use its built-in zoom mechanisms.
        //webSettings.setDisplayZoomControls(false);
        // Sets whether the WebView should display on-screen zoom controls when using the built-in zoom mechanisms.
        // The default is true.
 
        webView.setWebViewClient(new ViewClient());
 
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.loadUrl(editText.getText().toString());
            }
        });
    }
 
    private class ViewClient extends WebViewClient {
        // This method was deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
 
            return true;
        }
 
//        @Override
//        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//            view.loadUrl(request.getUrl().toString());
//
//            return true;
//        }
    }
}




Run the app and enter a URL.


Click 'OPEN' button and it shows the web site.


Use the built-in zoom controls if needed.


반응형
Posted by J-sean
: