scrolling to the top of a webview (Android) - java

I have a webview loading a big texts after the user has clicked an object.
I like the text to be shown from the top. But if the user scrolled down in the previous text the next is already scrolled and not shown from the top.
I am using webView.scrollTo(0,0); in my onPageFinished event of the webview.
This works fine in my emulator, but on my phone the text is scrolled in the middle.
when adding a pause with Thread.sleep it also works on my phone.
What am I doing wrong here ?
My Code:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
webView.scrollTo(0,0);
super.onPageFinished(view, url);
}
});

Try with this block of code -
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
view.scrollTo(0,0);
super.onPageFinished(view, url);
}
});
Actually, you're so close to the solution. Just the point is you need to set scrollTo in the returned view of onPageFinished method.

Related

WebView - only show back button if back history exists

I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.
The button is initially hidden and should only show if a back history exists.
How can I realise this?
The code is simple:
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
but where do I have to call this? How is the "some site loaded" event called?
Update:
I tried to apply #Murats answer, but nothing happens:
private WebView blizzView; // my webview
private Button backButton;
public void onPageFinished(WebView view, String url)
{
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished
It works like this:
blizzView.setWebViewClient(new WebViewClient() { //
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
});

Get callback from a url loading inside webview

I am loading an Url from that page, we do some action and redirect to a New page inside WebView. now I want to get Callback when redirected page finishes loading.
I have tried
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
L.d(url);
if (url.contains("Thank-you")) {
Utility.showToast(TabsActivity.this, "Subscription Done");
}
}
and also override
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
L.d(url);
view.loadUrl(url);
return true;
}
but I am still not able to get the callback from URL.
a bit of guessing is needed here, but I still believe, that you are try to find the string "Thank-you" in the url, but the url is written in lower case. Also you could try using WebChromeClient and then the onProgressChanged method.
Regards Tarik.
Register WebViewClient on your webview to return page finish listerner like this:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

Playing youtube video using webview in an android app

I am trying to play an youtube video inside an android app by using the url of the video.I have tried using the following code:
new Thread(new Runnable() {
#Override
public void run() {
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
try {
myWebView.loadUrl(icobj.video_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
It works, but the problem is that there is a black screen shown for about 4 seconds before the page opens.How do i get rid of the black screen before the page opens?
Check this link. You can listen the webview and show a placeholder or progress bar until loaded. It is all up to you.

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
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://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

Android webview not loading images with site using cookie

I am loading a SharePoint page into a webview using LoadUrl. All the other content comes down but not the images. The site requires a cookie to login but that part is working fine its just once you get past the login page the next page is correct minus the images.
String myUrl = "https://www.mysite.com/";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(myUrl, cook);
CookieSyncManager.getInstance().sync();
webView = (WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient()
{
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
{
handler.proceed();
}
public void onProgressChanged(WebView view, int progress)
{
}
});
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true); webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webView.loadUrl(myUrl);
You might have the same problem as I do.
Is it possible that the picture source require authentication? You can try to open the sources separately and see if you can access them or not.
I did the same and found out that the image sources I wanted to show are protected via a cookie and right now I'm looking for a solution to send this cookie along but couldn't find a way yet.
When I read you question that is exactly the same you try to do.
( sorry to write this here but I don't have enough points to write a comment )

Categories