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?
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, 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 :)
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
will work only if the view you are setting the attributes to, is inside a RelativeLayout.
Original answer,