티스토리 뷰
1. MainActivity.kt
package com.tistory.boywin1992
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView = findViewById<WebView>(R.id.webView)
val url = "https://boywin1992.tistory.com/"
// zoom
webView.settings.setSupportZoom(true)
webView.settings.builtInZoomControls = true
webView.settings.displayZoomControls = true
// new page
webView.settings.javaScriptCanOpenWindowsAutomatically = false
webView.settings.setSupportMultipleWindows(false)
// html size
webView.settings.useWideViewPort = true
webView.settings.loadWithOverviewMode = true
// javascript
webView.settings.javaScriptEnabled = true
// load local images
webView.settings.loadsImagesAutomatically = true
// cache
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
// local storage
webView.settings.domStorageEnabled = true
// allow file access
webView.settings.allowFileAccess = true
// database API
webView.settings.databaseEnabled = true
// media play
webView.settings.mediaPlaybackRequiresUserGesture = false
// allow content access with url
webView.settings.allowContentAccess = true
// location API
webView.settings.setGeolocationEnabled(true)
// client
webView.webViewClient = CustomWebViewClient(this)
webView.webChromeClient = CustomWebChromeClient(this, url)
// load URL
webView.loadUrl(url)
}
}
2. CustomWebViewClient
package com.tistory.boywin1992
import android.app.Activity
import android.view.Gravity
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar
class CustomWebViewClient(private val activity : Activity): WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
val activityView = activity.window.decorView.rootView // fragment
val message = "hello snack bar\nurl = $url"
val snackBar = Snackbar.make(activityView, message, Snackbar.LENGTH_SHORT)
val snackBarView = snackBar.view
val params = snackBarView.layoutParams as FrameLayout.LayoutParams
params.gravity = Gravity.CENTER
snackBarView.layoutParams = params
snackBarView.findViewById<TextView>(R.id.snackbar_text).maxLines = 10
snackBar.show()
}
}
3. CustomWebChromeClient
package com.tistory.boywin1992
import android.app.Activity
import android.app.Dialog
import android.os.Message
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
class CustomWebChromeClient(private val activity: Activity,private val url:String) : WebChromeClient() {
override fun onCreateWindow(
view: WebView?,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message?
): Boolean {
val newWebView = WebView(activity)
// zoom
newWebView.settings.setSupportZoom(true)
newWebView.settings.builtInZoomControls = true
newWebView.settings.displayZoomControls = true
// new page
newWebView.settings.javaScriptCanOpenWindowsAutomatically = false
newWebView.settings.setSupportMultipleWindows(false)
// html size
newWebView.settings.useWideViewPort = true
newWebView.settings.loadWithOverviewMode = true
// javascript
newWebView.settings.javaScriptEnabled = true
// load local images
newWebView.settings.loadsImagesAutomatically = true
// cache
newWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
// local storage
newWebView.settings.domStorageEnabled = true
// allow file access
newWebView.settings.allowFileAccess = true
// database API
newWebView.settings.databaseEnabled = true
// media play
newWebView.settings.mediaPlaybackRequiresUserGesture = false
// allow content access with url
newWebView.settings.allowContentAccess = true
// location API
newWebView.settings.setGeolocationEnabled(true)
// client
val dialog = Dialog(activity)
dialog.setContentView(newWebView)
dialog.show()
newWebView.webViewClient = CustomWebViewClient(activity)
newWebView.webChromeClient = object : WebChromeClient() {
override fun onCloseWindow(window: WebView?) {
dialog.dismiss()
}
}
(resultMsg?.obj as WebView.WebViewTransport).webView = newWebView
resultMsg.sendToTarget()
// load URL
newWebView.loadUrl(url)
// test
// webView.webView.loadUrl("file:///android_asset/sample.html")
//
return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg)
}
}
4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tistory.boywin1992">
<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/Theme.boywin1992"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'android' 카테고리의 다른 글
play kotlin (0) | 2022.05.22 |
---|---|
how to get fcm token (0) | 2022.05.21 |
[Windows OS] 안드로이드 스튜디오 버전 확인 방법 (0) | 2022.05.21 |
[Mac OS] 안드로이드 스튜디오 버전 확인 방법 (0) | 2022.05.21 |
[안드로이드/코틀린] view binding 사용하는 방법 (0) | 2022.05.21 |
- Total
- Today
- Yesterday
- web view
- defaultInputmode
- logcat
- new project
- Android
- kotlin
- javaClass
- custom web view
- create project
- web view settings
- 화면 방향 고정
- 웹뷰
- 로그캣
- 안드로이드
- web chrome view
- permissions
- 외부 브라우저
- StringWriter
- Inputmode
- 영문 키보드
- logtag
- andorid
- 클래스 이름
- simpleName
- check permissions
- 화면 고정
- screenorientation
- 코틀린
- create new project
- android studio
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |