Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align addContentView at the screen's bottom in Android

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.

like image 201
Nikitas Avatar asked Dec 03 '22 08:12

Nikitas


2 Answers

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

like image 130
ol0 Avatar answered Mar 05 '23 02:03

ol0


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

like image 30
Sunil Kumar Sahoo Avatar answered Mar 05 '23 01:03

Sunil Kumar Sahoo