1. 인터넷을 연결되어야 하는 경우 manifest에서 internet 권한을 추가해 줘야한다.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.saii.webview">
<application> ...
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
2. assests 폴더를 생성한다.
New > Folder > Assets Folder 아래 이미지 참조.
3. HTML은 할 줄 몰라서 간단하게 만들었습니다.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Saii WebView Example</title>
</head>
<script type="text/javascript">
function showAndroidToast(toast) {
var word = document.createElement('div');
word.textContent = Saii.showToast(toast);
document.body.append(word);
}
</script>
<body>
<h1 id="webview-example">Saii WebView - Example</h1>
<input id="keyword" type="text" placeholder="keyword">
<br>
<button type="submit" onClick="showAndroidToast(keyword.value)">Submit</button>
</body>
</html>
4. 메인 화면에 WebView 하나를 배치한다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wv_main"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
5. 마지막으로 WebView를 Html 또는 다른 웹 페이지와 연결을 시켜준다.
MainActvity.java
package com.saii.webview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.wv_main);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//mWebView.loadUrl("https://saii42.tistory.com/97");
mWebView.loadUrl("file:///android_asset/index.html");
}
}
참고
https://github.com/saii42/android/tree/main/example/WebView