I'm loading a WebView with application context, in the background, so that when the activity opens, we won't have a time delay of loading the webview.
I have a very simple html file, loaded in the webview, with select component :
<select>
<option value="name1">value1</option>
<option value="name2">value2</option>
<option value="name3">value3</option>
</select>
I initiate the webview in the background, with application context
WebView webView = new WebView(getApplicationContext());
webView.setJavaScriptEnabled(true);
webview.loadUrl("https://www.google.com");
And open it in the activity once its ready :
LinearLayout root = new LinearLayout(this);
root.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
root.setOrientation(LinearLayout.VERTICAL);
root.addView(webView);
setContentView(root);
The webview opens in the activity, BUT, the select components don't open the alert, for picking from the dropdown. The reason for it is - Alert only works with an activity context.
Here's what i tried so far :
- Trying to update the context of a view is not possible.
- Coping a view - not possible.
- Initiating the webview with another activity, and passing it to my current activity - doesn't work as well.
- Tried calling invalidate, reload - doesn't work.
- Init WebView in onCreate() of activity :
WebView webView = new WebView(this);
this works, but it takes time for the webview to load, and its not my intention. - The fact that i'm loading the WebView programmatically, and not setting it with xml layout - it's not the problem here.
- i saw this solution already : https://stackoverflow.com/a/28030088/5130239 , it didn't work either.
I want to stress out, that a solution of sort : don't load the webview in the background is not possible for me, so please, don't suggest it, i'm looking for something that fits my requirements.