WebView onPageFinished not called when MapView is visible - android

I have a MapView inside a fragment which creates a WebView and a loads URL.
The WebView loads fine if the MapView is hidden (android:visibility="gone"). But when it is made visible, the WebView's onPageStarted() is called but onPageFinished() is never called.
MapView:
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
android:id="#+id/mapView" />
WebView:
webView = new WebView(getActivity());
webView.addJavascriptInterface(this, "scraper");
WebSettings webSettings = webView.getSettings();
webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("page start called");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.out.println("page finished called");
}
});
webView.loadUrl("http://denver.craigslist.org/apa/5436947521.html");
Few Observations:
If I leave the MapView hidden, WebView loads fine.
Only Log message after onPageStarted() - D/cr_Ime: [ImeAdapter.java:587] detach
Nothing in onReceivedError() or any other error callbacks.

I have done this way, it works fine for me.
I have used MapFragment instead of MapView.
fragment_location.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
LocationFragment.java:
public class LocationFragment extends Fragment implements OnMapReadyCallback {
private static View view;
private GoogleMap googleMap;
private double latitude = 23.0300, longitude = 72.5800;
private WebView webView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try
{
view = inflater.inflate(R.layout.fragment_location,null);
MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng mLatLong = new LatLng(latitude, longitude);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLong, 13));
googleMap.addMarker(new MarkerOptions()
.title("MyTitle")
.snippet("MySnippet")
.position(mLatLong));
if(webView!=null){
webView.requestFocus();
}
}
#Override
public void onResume() {
super.onResume();
webView = new WebView(getActivity());
webView.addJavascriptInterface(this, "scraper");
WebSettings webSettings = webView.getSettings();
webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("WebView", "page start called");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("WebView", "page finished called");
}
});
webView.loadUrl("http://stackoverflow.com/");
}
}
In my case onPageStarted & onPageFinished both are getting triggered.
Screenshot:
Edit 1: Actually MapView always take focus while initialization so once MapView initialization process done request focus to WebView.
Hope this will help you.

Android Webview don't support map that's why it's not show.

Related

The progress bar above the Android webview does not stop

progress bar will not stop and will load indefinitely.
Referred Page:
progress bar in android webview
fragment code
public class testFragment extends Fragment {
private WebView mWebView;
private WebSettings mWebSettings;
private ProgressBar progressBar;
public testFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#SuppressLint("SetJavaScriptEnabled")
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testfragment, container, false);
mWebView = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.setWebViewClient(new WebViewClient());
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(false);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(false);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebView.loadUrl("https://stackoverflow.com/");
return v;
}
}
Removing the annotation from the annotation will completely disappear the progress bar.
I also wanted to use #Override public void onProgressChanged (WebView view, int newProgress) on the page referenced, but it did not appear in the available methods.
The issue is,
you have set 2 web clients and the last statement was overriding the first one. So visible & hide was never executed.
Since in UI, you have set visibility for progress bar as visible .So it is always visible.
Remove mWebView.setWebViewClient(new WebViewClient()); line.
For onProgressChanged()
You have to use WebChromeClient. Since is available in Chrome Client, not on WebViewClient
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
});

the scrolling is working by reverse on WebView

I used WebView in my app and it sees the scrolling is not working properly, when I trying to scroll down the page does not go down and the reverse is true, but when I try to touch the screen and get off in reverse "touch with scroll up" It's working!!, more clarification in this images gif1 , gif2
code
public class VisitSiteFragment extends android.app.Fragment {
private View view;
private ProgressBar webViewProgressBar;
private WebView webView;
String url;
public VisitSiteFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_visit_site, container, false);
if (getArguments() != null) {
url = this.getArguments().getString("url");
Log.e("URL", url);
}
webViewProgressBar = (ProgressBar) view.findViewById(R.id.webViewProgressBar);
webView = (WebView) view.findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
webViewProgressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
webView.loadUrl(url);
return view;
}
}
the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".VisitSiteFragment"
android:orientation="vertical"
android:background="#color/white"
>
<ProgressBar
android:id="#+id/webViewProgressBar"
android:layout_width="match_parent"
android:layout_height="8dp"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:theme="#style/progressBarBlue"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progress="20"
/>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
I saw someone asking about this problem here, but the answer in that question did not help
After many attempts of WebView settings, I solved the issue by disabling scrolling that embedded with WebView as explained in this answer, and used ScrollView instead of it
The code after edited
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.setScrollContainer(false);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
webViewProgressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});

"built-in" progressBar technique for Webview on Android

