https://developer.android.com/develop/ui/views/layout/webapps/webview?hl=ko
WebView에서 웹 앱 빌드 | Android Developers
이 페이지는 Cloud Translation API를 통해 번역되었습니다. WebView에서 웹 앱 빌드 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. WebView를 사용하여 웹 애플리케이
developer.android.com
참고
WebView와 WebViewClient는 Android SDK에 기본적으로 포함되어 있는 클래스들이기 때문에 별도의 라이브러리를 dependencies에 추가할 필요가 없음
<< But 이전 버전 Android 에서는 추가 할 수 있음
dependencies {
implementation("androidx.webkit:webkit:1.8.0")
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Android 프로젝트 기준
res/layout/activity_main.xml
없으면 파일, 디렉토리 생성
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 상단에 공간을 차지할 TextView -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="APP"
android:padding="16dp"
android:textSize="18sp"
android:gravity="center" />
<!-- WebView의 높이를 조정하여 전체를 차지하지 않도록 설정 -->
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- 여기에 다른 UI 요소들이 있을 수 있습니다 -->
</LinearLayout>
MainActivity
""" 핵심 코드
~~~
private lateinit var webView: WebView
~~~
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
webView.settings.javaScriptEnabled = true // JavaScript 사용을 허용
webView.settings.domStorageEnabled = true // DOM storage 활성화 // 로컬 스토리지 사용( Token 때문에 필요함 )
webView.settings.allowFileAccess = true
webView.settings.allowContentAccess = true
webView.settings.allowFileAccessFromFileURLs = true
webView.settings.allowUniversalAccessFromFileURLs = true
webView.webViewClient = WebViewClient() // 링크 클릭 시 새 창이 아닌 WebView 내에서 열리도록 설정
webView.loadUrl("https://www.google.com/") // 원하는 URL로 변경
"""
package com.example.rssreader
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
// WebView sdk << << << <<
import android.webkit.WebView
import android.webkit.WebViewClient
// << << << << << << << <<
class MainActivity : ComponentActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
webView.settings.javaScriptEnabled = true // JavaScript 사용을 허용
webView.settings.domStorageEnabled = true // DOM storage 활성화 // 로컬 스토리지 사용( Token 때문에 필요함 )
webView.settings.allowFileAccess = true
webView.settings.allowContentAccess = true
webView.settings.allowFileAccessFromFileURLs = true
webView.settings.allowUniversalAccessFromFileURLs = true
webView.webViewClient = WebViewClient() // 링크 클릭 시 새 창이 아닌 WebView 내에서 열리도록 설정
webView.loadUrl("https://www.google.com/") // 원하는 URL로 변경
}
override fun onStart() {
super.onStart()
// 액티비티가 화면에 나타날 준비가 되었을 때 수행할 작업
//TODO : 시작시 회사 로고 보이게
}
override fun onResume() {
super.onResume()
// UI 업데이트나 리스너 등록
//TODO : 앱이 백그라운드 -> 포그라운드때 핸들링
}
//TODO : (SharedPreferences 사용)로그인정보 기억하다가 앱실행시, 자동 로그인
// override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// if (requestCode == PERMISSION_REQUEST_CODE) {
// // 권한 요청 결과 처리
// //TODO : 앱에서 권한이 필요 할 때 요청하는 핸들러
// }
// }
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack() // WebView에서 뒤로 가기
} else {
super.onBackPressed() // 기본 뒤로 가기
}
}
}
'언어 정리 > kotlin, android' 카테고리의 다른 글
BLE 기능 [ Kotlin ] (1) | 2024.11.19 |
---|---|
android studio IDE 단축키 (0) | 2024.11.15 |
kotlin 문법정리 (0) | 2024.11.14 |
댓글