How to set EditText box height and width programmatically in Android

[亡魂溺海] 提交于 2019-11-29 01:21:08
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.

edittext.getLayoutParams().width=32;
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)));
RelativeLayout.LayoutParams.width = 32; 
RelativeLayout.LayoutParams.height = 50;

works for me. for example

    lparams.width = 32;
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams();
            params.width = mScreenWidth / 5;
            params.height = mScreenWidth / 5;
            edittext.setLayoutParams(params);
Vipin Yadav

Try the following code:-

Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

profile_pic.getLayoutParams().height=(screenHeight/6);
    profile_pic.getLayoutParams().width=(screenHeight/6);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!