android: how to let js code to change the webview's size

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



android: how to let js code to change the webview's size



I'm developing an android application with API 19(android4.4).I have a linearLayout view nested another linearLayout view. Now I want change this linearLayout's height by js code, but it doesn't work.



active_main.xml


<LinearLayout xmlns="...." ...
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@id/sContainer"
tools:context=".MainActivity">
</LinearLayout>



java test code:


...
private LinearLayout webViewContainer;
...
private void initView()
LinearLayout container=(LinearLayout)findViewById(R.id.sContainer);
//add a child linearLayout
webViewContainer=new LinearLayout(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,200);
webViewContainer.setLayoutParams(layoutParams);
//add a child webview in this linearlayout
WebView webview=(WebView)new WebView(this);
LinearLayout.LayoutParams webParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
webview.setLayoutParams(webParams);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JsInteraction(), "bridge");
webView.loadUrl("http://xxxx/index.html");
webView.setWebViewClient(new WebViewClient()
@Override
public void onPageFinished(WebView view, String url)
view.loadUrl("javascript:callHtml('show')");

);
webViewContainer.addView(webview);
container.addView(webViewContainer);



class JsInteraction
@JavascriptInterface
public void expand()
LinearLayout.LayoutParams layoutParams=webViewContainer.getLayoutParams();
layoutParams.height = 500;
webViewContainer.setLayoutParams(layoutParams);
Log.v("height", layoutParams.height+"");




index.html js code


<script>
function callHtml(method)
case 'show':
setTimeout(function()
bridge.expand();//call app to expand after 5s
,5000);
break;

</script>



log shows that layoutParams.height has changed to 500, but nothing happens in my emulator, something I make wrong?
In addition, if i click a button to change the layoutParams's height, it works.





May I know the reason for this I'm developing an android application with API 24(android4.4)? Why you are not using latest SDK and tools?
– VicJordan
Aug 8 at 1:58





i just test some function, it doesn't work at API 19 too
– cityvoice
Aug 8 at 2:25




2 Answers
2



That's "probably" because you haven't called requestLayout which is needed after you make layout changes (the view's layout gets invalidated)


requestLayout



Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.



So, after changing the height, you should call requestLayout...


requestLayout


linearLayout.requestLayout();





I add this code ,it doesn't work too. It seems that if user interaction with app , it works, but if the code initiates the change size action , it doesn't work.
– cityvoice
Aug 8 at 2:41



I have resolved this issue, just change JsInteraction code to below:


class JsInteraction
@JavascriptInterface
public void expand(final int height)
runOnUiThread(new Runnable()
@Override
public void run()
//change size here









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard