Skip to content

Instantly share code, notes, and snippets.

@takeshiyako2
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshiyako2/e9a0cbdb3892a3dabe7a to your computer and use it in GitHub Desktop.
Save takeshiyako2/e9a0cbdb3892a3dabe7a to your computer and use it in GitHub Desktop.
Android SampleWebView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yako.samplewebview" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>
</resources>
package com.example.yako.samplewebview;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
// mWebViewをprivateに
private WebView mWebView;
// WebViewで開くURLを設定
private static String top_url = "http://takeshiyako.blogspot.jp/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// レイアウトのリソースを利用
mWebView = (WebView) findViewById(R.id.webView);
// WebViewClientを利用 標準のブラウザの起動を防ぐ
mWebView.setWebViewClient(new WebViewClient());
// JavaScriptを有効
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// ユーザエージェント設定
String ua = mWebView.getSettings().getUserAgentString();
ua = ua + " my-android-app";
mWebView.getSettings().setUserAgentString(ua);
// キャッシュクリア
mWebView.clearCache(true);
// 履歴をクリア
mWebView.clearHistory();
// URL読み込み
mWebView.loadUrl(top_url);
}
/***
* Activityの「onResume」に基づき開始される
*/
@Override
public void onResume() {
super.onResume();
mWebView.onResume();
}
/***
* Activityが「onPause」になった場合や、Fragmentが変更更新されて操作を受け付けなくなった場合に呼び出される
*/
@Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
/***
* フォアグラウンドでなくなった場合に呼び出される
*/
@Override
public void onStop() {
super.onStop();
mWebView.onPause();
}
/***
* Activityが破棄される時、最後に呼び出される
*/
@Override
public void onDestroy() {
super.onDestroy();
mWebView.destroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment