How to load external webpage in WebView
Thanks to this post, I finally found the solution. Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;
public class Main extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview );
}
}
Open page in WebView by clicking on an external link
Deeplinks
normally just triggers the app to be opened, they don't help you navigate or open a specific location unless you define Fragment
or Activity
based intent-filters
or manually handle.
You should get the data which includes URL
from intent
first then give it to WebView
as a URL
. You can do that inside either onNewIntent
or onCreate
methods like so;
override fun onNewIntent(intent: Intent){
super.onNewIntent(intent)
val uri = intent.data
}
then load it in WebView
like;
webview.loadUrl(uri.toString())
load web page in webview (android)
You have not set webviewclient so please set it using below code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.webView1);
browser .setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
browser.loadUrl("http://www.google.com");
}
How to load an URL inside a WebView using Android Kotlin?
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.kotlinwebview.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
2.MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
webView.settings.setJavaScriptEnabled(true)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
webView.loadUrl("https://www.google.co.in/")
}
}
Loading a local file via a external webpage inside WebView
Switched to Cordova & Dropped the idea of interlinking the pages, rather I programmed the pages using REST API via Wordpress.
Related Topics
Read Only File System on Android
Android List View Drag and Drop Sort
Hide/Show Bottomnavigationview on Scroll
Why My App Is Not Showing Up on Tablets in Google Play
Is This the Right Way to Clean-Up Fragment Back Stack When Leaving a Deeply Nested Stack
Remove Underline from Links in Textview - Android
Using Asynctask to Load Images in Listview
Highlight Text in Textview or Webview
How to View Androidmanifest.Xml from APK File
Why Does Contentresolver.Requestsync Not Trigger a Sync
How to Display Album Art Using Mediastore.Audio.Albums.Album_Art
How to Read Value from String.Xml in Android