I would like to ask something about the addContentView()
command.
I have created a custom view apparted from a LinearLayout(fill parent, wrap content)
in vertical mode and some buttons.
My question: Is it possible to place my custom view at the bottom of the screen by using the addContentView()
command?
I now use addContentView()
but my custom view is placed at the top of my screen.
I have already tried to change the height of the custom view in fill parent but then I have a full screen custom view.
Solution is to wrap your custom view in a RelativeLayout that fills whole screen and then add it with addContentView. For example to add a custom Button on the bottom of the screen:
// Fake empty container layout
RelativeLayout lContainerLayout = new RelativeLayout(this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ));
// Custom view
Button mCustomView = new Button(this);
mCustomView.setText("Test");
LayoutParams lButtonParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
lButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mCustomView.setLayoutParams(lButtonParams);
lContainerLayout.addView(mCustomView);
// Adding full screen container
addContentView(lContainerLayout, new LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ) );
This should make it
Try with the folowing code
LayoutParams lp =new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.BOTTOM;
addContentView(b, lp);
Using RelativeLayout
also you can achieve same. You can add rule to set view at the bottom of parent layout.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With