Android - Hiding TitleBar after WebView Loads - android

I currently have a WebView in an app I am working on and after each page loads in the WebView I would like to hide the TitleBar. I have looked at this question and the answer looks like it should do exactly what I want, however the TitleBar is never hidden after the page finishes loading.
The code I am using is the same as the answer I have linked to, but I have also included it bellow. I can confirm that onProgressChanged is being called and that progress does get reported as 100 when the page finishes loading, but setProgressBarIndeterminateVisibility(false) and setProgressBarVisibility(false) both seem to do nothing. I am using requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) and requestWindowFeature(Window.FEATURE_PROGRESS) before I sent content.
I have tested this on Android 2.3 and 4.1 and the result is the same for both, the TitleBar is not hidden.
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
setProgress(progress * 100);
if(progress == 100) {
setProgressBarIndeterminateVisibility(false);
setProgressBarVisibility(false);
}
}
});

I created a android web browser targeted towards android 2.3 and ran into this same problem.
The solution is to add this line of code somewhere in your main activity.
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
make sure you put this line before your setContentView() method or your app will force close.

The old title bar is somewhat limited in functionality. You will probably have to make your own title bar within the layout of the activity and show/hide it yourself with setVisibility().
If you are using the ActionBar instead of the old title bar (which is what people should be doing anyways), it has show() and hide() methods you can use to do what you want.

public void onProgressChanged (WebView view, int progress)
{
if(progress<100 && pb.getVisibility()==pb.Invisible)
{
pb.setvisibility(pb.Visible)
}
pb.setProgress(progress);
if(progress==100)
{
pb.setVisibility(ProgressBar.INVISIBLE);
}
*where pb =(ProgressBar)findViewById(R.Id.progressbar);
its working correctly for me
hope you got my code simple and sweet.....

Related

Android: after loading data many times in webview appear white page

when I load some data in web view many times that white page appears ...just scroll it the data is return back..Am notice this issues on android 4.x ...I test it in android 2.3.3 webview work fine
what I can do to prevent this issues ?
I am facing a similar issue and finally solved but I am not sure why. Things that make it work was:
1)
When I finish with a WebView onDestroy of activity/fragment I have:
#Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
if (myWebView != null) {
myWebView.destroy();
myWebView = null;
}
}
}
2)
I render the code via myWebView .loadDataWithBaseURL("file:///android_asset/", html_sourse, "text/html", "UTF-8", null); instead of myWebView.loadData(Uri.encode(html_sourse), "text/html", "utf-8");. When I am using the second line with an embedded css stylish inside html code page does not appear, changing to the first one (loadDataWithBaseURL) and adding to the html code a css from asset folder page appeared. I cannot understand why but works for me!
3) (optional)
Sometimes it is helpful to use clears if you do not care about keeping history:
myWebView.clearHistory();
myWebView.clearCache(true);

How to setVisibility(View.VISIBLE) to a webView AFTER its loaded its contents?

In my app there is an advertising web view, that is calling an url when the activity starts.
Everything is nice except one minor thing, it is more like a visibility issue...
So the thing is when i start the activity i literally see the page loading.. About in 0,3 seconds the texts appear then the pictures, and all the content somehow floating in from left to right. It not so nice, i would like to set the visibility of my webView to VIEW.GONE until it is done with the loading, and after that i could set it VISIBLE...
Is there a good, and working way for achieve this?
It is important that it is must work on Android 2.3 to 4.0 in each and every OS version.
I already tried onPageFinished and PictureListener related to this stack question here, but no hope, onPageFinished only works properly on 4.0 and PictureListener is for pictures which is not the best way to do this in case of different advertisings with different number of pictures. (and what if there is no picture at all...)
Please if you know a way let know with me. Thanks all.
have you tried WebChromeClient ?
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress == 100) {
// dismisses the PD
dismissLoadingDialog();
}
}
});
webView.loadUrl("your-Url");
// show message dialog here
createLoadingDialog();
i have used this on 2.3 to 4.0 OSes without any issue
Use webview visibility during onPageFinished method
view not display util all page loading.
webView1.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView1.setVisibility(View.VISIBLE);
}

How to know when WebView rendering is finished

I want to display a progress bar while the WebView is loading. I am hiding it on OnPageFinished(), but this is too early. The Webview is still rendering the image.
The WebView documentation states: "When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture)."
However, OnNewPicture and the PictureListener interface is deprecated and obsolete. Is there another way to know that the rendering is complete?
Unfortunately, I have found that there is no event or mechanism to truly know when the page is completely loaded and finished rendering.
How about creating a custom WebChromeClient and overriding its onReceivedIcon() or onProgressChanged() methods. The onReceivedIcon() will be triggered once the favIcon is loaded and and using the onProgressChanged(), you can enquire the progress of the webview loading. I hope this helps
Please refer to this code snippet
webView.setWebChromeClient(new CustomWebChromeClient());
You can use onProgressChanged or onReceivedIcon whatever that suits your needs.
public class CustomWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress==100) {
progressBar.setVisibility(View.GONE);
progressBar.setProgress(newProgress);
}
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
// icon received here
super.onReceivedIcon(view, icon);
}
}
I think you can add and execute JavaScript function at the end of body tag of your web page to call your JavaScript interface function to hide the progress bar. But, beware of enabling JavaScript on your WebView.
I'm suffering same problem, but there is no way to know the timing.
The only way I use is to give enough room for the rendering completion by postDelayed().
In my case 200ms is enough if u don't use heavy contents more than 200K in text.

Is there a way to check if a WebView content is visible to user?

In my application I need to show a progress dialog when downloading and loading content to WebView. I know about the onPageStarted and onPageFinished methods, it works, however, there is a few seconds of delay between the onPageFinished call and the time the content is actually visible on the screen.
Is there a way to dismiss the progress dialog when the content is really visible, not only loaded?
You can do this by adding a PictureListener to your webview. Note that this is deprecated, but as far as I know there is no actual replacement provided by Android at this time.
From API level 23 you can use onPageCommitVisible.
#Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
// There you go.
}
Unfortunately I have not found any way to use it on lower API levels.

Remove process dialog when page starts loading?

I am using a webview in an application and currently when webview starts, a dialog appears showing "please wait, loading" and remains there on screen until every single dot on the web page is loaded. which obviously makes it seem working too slow. Now i want is to dismiss the dialog when webview has something on it and remaining may keep on loading, so that app may become a little more responsive. what should i do....????
Here's a great tutorial on adding a progress bar to the top of your webview:
http://longweekendmobile.com/2011/08/20/custom-progress-view-on-android-webview/
To apply this to your needs, just dismiss the progress dialog prior to the progress reaching 100.
Something like this:
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress >= 50){
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
}
});
Adjust the if statement with 50 to whatever value you want. This code is untested and should just be used to give you the idea.

Categories

Resources