Webview

2017. 8. 21. 02:51Application Programming/Android

0) manifest.xml에 인터넷 사용 권한 추가

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


1) WebView 객체 생성

2) findViewById로 위에서 생성한 객체와 뷰 연결

3) 객체.loadUrl("주소")

4) 객체.setWebViewClient(new VebViewClient())

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//
mWebView=(WebView)findViewById(R.id.webview1);
mWebView.setBackgroundColor(0);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.loadUrl("http://www.naver.com/");
mWebView.setWebViewClient(new myWebViewClient());
//
}
private class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
}