Skip to content

Instantly share code, notes, and snippets.

@lukeduncan-scot
Last active November 21, 2018 23:32
Show Gist options
  • Select an option

  • Save lukeduncan-scot/9e8f7f380e72d32fd9cbc022e78e26fb to your computer and use it in GitHub Desktop.

Select an option

Save lukeduncan-scot/9e8f7f380e72d32fd9cbc022e78e26fb to your computer and use it in GitHub Desktop.
Activity that loads a URL into a WebView
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="***">
// This is required
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".WebActivity"/>
</application>
</manifest>
public class WebActivity extends AppCompatActivity {
private static final String KEY_URL = WebActivity.class.getSimpleName() + "_URL";
public static Intent createIntent(Context context, URL url) {
return new Intent(context, WebActivity.class)
.putExtra(KEY_URL, url);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
WebView webView = findViewById(R.id.webView);
// Keep this line if you want to open in your app, otherwise it opens in Chrome
webView.setWebViewClient(new WebViewClient());
final URL url = (URL) getIntent().getSerializableExtra(KEY_URL);
webView.loadUrl(url.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment