moony
moony

Reputation: 434

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?

Upvotes: 4

Views: 5905

Answers (3)

arlistan
arlistan

Reputation: 741

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

Upvotes: 3

sherin
sherin

Reputation: 1091

Try this too you can set EditText box height and width:

 LayoutParams lparams = new LayoutParams(60,30); // Width , height or (LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
 EditText edittext = new EditText(this);
 edittext.setLayoutParams(lparams);

Upvotes: 0

Chirag Jain
Chirag Jain

Reputation: 1612

If you want your edittext to match the width of linearLayout

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                         LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);

other wise if you want width to be equal to few character wide..

see http://developer.android.com/reference/android/widget/TextView.html#setEms(int)

v.setEms(5);

Upvotes: 3

Related Questions