(Regarding this question)
I'd like to create a component in React Native that has exactly the same functionality that the native WebView, just overriding some of its methods.
I've followed the tutorials in React Native page, with modules and UI Views (which I think is the correct approach), but I'm not able to get what I'd like.
What I have been able to do:
// .../com/project/permissionwebview/
public class PermissionWebviewView extends LinearLayout{
private Context context;
public PermissionWebviewView(Context context) {
super(context);
this.context = context;
init();
}
public void init() {
inflate(this.context, R.layout.permissionwebview, this);
}
}
And my View manager:
// .../com/project/permissionwebview
public class PermissionWebviewViewManager extends SimpleViewManager<PermissionWebviewView> {
public static final String REACT_CLASS = "PermissionWebviewViewManager";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public PermissionWebviewView createViewInstance(ThemedReactContext context) {
return new PermissionWebviewView(context); //If your customview has more constructor parameters pass it from here.
}
}
The XML of the layout:
// .../res/layout/permissionwebview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<WebView android:id="@+id/permission_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I am able to call it from React Native, but nothing is shown and nothing happens.
What I want is simply a custom element which calls the native one (in this case, WebView
), so it renders exactly the same (same props, same methods), just overriding some of its methods when necessary (in my case, onPermissionRequest()
).
Which would be the correct approach? How (and where) do I invoke the parent/super WebView?
EDIT 1
I think I've made it run, but I don't know if it's the correct approach, as I still don't know how to pass all the parameters from React Native component (<PermissionWebview />
) to the native WebView
element:
// .../com/project/permissionwebview/
public class PermissionWebviewView extends LinearLayout{
private Context context;
private WebView myWebView;
public PermissionWebviewView(Context context) {
super(context);
this.context = context;
init();
}
public void init() {
inflate(this.context, R.layout.permissionwebview, this);
myWebView = (WebView)findViewById(R.id.permission_webview);
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
myWebView.loadUrl("https://staticWebSite.Until/Parameters/Are/Passed");
}
}
Thank you!