iamasgor

simpleWebViewApp

Aug 3rd, 2015
199
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //MainActivity.java file
  2. package asgor.net.myweb;
  3.  
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.KeyEvent;
  7. import android.view.Window;
  8. import android.webkit.WebView;
  9. import android.webkit.WebViewClient;
  10.  
  11.  
  12. public class MainActivity extends Activity {
  13.     private WebView mWebView;
  14.  
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  19.         mWebView = new WebView(this);
  20.         mWebView.getSettings().setJavaScriptEnabled(true);
  21.         mWebView.loadUrl("http://asgor.net/");
  22.         mWebView.setWebViewClient(new WebViewClient() {
  23.             @Override
  24.             public boolean shouldOverrideUrlLoading(WebView view, String url) {
  25.                 view.loadUrl(url);
  26.                 return true;
  27.             }
  28.         });
  29.  
  30.         this.setContentView(mWebView);
  31.     }
  32.  
  33.     @Override
  34.     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
  35.         if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
  36.             mWebView.goBack();
  37.             return true;
  38.         }
  39.         return super.onKeyDown(keyCode, event);
  40.     }
  41. }
  42.  
  43. //AndroidManifest.xml file
  44.  
  45. <?xml version="1.0" encoding="utf-8"?>
  46. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  47.     package="asgor.net.myweb" >
  48.  
  49.     <uses-permission android:name="android.permission.INTERNET"/>
  50.  
  51.     <application
  52.         android:allowBackup="true"
  53.         android:icon="@mipmap/ic_launcher"
  54.         android:label="@string/app_name"
  55.         android:theme="@style/AppTheme" >
  56.         <activity
  57.             android:name=".MainActivity"
  58.             android:label="@string/app_name" >
  59.             <intent-filter>
  60.                 <action android:name="android.intent.action.MAIN" />
  61.  
  62.                 <category android:name="android.intent.category.LAUNCHER" />
  63.             </intent-filter>
  64.         </activity>
  65.     </application>
  66.  
  67. </manifest>
  68.  
  69. //add webViewClient for webView app.
  70. //Import this
  71. import android.webkit.WebViewClient;
  72. //then add this before load url.
  73. view.setWebViewClient(new WebViewClient());
  74.  
  75. //Fixed orientation.
  76. android:screenOrientation="portrait"
  77.  
  78. //Prevent reload page when orientation change.
  79. android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
RAW Paste Data