I’m trying to put a button at the bottom of a webview. For some reason, it keeps showing up at the top. Here’s my code:
webview = new WebView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
Button myButton = new Button(this);
myButton.setId(11);
LayoutParams buttonParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
myButton.setText("A Button!");
myButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
myButton.setLayoutParams(buttonParams);
webview.setLayoutParams(params2);
//setBackgroundcolor(0) - You must make the background color transparent before setting backgroundResource
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.background);
webview.addView(myButton);
Edit —
I’m gonna go ahead and set up the base of my reusable view in xml. I’ll just use ScrollView with a RelativeLayout inside of it (containing my WebView and Button). The reason is because I only want the Button to show up once I reach the end of the webpage.
WebViewextendsAbsoluteLayout, so you can’t useRelativeLayout.LayoutParams. Moreover,AbsoluteLayoutis deprecated, andWebViewis definitely not a view to be used as a container.You can overlay a
RelativeLayoutover theWebViewputting them in aFrameLayout, they’ll get piled up, with the last view added (your RelativeLayout) at the top.