Here is my code,
Code:
http://pastebin.com/cHVThsR5
I am having the spinner here but the spinner keeps rotating. How do I let the app know that the page load is finished? and after all making the spinner invisible?
you will need to do something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pb.setVisibility(View.GONE);
}
});
Did you try making it invisible manually?
if (isInternetPresent) {
ImageView NoInt=(ImageView) findViewById(R.id.NoInternet);
NoInt.setVisibility(View.GONE);
ProgressBar pb = this.findViewById(R.id.progressBar); //Get a reference to your ProgressBar
WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://glocalkhabar.com");
pb.setVisibility(View.GONE); //Make it invisible
Related
I have a simple webview app that loads my website. Everything is ok, but when the user clicks on the Instagram icon in bottom of the webpage, I want to open the Instagram app instead of loading the Instagram webpage.
I'm trying to achieve this with shouldOverrideUrlLoading function and webview.geturl("https://www.instagram.com/").
The problem is when the user clicks on the Instagram icon, the URL doesn't change and stays default "http://archclub.ir/".
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://archclub.ir/login");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = webView.getUrl();
Toast.makeText(MainActivity.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
}
return false;
}
});
}}
My problem is: When the user clicks on the Instagram icon on the website, the Toast.makeText(MainActivity.this, "salam", Toast.LENGTH_SHORT).show(); doesn't run.
I've checked this. I've debugged it. The problem is when the Instagram icon is clicked, the webview.geturl is equal to archclub.ir/login and doesn't change to instagram.com but the webview shows the Instagram page.
you were replacing the url over and over.
please replace this block of code instead of shouldOverrideUrlLoading method :
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url = webView.getUrl(); // just omit this line
Toast.makeText(ali.this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if (url.contains("https://www.instagram.com/")) {
Toast.makeText(ali.this, "salam", Toast.LENGTH_SHORT).show();
webView.loadUrl("http://archclub.ir/");
return true;
} else return false;
}
});
I have writing one app with WebView. My problem is the page can't load in real device. When I had run on an emulator, that page can see all images and contents. My code is looked like below.
My Activity
public class MyActivity extends AppCompatActivity {
private WebView myWebView;
private static final String TAG = "TaoBao";
//private ProgressDialog progressDialog;
FloatingActionMenu materialDesignFAM;
FloatingActionButton floatingActionButton1, floatingActionButton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
// webview
myWebView = (WebView) findViewById(R.id.activity_webview);
final WebSettings webSettings = myWebView.getSettings();
//webSettings.setSupportMultipleWindows(true);
//webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
if (Build.VERSION.SDK_INT >= 21) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webSettings.setJavaScriptEnabled(true);
// set javascript and zoom and some other settings
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAllowContentAccess(true);
myWebView.setScrollbarFadingEnabled(false);
// enable all plugins (flash)
webSettings.setPluginState(WebSettings.PluginState.ON);
// Floating Menu
materialDesignFAM = (FloatingActionMenu) findViewById(R.id.material_design_android_floating_action_menu);
floatingActionButton1 = (FloatingActionButton) findViewById(R.id.material_design_floating_action_menu_item1);
floatingActionButton2 = (FloatingActionButton) findViewById(R.id.material_design_floating_action_menu_item2);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
final ACProgressFlower progressDialog = new ACProgressFlower.Builder(this)
.direction(ACProgressConstant.DIRECT_CLOCKWISE)
.themeColor(Color.WHITE)
.text("Loading...")
.fadeColor(Color.DKGRAY).build();
progressDialog.show();
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
// if( URLUtil.isNetworkUrl(url) ) {
// return false;
// }
// if (appInstalledOrNot(url)) {
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// startActivity( intent );
//} else {
// do something if app is not installed
// }
// return true;
view.loadUrl(url);
return false;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
super.onPageFinished(view, url);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(TaoBao.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
myWebView.loadUrl("https://m.intl.taobao.com/");
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}enter code here
main_activity.xml
` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/activity_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>`
`Here is My Screenshot
Test on Real Device Android 4.3
Test on Emulator Android 4.3
On real device can not see any photo and contents from the web. But on emulator can see image and content. How to fix it can see image and content on real device? Please help me.
Best Regards,
STP
I have a WebView app which loads a url, I want to create a Settings Activity where user can customize settings..
My que is, How to make a RadioButton or Chooser which when clicked will open the Mobile version of Site, Similarly if Desktop Ver. is clicked, webview will load Desktop version...
By default webview loads mobile site, if you want to force load desktop site you can use
String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);
use radio button to choose this settings. Use isChecked() to check radio button else load normal webview.
You can try code below
if(rbtnMobile.isChecked){
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
webview.loadUrl(url);
}else{
webview.loadUrl(url);
}
Below is WebView code.
public class WebViewLinks extends Activity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
String Url = getIntent().getExtras().getString("Url");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(Url);
}
public class MyWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can use below code on button click or radio button selection.
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", yourURL);
startActivity(webViewIntent);
see below example.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.mobileview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", mobileURL);
startActivity(webViewIntent);
}else if(checkedId == R.id.desktopview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", deskTopURL);
startActivity(webViewIntent);
}
}
});
This similar SO question didn't help.
I have implemented a simple WebView to my MainActivity which loads a particular webpage on app launch. I have a TextView that shows the loading % of current webpage. The code is as below
public class MainActivity extends AppCompatActivity {
WebView webView;
final String homeUrl = "https://example.com";
String lastAccessedUrl = homeUrl;
WebChromeClient webChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = (WebView) findViewById(R.id.browser);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setLoadsImagesAutomatically(false);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webView.canGoBack();
webView.canGoForward();
webView.setWebViewClient(new MyBrowser());
webView.loadUrl(homeUrl);
webChromeClient = new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
TextView textView = (TextView) findViewById(R.id.loadingPercentage);
textView.setText(""+newProgress);
}
};
webView.setWebChromeClient(webChromeClient);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
lastAccessedUrl = url;
view.loadUrl(url);
return true;
}
}
}
The problem I am facing is, the TextView shows loading % only for the first time the webpage loads. Then it is set to 100 permanently. Then if I click on any link on the first page, the TextView does not start from zero.
Do justify in comments if you vote down.
Any help would be appreciated.
There is no such problem with your code. Make sure you are clicking on a url. If you click on an expand view, it will just expand to show you certain data. It wont load as a new url. Click on say, www.stackoverflow.com and check if it's showing you loading percentage.
Hi i have used following code for built in zoom in zoom out control
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
also i have used code like below
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(sabNZBurl);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
backwardBtn = (Button) findViewById(R.id.btnBackWard);
cancelBtn = (Button) findViewById(R.id.btnCancel);
refreshBtn = (Button) findViewById(R.id.btnRefresh);
forwardBtn = (Button) findViewById(R.id.btnForward);
backwardBtn.setOnClickListener(this);
cancelBtn.setOnClickListener(this);
refreshBtn.setOnClickListener(this);
forwardBtn.setOnClickListener(this);
final class MyWebViewClient extends WebViewClient {
#Override
// Show in WebView instead of Browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
PD.dismiss();
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setSupportZoom(true);
}
}
but still webview does not come with built in zoom in zoom out control can any body help me to solve this problem
Do you have a viewport meta tag in web page to display? If yes and if it contains user-scalable="no" then it is normal, the HTML itself disables zoom. Simply edit the tag like this:
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes, width=device-width" />
Try this, I got answer
WebSettings webSetting = mWebView.getSettings();
webSetting.setBuiltInZoomControls(true);
May be this is what your are searching..
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
try this:-
webview.getSettings().setUseWideViewPort(true);