Edittexts in wrong position

Asked

Viewed 33 times

0

I am creating a list of Edittexts programmatically. In all I give a margin of 20dp and place one below the other. The problem is that textbox2 is always on top of 1, the other textbox is in the perfect position. follow the code written to create the Edit Texts:

int numeroParcelas = 15;
for (int e = 0; e < numeroParcelas; e++){
    final EditText myEditText = new EditText(mRlayout.getContext());
    RelativeLayout.LayoutParams editParams = new RelativeLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.WRAP_CONTENT);
    myEditText.setId(e);
    myEditText.getLayoutParams();
    myEditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
    myEditText.setHint("Parcela " + (e + 1) + ":");
    myEditText.setHintTextColor(R.color.colorAccent);
    myEditText.setTextSize(20);
    if ( e > 0 ) {
        editParams.addRule(RelativeLayout.BELOW, e -1 );
        editParams.setMargins(0,20,0,0);
    }else{
        editParams.addRule(RelativeLayout.ALIGN_PARENT_START);
    }
    myEditText.setLayoutParams(editParams);
    mRlayout.addView(myEditText);
}


Follow a print of the result:
inserir a descrição da imagem aqui

  • If you take the line from the editParams.setMargins(0,20,0,0);, what happens?

  • Will be all Edittexts on top of each other

  • What if instead of putting inside the if, you put right before the myEditText.setLayoutParams(editParams); after the }?

  • Anyway. Actually that if was a test I just did

1 answer

0

Try removing the if/Else to leave only editParams.addRule(RelativeLayout.BELOW, -1 ); I think it might work, because as far as I can tell e > 0 (parcel 2) is creating an edittext in the same position as the first item, only with a margin of 20 vertically (which was defined in setMargins)

Browser other questions tagged

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