Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set EditText box height and width programmatically in Android

Tags:

I want to manage the height and width of an EditText box programmatically in Android. I tried edittext.setWidth(32); and edittext.setEms(50);, but both are not working. See my below code, as I am using it to create dynamic EditTexts in Android.

private EditText createEditText()
{
    final LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    final EditText edittext = new EditText(this);
    edittext.setLayoutParams(lparams);
    edittext.setWidth(32);
    edittext.setEms(50);
    return edittext;
}
like image 529
Ronak Mehta Avatar asked Feb 10 '12 08:02

Ronak Mehta


3 Answers

private EditText createEditText()
{
    final LayoutParams lparams = new LayoutParams(50,30); // Width , height
    final EditText edittext = new EditText(this);
    edittext.setLayoutParams(lparams);
    return edittext;
}

Try this.

like image 187
Dhruvisha Avatar answered Sep 28 '22 00:09

Dhruvisha


edittext.getLayoutParams().width=32;
like image 24
JohnCookie Avatar answered Sep 28 '22 02:09

JohnCookie


DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
final float height = metrics.heightPixels;

EditText edittext = new EditText(this);

edittext.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,(int) (height/2)));
like image 39
B Roh Avatar answered Sep 28 '22 02:09

B Roh