Ludvig A. Norin
Ludvig A. Norin

Reputation: 5325

How to make single-line EditText draw itself correctly when using a specific size

I need to set the height and width of a EditText widget programmatically. I am using a RelativeLayout to do this, and it works fine as far as the size goes, but the actual widget paints itself incorrectly:

Incorrect Drawing of EditText

I would have expected it to paint like this (which it will if I set the height to ViewGroup.LayoutParams.WRAP_CONTENT instead of my requested height - sometimes): enter image description here

Any ideas?

Please note that I can't switch to DIP:s (dpi independence is actually already handled by the engine that use this code). Also note that I haven't found any value of setTextSize() that fix the misbehaviour.

This is how I create my widget:

  EditText et = new EditText(this);
  et.setFocusable(true);
  et.setSingleLine(true);
  et.setHint(label);
  et.setText(text);
  et.setTextSize(TypedValue.COMPLEX_UNIT_PX,(h/3)+5);
  et.setId(cn); 
  rllp = new RelativeLayout.LayoutParams(w,h);
  rllp.leftMargin = x;
  rllp.topMargin = y;
  layout.addView(et,rllp);

Upvotes: 1

Views: 495

Answers (1)

Rohit Mandiwal
Rohit Mandiwal

Reputation: 10462

try to change the last line of the posted code to this

layout.addView(et);

Upvotes: 1

Related Questions