Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set size on an EditText programmatically

I have created an EditText programmatically. This EditText has one line and is added to a LinearLayout:

LinearLayout ll = (LinearLayout) myView.findViewById(R.id.select_options_option);
v = new EditText(context);
((EditText) v).setLines(1);
ll.addView(v);

My problem is that I can only type one character in this EditText. How can I set the width of an EditText, so that I can type in 3 or more charaters?

like image 289
moony Avatar asked Oct 21 '25 22:10

moony


1 Answers

Try adding layout parameters to the linear layout before adding the TextView

LinearLayout ll = (LinearLayout) myView.findViewById(R.id.select_options_option);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp);
v = new EditText(context);
((EditText) v).setLines(1);
ll.addView(v)

You can also add the WRAP_CONTENT in your xml file

like image 194
arlistan Avatar answered Oct 24 '25 15:10

arlistan