I am loading a web view on the startup of my application.
But before that I am loading a splash screen and I want to show the splash screen until the web view is fully loaded.
How can I do that?
I am working in Android Studio.
You can hide your splash screen when your webpage load complicated using webview client
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// hide your splash screen
}
});
Do like this...
public class SItemsWebView extends ActionBarActivity {
private WebView webView;
private String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_subject_list_items_in_web_view);
this.initViews();
loadWebView(url);
}
//initializes all views
private void initViews() {
webView = (WebView) findViewById(R.id.filesWebView);
}
//show Url in WebView
public class myWebClient extends WebViewClient {
#JavascriptInterface
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//show your splash scree here
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// hide your splash image here
}
}
}
public void loadWebView(String url) {
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setVisibility(View.VISIBLE);
webView.loadUrl(url);
}
}
Try like this,
mWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap icon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, icon);
//show your splash scree here
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
// hide your splash screen
}
});
Related
I have a Web View in Android,and I open a html web page in it.But it's full of links and images,and when I click one of them,it loads in my webview. So I want to disable this behavior,so if I click on a link,don't load it. I've tried this solution and edited a bit for myself, but not worked.
Please give me a solution with full code. I'm new to Java and Android.
My Full code here:
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
WebView webView;
String url="http://example.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
webView.setWebViewClient(new MyWebViewClient());
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
}
}
You need to configure your shouldOverrideUrlLoading method in your MyWebViewClient class to only load a URL should it match your desired URL. Add a field to your MyWebViewClient class to store a URL and add a single-arg constructor.
private String desiredUrl;
public MyWebViewClient(String url) {
this.desiredUrl = url;
}
In your shouldOverrideUrlLoading method, check to see if the supplied URL is equal to your URL instance variable.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (desiredUrl.equals(url))
view.loadUrl(url);
return true;
}
When setting your web view client, pass your URL argument, e.g.
webView.setWebViewClient(new MyWebViewClient(url));
I'm trying to send string as url from Fragment to Activity and load url inside webview inside activity. But I tried a lot and it is not working out.
Here is my Fragment code
intent.putExtra("url", "http://www.ekantipur.com/");
startActivity(intent);
and my webview activity contains
package com.nepalpolice.nepalinews;
public class web extends AppCompatActivity {
WebView webview;
ProgressBar pbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
String url = getIntent().getStringExtra("url");
webview = (WebView)findViewById(R.id.webView1);
pbar = (ProgressBar)findViewById(R.id.progressBar1);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.loadDataWithBaseURL(url, null, null, null, null);
}
public class WebViewClient extends android.webkit.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
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
webview.setVisibility((View.VISIBLE));
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
It shows Progress bar and then blank white screen.
Thanks in advance.
Try like this
Instead of this
webview.loadDataWithBaseURL(url, null, null, null, null);
use like this
web.loadUrl(url);
and make sure, from the intent your getting the correct URL by printing in logs.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://appgeyser.com/");
}
It is very simple. Use this for load url.
this is my mainactivity, when i load some page the progress dialog doesn't disappear onpagefinished...where is the problem?
if you want to try the problem are with the page "accedi" or on the payment of the order
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("http://www.alldrink.it");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient=new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
setContentView(webView);
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
}
public class WebClientClass extends WebViewClient {
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Attendere");
pd.setMessage("Caricamento in corso..");
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
public class WebChromeClass extends WebChromeClient{
}
}
the dismiss() function is working, but your onPageStarted() has run two times, making dialog recreate again and look like it never disappear. Try changing your onPageStarted as:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!pd.isShowing())
{
pd.setTitle("Attendere");
pd.setMessage("Caricamento in corso..");
pd.show();
}
}
I need to get a url when user press somethings in a webview.
I use this code:
webView = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebClient());
MyWebClien
private class MyWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("URL", url);
return true;
}
}
But, if i click something, the event is never called.
Thank you
shouldOverrideUrlLoading(WebView view, String url) will get called on clicking a link on a webview.
usage example:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
// do whatever you want to do on a web link click
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
webView.loadUrl("url to open first webpage on a webview");
Based on this: how to get the current page url from the web view in android
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});
Im trying to add a message on the webview if the page thats trying to load to fails. Is this possible? If so, is it possible for the message to replace the default "the webpage failed to load" screen?
activity file
public class MainActivity extends Activity {
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.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
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);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}