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.
Related
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();
}
}
I'm trying to change the position of swipeRefresh progress and make it in center of the mobile screen but it always appear on the top,
I tried using this code to put the swipeRefresh on center but it didn't work
android:layout_centerVertical="true"
Here is my layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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:background="#drawable/bg">
<ProgressBar
android:id="#+id/awv_progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="ss"
android:layout_marginTop="150dp"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-2615894909216466/9780888430">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
main activity java code:
public class my_activity extends Activity {
WebView webView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
swipe = findViewById(R.id.swipe);
swipe.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
swipe.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
swipe.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect rect = new Rect();
swipe.getDrawingRect(rect);
swipe.setProgressViewOffset(false, 0, rect.centerY() - (swipe.getProgressCircleDiameter() / 2));
}
});
AdView mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
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 onLoadResource(WebView view, String url) { //Doesn't work
//swipe.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
//Hide the SwipeReefreshLayout
progressBar.setVisibility(View.GONE);
swipe.setRefreshing(false);
}
});
}
public void LoadWeb() {
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("http://promotions.panda.com.sa/flipbook/Zone3/");
swipe.setRefreshing(true);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}}
Using setProgressViewOffset to change position of refresh indicator.
To display the refresh indicator at center of SwipeRefreshLayout.
final SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe);
swipeRefreshLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
swipeRefreshLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
swipeRefreshLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Rect rect = new Rect();
swipeRefreshLayout.getDrawingRect(rect);
swipeRefreshLayout.setProgressViewOffset(false, 0, rect.centerY() - (swipeRefreshLayout.getProgressCircleDiameter() / 2));
}
});
Use setProgressViewOffset from SwipeRefreshLayout. It basically gives you the ability to decide where to start and and end animation vertically.
I realized that when we setup rect from SwipeRefreshView, the attributes wasn't initialized. So, to solve this problem we can get the device's height from windowManager rect, that is a public fun of Activity.
private companion object {
private const val SCREEN_FACTOR = 2
}
Java ->
public Int fun center(){
return getWindowManager().getMaximumWindowMetrics().getBounds.bottom/SCREEN_FACTOR
}
Kotlin ->
private val center
get() = windowManager.maximumWindowMetrics.bounds.bottom/SCREEN_FACTOR
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;
}
}
}
I'm loading one URL in WebView, the URL page contains functionality of capturing image from device camera OR select image from gallery. If I open URL in Browser, everything works fine but, if I open the same URL in WebView then Camera and Gallery functionality do not works. I think I'm missing some WebView setting. Please check my below code and help me to solve my problem.
Below is my WebViewActivity--
public class WebViewActivity extends AppCompatActivity
{
WebView myWebView;
ProgressBar myProgress;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
findViews();
}
private void findViews()
{
myWebView= (WebView) findViewById(R.id.myWebView);
myProgress= (ProgressBar) findViewById(R.id.myProgress);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
//Other webview settings
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(SafeNestConstants.MEDLIFE_URL);
}
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);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
myProgress.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);
myProgress.setVisibility(View.GONE);
}
}
}
Below is my activity_my_webview.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:id="#+id/activity_terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.WebViewActivity">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/myProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/progress_medium"
android:visibility="visible" />
</RelativeLayout>
Also let me know if I can provide more information for the same. Thank you.
I have 5 buttons and i need to open different webpages after buttons is clicked. How to make it?
my java code for webview activity:
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView v, String url)
{
v.loadUrl(url);
return true;
}
}
And 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"
android:orientation="vertical"
tools:context="lv.shit.test.Sakums" >
<webview android:id="#+id/manswebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</webview>
</linearlayout>
What should I write on OnClick to open webpages in my webview?
You can load different pages on your webView object on the click.
as
webView.loadUrl("http://googlecom");
You can pas the url to the method loadUrl() under the webView object to preform the loading of different urls.
Call this method inside the onClick listener:
loadWebView(url);
public void loadWebView(String url){
/*
* Setting the options of my webView
*/
mWebView = (WebView)findViewById(R.id.manswebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mWebView.loadUrl(url);
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
});
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
//when finish loading page
public void onPageFinished(WebView view, String url) {
}
});
//done loading now render the content
this.setContentView(mWebView);
}