How to set the android property layout_alignParentBottom to a button dynamically - android

I have a dynamic layout and I have a button. How can I set the property layout_alignParentBottom to that button so that it would appear in the bottom of my relative layout?
Or do you know another way?

This will only work if your layout is a RelativeLayout, as other ViewGroups don't understand alignParentBottom.
RelativeLayout layout = findViewById(R.id.my_relative_layout);
Button button = new Button(this); // your button
lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(button, lp);

Related

Can I set the position of the widget with programming in android

Can I set the position of the widget with programming in android instead the xml code. For example:
And like these. Are there methods instead the above xml code to use?
Yes you can set all the circled attributes via code aswell. For example this code aligns the button1 to the left of its parent (RelativeLayout) and positions button2 left of button1.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);
Keep in mind that
layout_alignTop
layout_alignEnd
layout_alignParentBottom
layout_alignParentLeft
layout_alignParentStart
layout_toRightOf
layout_above
layout_below
layout_centerInParent
layout_centerHorizontal
will work only if the view you are setting the attributes to, is inside a RelativeLayout.
Original answer,
Yes you can, for example
Button button1 = new Button(R.id.buttonn);
Button button2 = new Button(R.id.buttonn2);
button1.setX(button2.getX);
There are more functions like setX , if you need something spesificly just ask for it :)

adding linear layout to current view

I'm sure this has come up often, but I couldn't find an exact solution for my issue. I'm simply trying to programmatically create a linear layout with a textview and 2 buttons and add it to the bottom of my fragment (which is also a linear layout). I.e. I want it to appear below the other views on the screen not the actual bottom. However, when I try to add it, it always appears at the top corner and not the end. Here's my code:
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.CYAN);
TextView noteView = new TextView(getActivity());
LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
noteView.setLayoutParams(lparams);
noteView.setText(noteText);
layout.addView(noteView);
getActivity().addContentView(layout, lparams);
}
Have you tried to add gravity to the LinearLayout?
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
layout.setLayoutParams(params);
If your main goal is to have your views line up relative to each other, perhaps it would be worth considering using a Relative Layout to accomplish a similar task.
If that's not an option for whatever reason, I think LayoutParams has a gravity option that can be set to bottom which will align views to the bottom of the parent view. I can't guarantee this will work though as I usually do not accomplish tasks like these programmatically.

Add dynamically buttons to layout in android

I was wondering how can I add dynamically buttons to layout from code. I don;t know how many buttons I will should add. This number will be random. I want to add them in this way:
Any ideas how can I do that dynamically in this way?
You can do it this way to add button:
Button myButton = new Button(this);
myButton.setText("Button");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Button myButton1 = new Button(this);
myButton1.setText("Button1");
ll.addView(myButton1, lp);
create one linearlayout with horizontal orientation and add two buttons on that.. when you have more than two buttons then add another linearlayout with horizontal for that buttons..

Aligning a Linear Layout within a RelativeLayout to the Center and to the Left

Currently I have tried aligning my Linear Layout to the left and center but currently cannot get this to work. See code below:
CustomLinearLayout sideMenu = new CustomLinearLayout(this);
RelativeLayout.LayoutParams sideMenuParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
sideMenuParams.addRule(RelativeLayout.CENTER_VERTICAL);
sideMenuParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
sideMenu.setLayoutParams(sideMenuParams);
sideMenu.setOrientation(LinearLayout.VERTICAL);
sideMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sideMenu.setBackgroundColor(Color.TRANSPARENT);
//sideMenu.setGravity(Gravity.CENTER);
sideMenu.addView(AppSoups);
sideMenu.addView(salads);
sideMenu.addView(ribs);
sideMenu.addView(favorites);
sideMenu.addView(sandwiches);
sideMenu.addView(sides);
sideMenu.addView(desserts);
RelativeLayout screenLayout = new RelativeLayout(this);
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
screenLayout.setBackgroundResource(R.drawable.body_bkgd);
screenLayout.addView(sideMenu);
setContentView(screenLayout);
CustomLinearLayout is just a class that extends LinearLayout to do some custom drawing. Anyway what I am doing wrong here? So far it aligns left and to the top of the relative layout but i cannot get it to center. Just as a side note both or either or of my rules do not work in conjuction with setting the LinearLayout to the center(i have it commented out in the code). Finally the views that i am adding to the linear layout are just textviews - just FYI(code not shown)
I think you are "overriding the layout params you've set in line 5 with new layout params you are setting in line 7, so
sideMenuParams.addRule(RelativeLayout.CENTER_VERTICAL);
sideMenuParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
are not really taking affect
In here
screenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
try changing the WRAP_CONTENT to FILL_PARENT

Add button to bottom of linear layout programmatically

I have a bunch of buttons displayed using the default gravity. The last button I add to the LinearLayout view is what I'd like to appear at the bottom of the view. How do I add it programmtically to appear at the bottom of the screen? I've tried setting the gravity, but everything falls to the bottom. I just want the one button to fall to the bottom of the screen. Ideally, I won't have to make another view.
Try this:
Button button = new Button(this);
youLinearLayout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
edit
sorry, the code above dont work.
You cant do this using an single LinearLayout if orientation==vertical.
You'll need create another layout(RelativeLayout) and add TextView to it.
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(textView, params);
linearLayout.addView(relativeLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
If the rest of the screen is not empty, you can give android:layout_weight=0.0 to the Button, and 1.0 to the widget on the top.
In that way, widget with 1.0 weight will expand to fill empty areas, and Button with 0.0 will take only the default space, and being the last item addes to a vertical LinearLayout, it will be sticked to the bottom.

Categories

Resources