I was try to create a android webview with the refresh option.
Whenever I pull down for refresh it always goes to the home page.
I have try using the swiperefeshlayout. My code are as follows
activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/destiny"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/progressBar" />
</RelativeLayout>
MainActivity.java file
package com.my.project;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
WebAction();
}
});
WebAction();
}
public void WebAction(){
myWebView = (WebView)findViewById(R.id.destiny);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
myWebView.loadUrl("http://myurl");
swipe.setRefreshing(true);
myWebView.setWebViewClient(new WebViewClient());
AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this); //pass the activity in constructure
appUpdateChecker.checkForUpdate(false); //mannual check false here
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onReceivedError(WebView view, int errorCode, String description, String fallingUrl) {
myWebView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
progressBar.setVisibility(View.VISIBLE);
setTitle("Loading.....");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url){
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
super.onPageFinished(view, url);
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
With this code the refresh always goes to the home page
but I want to refresh the webview on the same page itself.
You are setting all variables and actions again onRefresh() you only need to load current url of WebView again. So you need to change your OnRefreshListener like below;
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myWebView.loadUrl(myWebView.getUrl());
}
});
Here are my codes and I cannot see any webview on my application. I tried to create a button and I could see that. When I ran my app, there is only a white blank space in place of webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical"
tools:context="com.example.user.calisma1.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/b"/>
<WebView
android:id="#+id/wv"
android:layout_width="match_parent"
android:layout_height="300dp"></WebView>
import java.util.Random;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView wv2;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv2 = findViewById(R.id.wv);
wv2.getSettings().setJavaScriptEnabled(true);
wv2.loadUrl("https://webmail.etu.edu.tr/");
b = findViewById(R.id.b);
}
}
Add internet permission in manifest, if not added:
<uses-permission android:name="android.permission.INTERNET"/>
Add below code before loadUrl() to set webview client:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
Your url(https://webmail.etu.edu.tr/) involves redirection with HTTPS(SSL) so, following lines should be added.
wv2.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv2.loadUrl("https://webmail.etu.edu.tr/");
I can't load this specific url (http://sia.bogota.unal.edu.co/academia/) using WebView, but I can load other URLs. (See images below)
This is my code
Activity Class:
package co.luisfer.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
Activity activity;
private ProgressDialog progDailog;
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
webView.loadUrl("http://sia.bogota.unal.edu.co/academia");
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<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
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I don't have any clue what's going on.
Can you help me?
Thanks in advance
PS: I have already set up the internet permission:
<uses-permission android:name="android.permission.INTERNET" />
I am trying to make a simple WebView in my activity and I keep getting a "webpage not available" page in my android browser on the virtual device as well as an actual device.
I've looked though some samples online as well as the example in my book but I believe something is still missing. Any help would be appreciated. Code is below. wv is referencing a webview object in the main.xml file.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wv.getSettings();
wv.getSettings().setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
wv.loadUrl("http://www.amazon.com");
}
}
For an android webView you need to keep 4 things in mind to make it work perfect.
Give the necessary permission to access internet in your android Manifest.xml
Import necessary libs like webclient and webchromeclient in your YourActivity.java
Enable javascript if you are going to show a webpage which needs java support.
Copy and paste all the image files you use in your webView to all the "drawable" folders in "res" folder of your project.
I've given you the complete code from Manifest to Activity.java. It works perfect. All the best..!
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourappname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/yourapplogo"
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>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java:
package com.example.yourappname;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView1);
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.yourwebsite.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient());
setContentView(web);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
//flip screen not loading again
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(web.canGoBack()){
web.goBack();
}
else
{
backButtonHandler();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
// Setting Dialog Title
// Setting Dialog Message
alertDialog.setTitle("Your App Name");
// I've included a simple dialog icon in my project named "dialog_icon", which's image file is copied and pasted in all "drawable" folders of "res" folders of the project. You can include any dialog image of your wish and rename it to dialog_icon.
alertDialog.setIcon(R.drawable.dialog_icon);
alertDialog.setMessage("Exit Now?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
please add the INTERNET permissions in manifest.xml...
you can also view my app using webview....
http://slideme.org/application/surfen-mini
My problem is that the webpage is not loaded inside the WebView.
mWebview.loadUrl("http://www.google.com"); launches the web browser...
This is the code of my activity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.loadUrl("http://www.google.com");
setContentView(mWebview);
}
}
I added the required permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
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 );
}
}
try this
webviewlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
/>
In your Activity:
WebView webView;
setContentView(R.layout.webviewlayout);
webView = (WebView)findViewById(R.id.help_webview);
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
Update
Add webView.setWebViewClient(new WebViewController()); to your Activity.
WebViewController class:
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
webView.setWebViewClient(new WebViewController());
Please use this code:-
Main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/background">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/top_heading"
android:id="#+id/rlayout1">
<TextView android:layout_width="wrap_content"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"
android:textColor="#ffffff" android:textSize="22dip"
android:textStyle="bold" android:layout_height="wrap_content"
android:text="More Information" android:id="#+id/txtviewfbdisplaytitle" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
</RelativeLayout>
MainActivity.Java
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
Try this code if any query ask me.
It's very simple try integrate these lines of code
first take permission in the Android Manifest file
<uses-permission android:name="android.permission.INTERNET" />
then write some code in you Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Then write these code in your MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity{
private WebView mWebview ;
String link = "";// global variable
Resources res;// global variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_modernherbal_main);
mWebview = (WebView) findViewById(R.id.help_webview);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.example.com");
}
}
Try this it'll help you to solve your problem
just go into XML file and give id to your webView then in java paste these line:
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_layout_file_name);
mWebview = (WebView)findViewById(R.id.id_you_gave _to_your_wenview_in_xml);
mWebview.loadUrl("http://www.google.com");
}
}
I used this code that was cool. but have an error. " neterr_cleartext_not_permitted"
show when you use this code then you will face this problem..
I got a solution of this.you have to add this in your AndroidManifest.xml near about Application
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET" /> // ignore if you already added. outside of Application.
You can do like this.
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("Your URL goes here");
try this;
webView.loadData("<iframe src='http://www.google.com' style='border: 0; width: 100%; height: 100%'></iframe>", "text/html; charset=utf-8", "UTF-8");
Add Internet permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
In your Layout:
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
/>
In your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
progressDialog = new ProgressDialog(this);
url_Api = "https://learn.microsoft.com/en-us/learn";
webView = this.findViewById(R.id.webView);
progressDialog.setMessage(getString(R.string.connection_Wait));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
LoadUrlWebView( url_Api );
}catch (Exception e){
Log.w(TAG, "onCreate", e);
}
}
private void LoadUrlWebView( String url_api ) {
try {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new MyWebChromeClient( url_api ));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.loadUrl(url_api);
} catch (Exception e) {
Log.w(TAG, "setUpNavigationView", e);
}
}
private class MyWebChromeClient extends WebChromeClient {
private String urlAccount;
public MyWebChromeClient( String urlAccount ) {
this.urlAccount = urlAccount;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
//Tools.LogCat(context, "INSIDE MyWebChromeClient | onProgressChanged / newProgress1:" + newProgress);
progressDialog.setMessage(newProgress + "% " + getString(R.string.connection_Wait));
if (newProgress < 100 && !progressDialog.isShowing()) {
if (progressDialog != null)
progressDialog.show();
}
if (newProgress == 100) {
if (progressDialog != null)
progressDialog.dismiss();
}
}catch (Exception e){
Log.w( "onProgressChanged", e);
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
sharedPreferences = new Shared_Preferences( context );
sharedPreferences.setPageWebView(view.getUrl());
}
}
Add WebView Client
mWebView.setWebViewClient(new WebViewClient());
You need to add WebView client
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
also you can use onPageFinished to do task after webview done loading web page
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView .loadUrl("http://www.google.com");
webView.setWebViewClient(new MyWebViewClient());
}
}
AndroidManifest.xml: (add uses-permission and android:usesCleartextTraffic)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
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/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Simple Webview activity in kotlin :
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent.getStringExtra(URL_EXTRA)?.let { url ->
WebView(this).apply {
settings.javaScriptEnabled = true // check if your need this
settings.domStorageEnabled = true
settings.setSupportZoom(true)
settings.builtInZoomControls = true
settings.displayZoomControls = false
loadUrl(url)
setContentView(this)
}
}
}
companion object {
const val URL_EXTRA = "url"
}
}
Add below method in your activity class.Here browser is nothing but your webview object.
Now you can view web contain page wise easily.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
browser.goBack();
return true;
}
return false;
}