0

I have problem with android EditText widget. In code I set width of widget by pixel. But when user focus and try input text, with of editText widget change. How we can solve this ? Please see video to understand my problem. VIDEO

Note: no need minuses. If you some not understand, please ask here, and I provide more details. Thanks.

float[] sizes = calculateMaxWidthAnswer(answer);

    this.editTextWidth = sizes[0];
    this.editTextHeight = sizes[1];

    editText.setWidth((int) editTextWidth);
    editText.setHeight((int) editTextHeight);


 protected float[] calculateMaxWidthAnswer(String item) {
    CustomKeyboardEditText cKE = new CustomKeyboardEditText(getContext(), isDisabledCustomKeyboard);
    cKE.setSingleLine(true);
    cKE.setTypeface(Typeface.DEFAULT_BOLD);
    cKE.setText(item);
    Paint mPaint = cKE.getPaint();
    Rect bounds = new Rect();
    mPaint.getTextBounds(item, 0, item.length(), bounds);
    float size = bounds.width() + MathTool.dpToPx(cKE.PADDING_LR * 3);

    return new float[]{size, bounds.height()};
}

In CustomKeyboardEditText widget, any code which can make this trouble.

billy gates
  • 188
  • 3
  • 16
  • 2
    Could you show us some code? – MeLean Oct 17 '16 at 09:37
  • @MeLine updated – billy gates Oct 17 '16 at 09:55
  • 1
    If i want to change the width of a view i would use view.getLayoutParams().Width = ... – Nanoc Oct 17 '16 at 09:57
  • I think that the with changes because your EditText Style has some padding. You can see how to choose different styles [here](http://stackoverflow.com/questions/11723881/android-set-view-style-programatically) – MeLean Oct 17 '16 at 10:18
  • @MeLine it's simple default textEdit. With set 9path image resource background. So, no different style. And why width change when we start input text ? – billy gates Oct 17 '16 at 10:22
  • It is normal behavior of editText to change width in order to fit all entered text in it. If you want fixed width You can see [this question](http://stackoverflow.com/questions/4257843/in-android-how-to-create-edittext-of-fixed-width) – MeLean Oct 17 '16 at 10:39

0 Answers0