I want to add a linerlayout at the bottom of relativelayout. How could I achieve that?
This is the code snippet that I am using:
rl=new RelativeLayout(this);
ll = new LinearLayout(this);
buttons=new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
ll.setOrientation(LinearLayout.VERTICAL);
//buttons.addRule();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.topMargin=450;
//params.gravity = Gravity.BOTTOM;
rl.addView(ll);
rl.addView(buttons,params);
I think something like this, should work :
rl=new RelativeLayout(this);
buttons=new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(buttons, lp);
Related
Here's my Code
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
addContentView(scrollView, scrollParams);
Of course it works within LinearLayout only. Please tell me how to put it inside a ScrollView.
ScrollView's child should only have height as WRAP_CONTENT. If you try the same layout using xml AS will give you a lint warning saying exactly this.
If you need your child view to occupy full height of scroll view when content is less you could use setFillViewport().
This should work for you:
mainView = new MainView(this, imageId, text, 3);
mainView.setOnTouchListener(this);
LinearLayout.LayoutParams mainParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout mainLinear = new LinearLayout(this);
ScrollView.LayoutParams scrollParams = new
ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout.LayoutParams myViewParams = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mainLinear.addView(mainView, myViewParams);
scrollView.addView(mainLinear, mainParams);
scrollView.setFillViewport(true);
addContentView(scrollView, scrollParams);
Guys I have a cardview and I want to add 2 button on it with rule but addRule() method is not working. In the picture, figure A is occurring but I want to make figure B, I mean, I want the buttons set align_parent_right and align_parent_bottom and the second button adjacent to first one. When I run it, figure A occuring. Any suggestions?
RelativeLayout.LayoutParams rl= (RelativeLayout.LayoutParams) iView.getLayoutParams();
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(width/4,height/5);
RelativeLayout.LayoutParams lparams2 = new RelativeLayout.LayoutParams(width/4,height/5);
removeButton=new Button(mContext);
modifyButton=new Button(mContext);
lparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
cardView.setLayoutParams(rl);
cardView.addView(removeButton,lparams);
cardView.addView(modifyButton,lparams2);
Try this:-
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
CardView cardView = new CardView(this);
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
cardView.setLayoutParams(cardParams);
mainLayout.addView(cardView);
RelativeLayout relativeLayout = new RelativeLayout(this);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
cardView.addView(relativeLayout);
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.RIGHT);
relativeLayout.addView(linearLayout);
Button removeButton = new Button(this);
Button modifyButton = new Button(this);
removeButton.setText("Remove");
modifyButton.setText("Modify");
linearLayout.addView(removeButton);
linearLayout.addView(modifyButton);
Please have a look at the following code:
LinearLayout ll1 = new LinearLayout(context);
ll1.setBackgroundColor(Color.BLUE);
ll1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll1LayoutParams.setMargins(100, 0, 100, 0);
ll1.setLayoutParams(ll1LayoutParams);
...
// parentLayout is FrameLayout
parentLayout.addView(ll1, ll1LayoutParams);
Why doesn't it work?
Change
LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
to
FrameLayout.LayoutParams ll1LayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
When assigning layout params to a child, you must assign the LayoutParams class of its parent and not the view. Since here your parent view is a FrameLayout, you have to use FrameLayout.LayoutParams.
I'm trying to add a textview and a button to a linear layout programatically.
The button is showing up but the textview isn't.
Here is my code:
LinearLayout main = (LinearLayout) findViewById(R.id.mainlayout);
LinearLayout first = new LinearLayout(this);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tvparams.weight = 3.0f;
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
btparams.weight = 2.0f;
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
main.addView(first);
Try setting some text on the TextView. Use it's setText() method
ok try this to set programmatically
LinearLayout myLayout = (LinearLayout)findViewById(R.id.parent);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,3.0f);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,2.0f);
LinearLayout first = new LinearLayout(this);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("asdsad");
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
myLayout.addView(first);
hope it ll help,working for me correctly.
I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);