trying to open in webview a link thats jumps another page - java

yes I searched the site and didn't find something that helps me
because every one are saying use "shouldOverrideUrlLoading"
but that's not helping me - its not working
I am trying to open - In the same page an URL that supposed to open a new page link, but when I press it - nothing happens
I am using it as a fragment
public class FlightFragment extends Fragment {
private WebView webView;
public FlightFragment(){};
private String currentUrl;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.flight_fragment,container,false);
currentUrl = "https://flights.travelor.me/?locale=he";
webView = (WebView)rootView.findViewById(R.id.flightweb);
WebViewClient MyWebViewClient= new WebViewClient();
MyWebViewClient.shouldOverrideUrlLoading(webView, currentUrl);
ourWebViewClient(currentUrl);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new FlightFragment.MyBrowser());
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == keyEvent.ACTION_DOWN) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
webView.loadUrl(currentUrl);
return rootView;
}
public void ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
private class MyBrowser extends WebViewClient {
#SuppressWarnings("deprecation")
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.equals(currentUrl)) {
view.loadUrl(request.getUrl().toString());
}
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
}
}

You are returning false when your url is intercepted, try changing like this
webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
Intent intent=new Intent(CurrentActivity.this,NewActivity.class);
intent.putExtra("url to load in NewActivity",url);
startActivity(intent); //get this url using getextra in new activity
}
}
}});

If your target version is >= 26
then override both the function below
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
Because shouldOverrideUrlLoading(WebView view, String url) is deprecated since Android N. Write the code for Android N and above in the first override method.
In your code remove this
webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
return false;
}
view.loadUrl(url);
return true;
}
}});
as you are setting your WebViewClient again below this code and change your MyBrowser class
private class MyBrowser extends WebViewClient {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}

#golan Try to make your Webview fragment as a Activity which will work like a generic view and will open web pages as per requirement based on input url.
shouldOverrideUrlLoading loading method will required if you want to check the input urls.
Do all required web settings like
WebSettings webSettings = webviewBinding.detailWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadWithOverviewMode(false);
webSettings.setUseWideViewPort(true);
webSettings.setSupportZoom(true);
Override onBackPressed() method with following code:
if (detailWebView.canGoBack()) {
detailWebView.goBack();
}
else {
super.onBackPressed();
}
The canGoBack() will allow you to go back upto all web pages and if no more pages to go back it will redirect to you to last loaded activity or view.

Related

How to access an options menu item from outside the menu

How can an options menu item be accessed from an object outside of the menu itself? I'm trying to set an alpha value for one of the options menu items, but it's not working for some reason. The commented lines are what I tried.
I've seen the following code used before, but it's not clear on where and how it should be used:
Drawable drawable = item.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setAlpha(1);
}
Activity class
public class WebviewActivity extends AppCompatActivity {
private static final String TAG = WebviewActivity.class.getSimpleName();
WebView myWebView;
ProgressBar myProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_webview);
}
#Override
protected void onStart() {
super.onStart();
setContentView(R.layout.fragment_webview);
String url = getIntent().getStringExtra("url");
myWebView = findViewById(R.id.web_view);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
myProgressBar.setProgress(newProgress);
}
});
}
private class WebViewClient extends android.webkit.WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
String url=request.getUrl().toString();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
public void onBtnBackPressed() {
if (myWebView.canGoBack()){
myWebView.goBack();
} else {
onBackPressed();
}
}
public void onBtnForwardPressed() {
if (myWebView.canGoForward()){
myWebView.goForward();
} else {
// menu.getItem(R.id.action_webbrowser_forward).setEnabled(false);
// menu.getItem(R.id.action_webbrowser_forward).mutate();
// menu.getItem(R.id.action_webbrowser_forward).setAlpha(1);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.web_browser, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
else if (item.getItemId() == R.id.action_webbrowser_back) {
onBtnBackPressed();
} else if (item.getItemId() == R.id.action_webbrowser_forward) {
onBtnForwardPressed();
}
return super.onOptionsItemSelected(item);
}
}
Define in your activity class this variable:
MenuItem action_webbrowser_forward;
In onCreateOptionsMenu() after the inflation of the menu, write this:
action_webbrowser_forward = menu.findItem(R.id.action_webbrowser_forward);
then you can use it anywhere in your class:
action_webbrowser_forward.setEnabled(false);
but mutate is used for drawables and not menu items, so get the icon of the item by action_webbrowser_forward.getIcon() and apply mutate to it.
Try passing MenuItem reference on your onBtnForwardPressed function parameters :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
else if (item.getItemId() == R.id.action_webbrowser_back) {
onBtnBackPressed();
} else if (item.getItemId() == R.id.action_webbrowser_forward) {
onBtnForwardPressed(item);
}
return super.onOptionsItemSelected(item);
}
And :
public void onBtnForwardPressed(MenuItem menuItem) {
if (myWebView.canGoForward()){
myWebView.goForward();
} else {
menuItem.setEnabled(false);
menuItem.getIcon().setAlpha(50);
}
}
Hope this helps
Okay I spend couple of hour to get this solution.apparently you can get menuitem from your toolbar. So in my case.
var menuItem = toolbar.menu;
Now to get specfic item from menu item
favIcon = menuItem.findItem(R.id.favourite);
Note: favIcon is MenuItem declare global
Now if you can do whatever you want to do for this icon
eg. to make it invisible
favIcon?.isVisible=false

