How to set margin on view programmatically? - java

I am new to android development and I am trying to add margins to my view but I have not been able to get it to work.
Here's my code:
ConstraintLayout layout = new ConstraintLayout(this);
final float scale = getResources().getDisplayMetrics().density;
layout.setMinHeight((int)(100*scale));
layout.setMaxHeight((int)(100*scale));
CircleImageView icon = new CircleImageView(this);
icon.setImageResource(image);
icon.setBorderWidth(3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(90*scale), (int)(90*scale));
icon.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(icon.getLayoutParams());
marginParams.setMargins(100, 0, 0, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(marginParams);
icon.setLayoutParams(params);
layout.addView(icon);
Why isn't this working? Thanks!

You should do like this:
LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
param.setMargins(left, top, right, bottom);
view.setLayoutParams(param);

After you have created the icon, you should first generate an id for it:
icon.setId(View.generateViewId());
Then, remove the setMargins line and add these lines below layout.addView(icon):
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.constrainWidth(icon.getId(), (int)(90*scale));
set.constrainHeight(icon.getId(), (int)(90*scale));
set.connect(icon.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(icon.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 100);
set.applyTo(layout);
I haven't tested it but it should be a step in the right direction.
Let me know how it goes.

Related

problem in creating textview programmatically

I'm try to create programmatically textView for a project. My problem is that I don't now how to set up the position of the textview on the screen.
I tried with the code below but instead of moving the textview it moves all the margin. The code is this:
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint);
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(50);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
constraintLayout.getLayoutParams();
params.setMargins(100, 100, 100, 100);
valueTV.setLayoutParams(params);
constraintLayout.addView(valueTV);
the code is inside the oncreate. Thanks for help
You can try something like that:
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(100);
constraintLayout.addView(valueTV);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) valueTV.getLayoutParams();
layoutParams.rightToRight = constraintLayout.getId();
layoutParams.topToTop = constraintLayout.getId();
layoutParams.bottomToBottom = constraintLayout.getId();
layoutParams.leftToLeft = constraintLayout.getId();
valueTV.setLayoutParams(layoutParams);

linearLayout.addView(TextView) not working

private LinearLayout clmnView;
LinearLayout clmnView = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = DimensionsHelper.getMargins(getContext(), DimensionsHelper.DimensionType.BALL);
params.setMargins(0, margin, 0, margin);
clmnView.setLayoutParams(params);
clmnView.setOrientation(LinearLayout.VERTICAL);
clmnView.setGravity(Gravity.CENTER_VERTICAL);
TextView historyTextView = new TextView(getContext());
historyTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
historyTextView.setText("this is history");
clmnView.addView(historyTextView);
No matter what I do ,the historyTextView is not display inside my clmnView.
Could you please help? Thank you in advance.
Okay everything was working as it should. But I was displaying white text in white background.
Changed the color of the text like this :
historyTextView.setTextColor(Color.parseColor("#000000"));
and my text appeared.

Cannot retrieve the integer getHeight()

I have created a Layout 'layout' programmatically, then I created a TextView in the same layout to display the layout height, there are no other methods or anything else in the class. Unfortunately the app closes, and I can't identify why.
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(layout.getHeight());
layout.addView(fimText, lparams);
setContentView(layout);
Your LinearLayout needs to be added to a container before you can call getHeight on it. Also, getHeight returns an int so you need to convert it to a String.
Try this:
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
setContentView(layout);
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(String.valueOf(layout.getHeight()));
layout.addView(fimText, lparams);
This is because layout.getHeight() will not return an CharSequence value. Add .toString() or ""+ to it.

Multiple RelativeLayout's all programmatically

Below is what my layout looks like.
What I want is for the 2 green arrows to be centered vertically in the top left square. Also, the double number sign ("##") needs to be centered vertically and horizontally in the top left square.
Then, the other set of double number signs need to be centered vertically and horizontally in the top right square.
Below is the code.
public class Puzzle extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dividers();
topLeft();
topRight();
setContentView(relativelayout);
}
public void topLeft() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.LEFT_OF, view1.getId());
relativelayout.addView(topLeftRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topLeftRL.addView(puzzle, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_VERTICAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
topLeftRL.addView(left, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_VERTICAL);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
topLeftRL.addView(right, params3);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.CENTER_IN_PARENT);
topLeftRL.addView(levelCounter, params4);
left.setImageResource(R.drawable.left_arrow);
right.setImageResource(R.drawable.right_arrow);
puzzle.setText(c.getResources().getString(R.string.puzzlePuzzleTV));
levelCounter.setText("###");
}
public void topRight() {
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params0.addRule(RelativeLayout.ABOVE, view0.getId());
params0.addRule(RelativeLayout.RIGHT_OF, view1.getId());
relativelayout.addView(topRightRL, params0);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(record, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
topRightRL.addView(moves, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_IN_PARENT);
topRightRL.addView(movesCounter, params3);
movesCounter.setText("##");
record.setText(c.getResources().getString(R.string.puzzleRecordTV) + " " + "##");
moves.setText(c.getResources().getString(R.string.puzzleMovesTV));
}
public void dividers() {
// vertical line (top of screen)
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(5, height / 6);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
params1.setMargins(0, 10, 0, 10); // left, top, right, bottom
relativelayout.addView(view1, params1);
// horizontal line (top of screen)
RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params0.addRule(RelativeLayout.BELOW, view1.getId());
params0.setMargins(10, 0, 10, 0); // left, top, right, bottom
relativelayout.addView(view0, params0);
// horizontal line (button of screen)
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5);
params3.addRule(RelativeLayout.ABOVE, linearlayout.getId());
relativelayout.addView(view3, params3);
}
}
So it looks like when I add multiple addRule() to a param, it doesn't read both of them. I think the problem lies in this line of the topLeft() method: params0.addRule(RelativeLayout.ABOVE, view0.getId());. I've tried switching this line with the other addRule in that param and the result is the same. Why is this?
EDIT #1
- Cleaned up my code to make neater and easier to read.
You are using params that require an anchor. You need to use "addRule (int verb, int anchor)"
so like
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
I found the answer to my own question. The error was in a part of my onCreate() method that I had not included in my opening post! This was the problem code:
view0.setId(0);
Apparently the id cannot be set to 0. Anyways, I changed the id number to a positive number and everything worked.

How to put one buttom right and another left

I try to create dynamically linerlayout and i created two buttoms.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleView.setText("Hallo Welt!");
layout.addView(titleView);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
layout.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout.addView(btnDisconnect);
I want to put Connect button to left corner and want to put disconnet button to the right corner. How can i do that?
Have you tried to set layout gravity for your buttons?
LayoutParams params;
Button btnConnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Left;
btnConnect.setLayoutParams(params);
...
Button btnDisconnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Right;
btnConnect.setLayoutParams(params);
...
Layout gravity defines where to place a view in its parent (see Gravity and layout_gravity on Android too)
Cheers
You can use RelativeLayout instead of linear layout.
and add rules to the layout params of the
RelativeLayout.Layoutparams params = RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

Categories