How do I add a loading bar for Webview on Android?
I have the following working Webview code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser (commented out, we'll allow fice URLs only)
//mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://google.com");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
I'm aiming to add a simple prograss bar/loading bar but can't seem to find how to do this.
Try this.
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
// Force links and redirects to open in the WebView instead of in a browser (commented out, we'll allow fice URLs only)
//mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new myWebClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://google.com");
}
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
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Don't forget to Add this in your XML before Web View.
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressBar1"/>
This is your code. try it!!!
Create class MyWebChromeClient.class
public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;
public MyWebChromeClient(ProgressListener listener) {
mListener = listener;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
mListener.onUpdateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
public interface ProgressListener {
public void onUpdateProgress(int progressValue);
}
}
in WebviewActivity.class
public class WebviewActivity extends BaseActivity implements MyWebChromeClient.ProgressListener {
private ProgressBar mProgressBar;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
mWebView = (WebView) findViewById(R.id.web_view);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebChromeClient(new MyWebChromeClient(this));
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
});
}
#Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.GONE);
}
}
}
this is webview_layout.xml
<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">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="#dimen/progress_bar_height"
android:progressDrawable="#drawable/bg_progress_bar_history" />
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progressBar" />
</RelativeLayout>
create bg_progress_bar_history.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#android:id/secondaryProgress">
<scale
android:drawable="#drawable/progress_bar_second"
android:scaleWidth="100%" />
</item>
<item android:id="#android:id/progress">
<scale
android:drawable="#color/color_bg_splash"
android:scaleWidth="100%" />
</item>
</layer-list>
you should implement custom WebViewClient
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new CustomWebViewClient());
mProgressBar.setVisibility(View.VISIBLE); // here show your progress view
mWebView.loadUrl("http://google.com");
and custom WebViewClient:
private class CustomWebViewClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
}); // here hide progress view
}
}

WebView cannot load webpage inside

I wanna load webpage inside fragment, but there is some problem.
1.without overriding shouldOverrideUrlLoading method, the website is open with default browser Chrome.
2.when overriding shouldOverrideUrlLoading, nothing happens. just new blank activity is created.
This is my onCreateView method inside Fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View resultView = inflater.inflate(R.layout.container, container, false);
if (getArguments() != null) {
String url = getArguments().getString(KEY_URL);
WebView productDetailView = (WebView)resultView.findViewById(R.id.webView);
productDetailView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});
productDetailView.loadUrl(url);
}
return resultView;
}
targetSdkVersion is 19 and added INTERNET permission and XML file of Fragment is like below.
<FrameLayout 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.example.ProductDetailFragment">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_gravity="center" />
</FrameLayout>
what's the problem?
Try this code
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
and start your activity with FLAG_ACTIVITY_NEW_TASK
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
webView.setSaveEnabled(false);
String receivedName = (String) intent.getSerializableExtra("USERNAME");
Log.d("receivedName ----- ", receivedName);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" +
receivedName);
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Webview.this.finish();
}
});
Try this code...
Try this.
final ProgressDialog pd = ProgressDialog.show(ActivityA.this, "", "Loading...", true);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("<url");
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
Have you tried adding this <uses-permission android:name="android.permission.INTERNET" />
into Manifest file ?
https://developer.android.com/reference/android/webkit/WebView.html

Preventing WebView from closing when clicked outside the webview

In my app, I load a WebView where users enter username/password and I don't want the webview to close should the users accidentally click outside of the webview since the page will have to make another network call to load this webview.
I know this can be done for Dialog (like so: dialog.setCanceledOnTouchOutside(true);) but couldn't find similar mechanism for webview.
Thanks!
Here is the code:
#SuppressLint("SetJavaScriptEnabled")
public class LoginDialogFragment extends DialogFragment {
private static final String TAG = LoginDialogFragment.class.getSimpleName();
View mainView = null;
WebView webView = null;
OnLoginSucceeded loginSucceeded = null;
ProgressBar progressBar = null;
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.webview, container, false);
if (getDialog() != null)
getDialog().setTitle(getString(R.string.login));
webView = (WebView) mainView.findViewById(R.id.webview);
//display the progress bar
progressBar = (ProgressBar) mainView.findViewById(R.id.user_profile_loading_progress);
progressBar.setVisibility(View.VISIBLE);
//turn caching off
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG, "URL received in onCreateView's onPageStarted is: " + url);
new HandleRequest().execute(url);
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
new FetchLoginFormTask().execute();
return mainView;
}
I found my answer here and I just used getDialog().setCanceledOnTouchOutside(false); ensuring that when back key is clicked, the webview is dismissed.
Try with override the method:
#Override
public void setCancelable(boolean cancelable) {
super.setCancelable(false);
}
If your issue is not getting solved by this then please put some code so i can help you.
Feel free for comment.

Categories

Resources