I want my extension to change webview url but where am I wrong

Hey, I want to make a simple pdf webview extension but it always fails

/**  ~~~~~
 * Created with the AppyBuilder Code Editor.
 * This is a template for basic Extension.
 * Modify this template to customize your extension.
 *
 * **** NOTE: DO NOT use a package name. 
 * **** The package name will be created for you automatically.
 * **** Adding a package name will cause a compile error
 */
import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.webkit.WebView;
@UsesPermissions(permissionNames = "android.permission.INTERNET")

@DesignerComponent(version = 1,  description = "This Extension was created with the AppyBuilder Code Editor.<br>" + 
                   "Create your own here:<br><a href='https://editor.appybuilder.com' target='_blank'>https://editor.appybuilder.com</a><br>",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class Webpdf extends AndroidNonvisibleComponent {
    private ComponentContainer container;
    /**
     * @param container container, component will be placed in
     */
    public Webpdf(ComponentContainer container) {
        super(container.$form());
        this.container = container;
    }
  
@SimpleFunction(description = "")
    public void PdfURL(String pdf) {
      WebView webview = (WebView) findViewById(R.id.webview);
      webview.getSettings().setJavaScriptEnabled(true); 
      webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
      
    }

    }
  
}

I am not sure how will google drive load a pdf file which exists in user’s device and + where are you supposed to get that webview?

1 Like

what are you trying to do ?? does findViewById gonna work ?? as i know find by id is given in xml file res. does this id is of webview of kodular ??. and btw how you gonna get the webview with your code

public void Init(HVArrangement layout, String path) {
	FrameLayout frameLayout = (FrameLayout) layout.getView();
	frameLayout.removeAllViews();
	this.webView = new WebView(this.activity);
	frameLayout.addView(this.webView, -1, -1);

	this.webView.getSettings().setJavaScriptEnabled(true);
	this.webView.loadUrl(path);

}

try this

2 Likes

as far i know for doing this you need to use Intent for getting application like google drive pdf viewer not web view @Leaf

I think because you copy the code from anywhere.
FindViewbyId works in AndroidStudio only. In App Inventor you have to make objects and use them instead of finding by ids.
For reference you can refer this code CustomWebView/CustomWebView.java at master · vknow360/CustomWebView · GitHub

Kodular uses Android x libraries, so you don’t need to use AppCompatActivity.

You use this method to find a view that you defined in the android: id XML attribute. In extension development, you should create a view programmatically. So,

WebView webview = new Webview(container.$context());

And to show the WebView in a particular arrangement provided by user, you should get an AndroidViewComponent from the user and then add the webview in that AndroidViewComponent as wangsk789 mentioned.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.