-2

This is my first time developing an app so please bear with me on this. I have a question related to this answer: Android WebView: display only some part of website

It's about this code that is written there:

public class MyWebClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
    return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:your javascript");
    }
}
.........
final MyWebClient myWebViewClient = new MyWebClient();
mWebView.setWebViewClient(myWebViewClient);

I'm having difficulties running this code even after modified like this:

public class MyWebClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        webView.loadUrl("http://tid.mspot.nu");
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("document.getElementsByClassName('white').style.visibility = 'hidden'");
    }
}

final MyWebClient myWebViewClient = new MyWebClient();
mWebView.setWebViewClient(myWebViewClient);

error: class, interface, or enum expected

error: class, interface, or enum expected

Since I'm not a java developer this gets a bit confusing (I mainly work with PHP) and some parts I'm totally familiar with.

Here is my currently working code:

import android.annotation.TargetApi;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setUseWideViewPort(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();// super.onReceivedSslError(view, handler, error);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    return false;
            }
        });
        webView.loadUrl("http://tid.mspot.nu");
    }
    @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);
    }
}

I would love to be able to implement that feature of hiding this btn_white class. I know it's supposed to be an ID which I'll change right now. This is just a button to download the app that I want to hide in the app.

I would love to have some help with this since it's my first time developing anything for Android. Thanks!

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Mattias
  • 11
  • 4

1 Answers1

0

If I understand correctly, you've created a class called MyWebClient in a file called MyWebClient.java. You should not have any code outside of your class, so get rid of the two lines of code outside of the class-level bracket:

public class MyWebClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        webView.loadUrl("http://tid.mspot.nu");
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("document.getElementsByClassName('white').style.visibility = 'hidden'");
    }
}

You're then not using this class within your main activity, instead opting to use an anonymous declaration. So where you are setting up the parameters of webView with this code:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();// super.onReceivedSslError(view, handler, error);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return false;
    }
});

You instead need to declare a new instance of MyWebClient within setWebViewClient like so:

webView.setWebViewClient(new MyWebClient());

This will then execute the hooks that you have defined within MyWebClient.


As an example of declaring your MyWebClient privately, if you're not planning on using it elsewhere:

public class MainActivity extends AppCompatActivity {

// Other activity code

    private class MyWebClient extends WebViewClient {
         // WebViewClient code here
    }
}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • oh no xD i don't have that class MyWebClient class xD the only class i got is "MainActivity.java" thats the only file there is supposed to load ^^ im so stupid, should have seen that the immediately xD – Mattias Jan 25 '19 at 12:21
  • Well, if you want to, you can declare `MyWebClient` as a private class. I'll add an example for you, generally public classes should have their own file, whose filename matches the name of the class. – Michael Dodd Jan 25 '19 at 12:23
  • @Mattias Hi, just checking up, did this answer help you at all? – Michael Dodd Jan 31 '19 at 08:29