In android web view the html page is being treated like it is being opened in a big screen rather in mobile device

I am using a webview in my android app. Now the issue is in that webview it is opening like a desktop site not like a mobile platform site.
As for example, when I open the page in webview it is being shown like
And the same page when I am opening in web browser it looks like;
Now my question is I want to open the page in my web view like it is in web browser and I have no idea why it is behaving like that.
My webview code is like;
final WebView webview = (WebView)vi.findViewById(R.id.webview);
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
set.setJavaScriptEnabled(true);
set.setDomStorageEnabled(true);
set.setLoadWithOverviewMode(true);
set.setUseWideViewPort(true);
set.setSaveFormData(true);
set.setJavaScriptCanOpenWindowsAutomatically(true);
set.setBuiltInZoomControls(true);
set.setDisplayZoomControls(false);
set.setSupportZoom(false);
set.setDefaultTextEncodingName("utf-8");
WebChromeClient webClient = new WebChromeClient(){
public void onCloseWindow(Window w){
super.onCloseWindow(webview);
System.out.println("Closed");
}
};
webview.setWebChromeClient(webClient);
webview.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
webview.setInitialScale(1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.loadUrl(url);
I changed the value of webview.setInitialScale(1); but no luck so far.
I can not change the HTML of the pages, so I need to do some kind of changes in webview end.
N.B. The attached images of page is same HTML page opening in browser and webview. PLEASE IGNORE THE BACKGROUND COLOR.
Any help will be very good for me. Thanks in advance.
This works for me
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
initViews();
}
protected void initViews() {
webView = (WebView) findViewById(R.id.webView);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getCertificate();
webView.setWebViewClient(new SSLTolerantWebViewClient());
webView.loadUrl("https://www.google.com");
}
private class SSLTolerantWebViewClient extends WebViewClient {
// ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.d("ddd", "ddd");
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.proceed();
}
}
}
Is your page responsive ? Try opening page in mobile browser. if it is not responsive make it responsive. Make sure you have added viewport meta tag in html like -

How do you disable links, pages and references in a WebView?

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));

WebView not loading after internet permissions granted?

I'm trying to use a WebView to display a webpage in my application. At first, I didn't have any permissions in the AndroidManifest.xml and I got the error 'the webpage at could not be loaded because net::ERR_CACHE_MISS'
But now that I have added the permission to the AndroidManifest.xml, the WebView loads as a empty white box.
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package= ... >
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</application>
</manifest>
And my Java class:
public class location_Fragment extends Fragment {
private WebView webView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
Try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Use The Following Code
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Bundle b = getIntent().getExtras();
String data =b.getString("link");
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
if(data.equals("")||data.equals("null")) {
Toast.makeText(WebViewActivity.this,"No Link Available",Toast.LENGTH_SHORT).show();
}
{
webView.loadUrl(data);
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
// activity.setProgress(progress * 1000);
WebViewActivity.this.setTitle("Loading...");
WebViewActivity.this.setProgress(progress * 100);
if(progress == 100)
WebViewActivity.this.setTitle(R.string.app_name);
}
});
}
private class MyWebViewClient extends WebViewClient {
private ProgressDialog pDialog;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (pDialog == null) {
// in standard case YourActivity.this
pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
pDialog.setMessage("Loading...");
pDialog.show();
}
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
public void onPageFinished(WebView view, String url) {
try{
pDialog.dismiss();
if (pDialog.isShowing()) {
pDialog.dismiss();
pDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

Webview loading dialog

My code below loads a progress dialogue into webview on the initial load but I'd like it to appear every time webview is loading a URL!
I think I need to use onpagestarted() before the progressbar but I'm a bit of a noob at all this and having trouble getting it working. Can anyone advise please?
//need to set progress bar condition here!
progressBar = ProgressDialog.show(MyActivity.this, "Loading title", "Loading...");
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
//web site load
myWebView.loadUrl("http://www.google.com");
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
As shown here: How to listen for a WebView finishing loading a URL?
You should use the onPageStarted() method to show the ProgressDialog:
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.show();
}
public void onPageFinished(WebView view, String url) {
progressBar.hide();
}
});

Categories

Resources