Android Question inline java / webchromeclient

FrankDev

Active Member
Licensed User
Longtime User
Hello

Hope you are all well !

I have an extension for WebchromeClient but don't know how to implement it in B4a.

Best regards
Frank

B4X:
public class MainActivity extends AppCompatActivity {
   private WebView webView;
   private FrameLayout framelayout;
   final Activity activity = this;
 
   @SuppressLint("SetJavaScriptEnabled")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
          
       webView = (WebView) findViewById(R.id.webView1);
       framelayout=findViewById(R.id.framelayout);
       webView.getSettings().setJavaScriptEnabled(true);
 
      String url="https://www.youtube.com/";
      webView.loadUrl(url);
       webView.setWebViewClient(new WebViewClient());
       webView.getSettings().setJavaScriptEnabled(true);
       webView.setWebChromeClient(new WebChromeClient());
       webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Win64; x64; rv:46.0) Gecko/20100101 Firefox/68.0");
       webView.getSettings().setGeolocationEnabled(true);
       webView.getSettings().setDomStorageEnabled(true);
       webView.getSettings().setDatabaseEnabled(true);
       webView.getSettings().setSupportMultipleWindows(true);
 
       webView.getSettings().setBuiltInZoomControls(true);
       webView.getSettings().setSupportZoom(true);
       webView.setInitialScale(0);
 
 
       final Activity activity = this;
 
       webView.setWebChromeClient(new WebChromeClient() {
 
           public void onProgressChanged(WebView view, int progress) {
               activity.setProgress(progress * 1000);
           }
       });
 
 
       if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
               != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.CAMERA}, 50);
       }
       webView.setWebChromeClient(new WebChromeClient() {
 
           @TargetApi(Build.VERSION_CODES.LOLLIPOP)
           @Override
           public void onPermissionRequest(final PermissionRequest request) {
               request.grant(request.getResources());
           }
 
           public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
               super.onShowCustomView(view,callback);
               webView.setVisibility(View.GONE);
               framelayout.setVisibility(View.VISIBLE);
               framelayout.addView(view);
           }
           public void onHideCustomView () {
               super.onHideCustomView();
               webView.setVisibility(View.VISIBLE);
               framelayout.setVisibility(View.GONE);
           }
       });
 
   }
}
 

eps

Expert
Licensed User
Longtime User
So you're just trying to improve the compatibility of using Youtube in a browser in Android?

B4X:
       webView.setWebViewClient(new WebViewClient());
       webView.getSettings().setJavaScriptEnabled(true);
       webView.setWebChromeClient(new WebChromeClient());
       webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Win64; x64; rv:46.0) Gecko/20100101 Firefox/68.0");
       webView.getSettings().setGeolocationEnabled(true);
       webView.getSettings().setDomStorageEnabled(true);
       webView.getSettings().setDatabaseEnabled(true);
       webView.getSettings().setSupportMultipleWindows(true);

       webView.getSettings().setBuiltInZoomControls(true);
       webView.getSettings().setSupportZoom(true);
       webView.setInitialScale(0);

So all of the above?

Have you looked at this? https://www.b4x.com/android/forum/threads/webviewsettings.12929/ it should cover most of those
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Hello,

actually it's about the lower part,
the upper area can be reached with the libs of @warwound
realize.

it's actually about displaying the fullscreen mode at webview



B4X:
final Activity activity = this;
 
       webView.setWebChromeClient(new WebChromeClient() {
 
           public void onProgressChanged(WebView view, int progress) {
               activity.setProgress(progress * 1000);
           }
       });
 
 
       if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
               != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.CAMERA}, 50);
       }
       webView.setWebChromeClient(new WebChromeClient() {
 
           @TargetApi(Build.VERSION_CODES.LOLLIPOP)
           @Override
           public void onPermissionRequest(final PermissionRequest request) {
               request.grant(request.getResources());
           }
 
           public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
               super.onShowCustomView(view,callback);
               webView.setVisibility(View.GONE);
               framelayout.setVisibility(View.VISIBLE);
               framelayout.addView(view);
           }
           public void onHideCustomView () {
               super.onHideCustomView();
               webView.setVisibility(View.VISIBLE);
               framelayout.setVisibility(View.GONE);
           }
       });
 
Upvote 0

eps

Expert
Licensed User
Longtime User
ah okay - so it may well depend on how you have laid out your Frame and browser I guess..

You could always execute the javascript in the HTML page you use or use JavaObjects such as those below (those are example ones)

B4X:
    Dim jo As JavaObject = Activity
    jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
     Array As Object(0x20))
    ActivityParent = jo.RunMethodJO("getParent", Null)


    Dim jo As JavaObject = wv
    Dim settings As JavaObject = jo.RunMethod("getSettings", Null)
    Dim r As Reflector
    r.Target = settings
    r.RunMethod2("setAllowUniversalAccessFromFileURLs", True, "java.lang.boolean")
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
I think it is not about executing javascript in the HTML browser but

Adding Java code to an Activity ?

but I do not know how to

e.g.

B4X:
webView.setWebChromeClient(new WebChromeClient() {
 
           public void onProgressChanged(WebView view, int progress) {
               activity.setProgress(progress * 1000);
           }
       });

add to the activity that this is also possible with the lib of @warwound
cooperates

Translated with www.DeepL.com/Translator (free version)
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Is this HTML local? i.e. something you've produced yourself?

Can you share some of the HTML? Is the javascript inline or in a separate resource?
 
Upvote 0
Top