Dynamically change the margin of a Checkbox

Asked

Viewed 199 times

1

This attribute can be changed dynamically in java code?

android:layout_marginTop

I have a CheckBox dynamically generated, and I need him to have a margin.

Java code:

CheckBox cb = new CheckBox(this);
//cb.setId();
//cb.setText();
cb.setChecked();
_relativeLayout.addView(cb);

Without the margin, it is displayed this way:

Imagem ilustrativa

One on top of the other.

EDITION:

With the margin, it is not displayed.

ViewGroup.MarginLayoutParams ml = new ViewGroup.MarginLayoutParams(10, 10);
ml.setMargins(0, 5, 0, 0);
CheckBox cb = new CheckBox(this);
//cb.setId();
//cb.setText();
cb.setChecked(true);
cb.setLayoutParams(ml);
_relativeLayout.addView(cb);

ISSUE 2

RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ml.setMargins(0, 5, 0, 0);
for (int i = 0; i < json.length(); i++) {   
    CheckBox cb = new CheckBox(this);
    //ml.addRule(RelativeLayout.BELOW, _relativeLayoutCbIngredientes);
    cb.setLayoutParams(ml);
    _relativeLayoutCbIngredientes.addView(cb);
}

Even though we set margem continues one on top of the other, the problem is not the margem, and I think so Bellow.

How to add the Bellow in the CheckBox and it is generated dynamically?

  • Exception, create a MarginLayoutParams and put topMargin in it. The LayoutParams will depend on what kind of father CheckBox.

  • what did you mean by tipo? If it is RelativeLayout, LinearLayout...?

  • The class ViewGroup has a class LayoutParams only with width and height. Every subclass (RelativeLayout, LinearLayout etc) defines a class LayoutParams who inherits from ViewGroup.LayoutParams and some of MarginLayoutParams. The latter accepts margins, and also characterizes a ViewGroup that treats margins. The type would be which subclass is the father of the CheckBox.

  • @Wakim, I don’t know if I got it right, I added the MarginLayoutParams and now is not displayed the CheckBoxes. I updated my question.

1 answer

1


Edit: As the View which must be above CheckBox is generated dynamically, this solution using RelativeLayout.BELOW does not meet and generates problems even adding a id auto-generated. To each addView the RelativeLayout will need to reevaluate all rules, generating an "overhead" in the solution.

A simple solution would be to adopt a LinearLayout with orientation="vertical", because to each item inserted the LinearLayout puts it below the previous.

The only modification to the current code would be:

for (int i = 0; i < json.length(); i++) {

    // Precisa gerar um LayoutParams para cada View, não recomendo reutilizar.
    LinearLayout.LayoutParams ml = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    // Margem opcional
    ml.setMargins(0, 5, 0, 0);

    CheckBox cb = new CheckBox(this);
    cb.setLayoutParams(ml);

    _linearLayoutCbIngredientes.addView(cb);
}

To add dynamic margin in a View which is adding to the Layout, needs:

  1. Create the LayoutParams specific to the subclass of ViewGroup that will add to View. The Builder of LayoutParams receives the layout_width and layout_height that the View will have.
  2. Set values before calling addView(View view) or use the addView(View view, LayoutParams param).

An example:

RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

// A classe RelativeLayout.LayoutParams extende MarginLayoutParams,
// verificado na documentacao: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

ml.setMargins(0, 5, 0, 0);

CheckBox cb = new CheckBox(this);

cb.setChecked(true);

// Ou
cb.setLayoutParams(ml);
_relativeLayout.addView(cb);

// Ou
_relativeLayout.addView(cb, ml);

As a suggestion, why not use a rule to position a View below another?

Using the rule would be:

RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

// A classe RelativeLayout.LayoutParams extende MarginLayoutParams,
// verificado na documentacao: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

// Adiciona uma regra para o CheckBox ficar abaixo da View existente cujo id seja viewAcima.
// Parto do pressuposto que essa View ja esta no layout como mostrado na imagem.

ml.addRule(RelativeLayout.BELOW, R.id.viewAcima);

CheckBox cb = new CheckBox(this);

cb.setChecked(true);

// Ou
cb.setLayoutParams(ml);
_relativeLayout.addView(cb);

// Ou
_relativeLayout.addView(cb, ml);

In the latter case, the RelativeLayout will add one more restriction in your CheckBox should fall below the viewAcima.

I believe this form is more correct, because using a fixed margin can generate problems depending on the density and screen size and etc of the device. Therefore, it may be that in some devices the fixed margin given is not sufficient to place the CheckBox below the other View.

  • I think the problem I’m having is with Bellow same. How I am generating the CheckBox dynamically, is adding one on top of the other. But, for me to add the Bellow, would need to take the CheckBox, which is generated dynamically through a For. How do I get that "R.id.viewAcima"?

  • Ah, I get it, that View is also added dynamically... I think it complicates a little, it would have to "set" a id different for each one and use there in the rule... It is necessary to use a RelativeLayout, could not be a LinearLayout upright?

  • Could be a LinearLayout yes. It already solves the problem?

  • Problem solved. rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.