Side Bar of WebView not Expanding - android

I am implementing a WebView to load a web page for an app. I have set up everything correctly but the issue i have is that the side bar for the website in the webViewis not coming up. The icon shows quite alright and also responds to clicks but it doesn't come out or expand. I tried loading the website in a Chrome mobile browser and it worked well. What could be wrong with my code? Please someone help me out.
MainActivity.xml
<?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"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_alignParentTop="true"
android:id="#+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/my_web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private ProgressDialog mProgressDialog;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.my_web_view);
loadWebView();
//implementing pull to refresh
swipeRefreshLayout = findViewById(R.id.pull_to_refresh);
swipeRefreshLayout.setRefreshing(true);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
loadWebView();
}
});
}
private void loadWebView() {
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading");
if (!swipeRefreshLayout.isRefreshing()){
mProgressDialog.show();
}
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
showToast();
}
});
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
swipeRefreshLayout.setRefreshing(false);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
showToast();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return super.shouldOverrideUrlLoading(view, request);
}
});
myWebView.loadUrl("https://app.gerocare.org/doctor");
}
private void showToast() {
Toast.makeText(this, "connection error", Toast.LENGTH_SHORT).show();
}
}

Related

Webview not start at top inside nestedscrollview

I made a simple webview app which holds webview inside nestedscrollview. When I took to Facebook and scroll to the bottom and click on see more stories. Next page starts at bottom or middle, not at the top.
What to do? I don't want to remove nestedscrollview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/webview"
/>
</android.support.v4.widget.NestedScrollView>
MainActivitiy.java
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webview);
webView.loadUrl("https://www.facebook.com");
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view,
int newProgress) {
if (newProgress >= 100) {
webView.scrollTo(0,0);
}
super.onProgressChanged(view, newProgress);
};
});
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url)
{ super.onPageFinished(view, url);
view.scrollTo(0,0);
}
});
webView.setPictureListener(new PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture) {
webView.scrollTo(0, 0);
}
});
}
}
This particular answer contains the information you need:
https://stackoverflow.com/a/9392142/2698179
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
webView.scrollTo(0, 0);
}
}
);
Will scroll to 0,0 on the site when the page is finished loading, but is apparently unreliable, the answer contains an alternative:
webView.setPictureListener(new PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture) {
webView.scrollTo(0, 0);
}
});
Either one of these will move the webview to the top left of the page when loading is complete.

how to start third activity if url is same in webview

how to start third activity if url is same in webview
for example i have webview
in my webview if url is like this
http://example.com/access.html
then start thirdactivity
how can i do this please help me to fix this issue
thanks
in advance
here is my webview code
public class SecondActivity extends AppCompatActivity {
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String url = getIntent().getStringExtra("url");
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new WebViewClient());
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl(url);
}
}
here is xml file of secondactivity
<?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:background="#color/colorPrimaryDark"
tools:context="com.shuvro.barcodescanner.BarcodeScannerActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap
favicon) {
super.onPageStarted(view, url, favicon);
// write your logic here.
if (url.equals(pdfWebURL)) {
loadingIndicator.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
loadingIndicator.setVisibility(View.GONE);
}
});
please write your logic in onPageStarted.
Please try below code
public class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String weburl) {
if (weburl.equals("YOURLINK")) {
Intent i = new Intent(getContext(), YourActivity.class);
startActivity(i);
return true;
} else {
view.loadUrl(url);
return true;
}
}
}

How to generate error on Webview?

I developed a website for my entreprisee and I work almost exclusively with PHP
So the Java language (and android studio) is a really new for me
Despite this I have to create an APK to use the website (in order to block the android home on this site)
I would like to know how to generate errors in order to test this function
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
LoadWeb();
progressBar.setMax(100);
progressBar.setProgress(1);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url) {
//Don't show ProgressBar
progressBar.setVisibility(View.INVISIBLE);
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
public void LoadWeb() {
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("mysite.com");
swipe.setRefreshing(true);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
}
One way of doing this could be by adding a proxy(using app like fiddler) which returns custom error code as per your wish for all requests from your device.
In case you do not need specific error codes, just use a random system wide wifi proxy which actually does not exist. In this case, device will try sending data to this proxy but won't find it and you will get a callback in onReceivedError.

How to show ProgressDialog on every click on Link in WebView

I am using webview with ProgressDialog. My issue is that on the Initial Launch, the progressDialog appears abd load properly but whenever I click a link (post links) inside webview, the progress is not showing. Here's the code -
public class Xyz extends WebViewClient
{
public void onLoadResource (WebView view , String url ) {
if (progressDialog == null ) {
progressDialog = new ProgressDialog(getActivity());
progressDialog . setMessage( "Loading..." );
progressDialog.setIndeterminate(true);
progressDialog . show();
}
}
public void onPageFinished(WebView view , String url ) {
if (progressDialog . isShowing()) {
progressDialog . dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
You are overriding the wrong method to achieve your goal. I've put up an Activity here which works as intended.
I'm using onPageStarted() instead of onLoadResource().
Activity MainActivity.java:
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
WebViewClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
}
if(!progressDialog.isShowing()) {
progressDialog.show();
}
}
public void onLoadResource(WebView view, String url) {
}
public void onPageFinished(WebView view, String url) {
if (progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
};
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(mClient);
webView.loadUrl("http://www.google.com");
}
}
Layout activity_main.xml:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.prasad.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Try to track the webview load progress using webChoromeClient like this:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
and replace your webview client as follwing
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Handle loading webclient android

I have problem with web view.
I don't know how to handle loading data( timeout) in web client when I open web success i turn off wifi and continues open web client. it loading forever.(
Here is xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/web_assignment"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/retry_assignment"
layout="#layout/layout_retry_web"
android:visibility="gone"
/>
<com.lusfold.spinnerloading.SpinnerLoading
android:id="#+id/spinnerLoalding_assignment"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<WebView
android:id="#+id/layout_assignment"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
Code is:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
tokenManager = new TokenManager(getApplicationContext());
token = tokenManager.GetTokenID();
if (networkStateReceiver.isConnected(this))
checkTimeToken(token);
else {
setVisibleLayout();
}
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
}
private void setVisibleLayout() {
webView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
spinnerLoading.setVisibility(View.GONE);
btnRetry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("onclick", "Tap reload");
loadweb();
}
});
}
private void checkTimeToken(final String token) {
//
}
private void loadweb() {
final String strUrl = URL_Utils.Assignments_Load + token;
Log.d("URL_asiagnmentss :", strUrl);
if (networkStateReceiver.isConnectedNetwork(this)) {
webView.loadUrl(strUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
spinnerLoading.setVisibility(View.VISIBLE);
webView.setVisibility(View.INVISIBLE);
spinnerLoading.setPaintMode(1);
spinnerLoading.setCircleRadius(20);
spinnerLoading.setItemCount(8);
Log.d("onPageStarted", "onPageStarted");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
spinnerLoading.setVisibility(View.INVISIBLE);
webView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
} else
setVisibleLayout();
}
Firts of all create one Boolean variable default true which track of whether timeout or not.
After that on your WebViewClient's onPageStarted method you have to put one thread which trigger at your given time.
In the last, onPageFinished set variable false
As shown in the code :-
public class MyWebViewClient extends WebViewClient {
boolean timeout = true;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Runnable run = new Runnable() {
public void run() {
if(timeout) {
// do what you want
showAlert("Connection Timed out", "Whoops! Something went wrong. Please try again later.");
}
}
};
myHandler.postDelayed(run, 5000);
}
public void onPageFinished(WebView view, String url) {
timeout = false;
}
}

Categories

Resources