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.
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));
Hi I have totally no idea why my webview is not call the onPageFinished method, and the it was showing the loading dialog all the time, I hope there is someone out there help me for my this issue. In the Android manifest file I already put the internet permission and also the network access state.
so this is my code:
public class MainActivity extends AppCompatActivity {
WebView mWebView;
RelativeLayout rLoading;
Map<String, String> header;
String urlToLoad;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
rLoading = (RelativeLayout) findViewById(R.id.loading);
if ( isNetworkAvailable() == true ) //check if internet available or not
{
mWebView.setFocusable(true);
mWebView.setFocusableInTouchMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
urlToLoad = "https://www.google.com/";
UsernamePasswordCredentials creds= new UsernamePasswordCredentials("username", "password");
Header credHeader = BasicScheme.authenticate(creds, "UTF-8", true);
header = new HashMap<String, String>();
header.put(credHeader.getName(), credHeader.getValue());
//show loading dialog
mWebView.loadUrl(urlToLoad, header);
mWebView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
rLoading.setVisibility(View.GONE);
}
});
}
else //Not connected
{
Toast.makeText(
this,
"No internet connection",
Toast.LENGTH_LONG
).show();
}
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivityMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityMgr.getActiveNetworkInfo();
/// if no network is available networkInfo will be null
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()){
mWebView.goBack();
}else {
super.onBackPressed();
}
}
}
Someone please help me
Try this Android WebView implementation:
webview= (WebView) findViewById(R.id.webview);
webview.loadUrl("www.google.com");
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//stop the ProgressBar
mProgressBar.setVisibility(View.INVISIBLE);
}
});
You can do it by this way
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
}
});
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
}
});
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();
}
}
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);
}
}