본문 바로가기
Android

WebView 기본적인 사용

by 일용직 코딩노동자 2019. 9. 24.
728x90
반응형

AndroidManifest.xml (매니페스트에 인터넷을 사용하겠다는 권한설정)

<uses-permission android:name="android.permission.INTERNET"/> 

추가해줘야합니다.

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/webview"
>
</WebView>

</android.support.constraint.ConstraintLayout>

 

MainAcitivity.java

import android.support.v7.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;
private WebSettings mWebSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mWebView=findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient()); //클릭시 새창 뜨지않게
mWebSettings=mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);

mWebView.loadUrl("https://m.naver.com"); //모바일로 띄울것이니 앞에주소는 www가 아닌 m
}
}

 

--결과--

 

궁금하신 사항은 댓글 남겨주세요.

728x90
반응형

댓글