Belgi
Belgi

Reputation: 15062

Android: Creating a TextView programmatically

How can I create a TextView (with Java code, not xml) with layout_width=fill_parent ?

The TextView should be editable.

Upvotes: 1

Views: 6646

Answers (4)

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

Create layoutParams with appropriate width and height by

LayoutParams params = //choose appropriate constructor

//beware about right import .

now Create textBox and setLayoutParams

EditText t = new EditText(this);

t.setLayoutParams(params);

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Its not a TextBox, its EditText in Android.

Anyway, you can create it run time using:

EditText ed = new EditText(this);    // Create a new EditText

// Setting the type of input that you want
ed.setInputType(InputType.TYPE_CLASS_TEXT);       

// setting height/width for your editText
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  

Upvotes: 4

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

EditText et = new EditText(context);// this gets you a new textbox

//assuming you're in eclipse when you hit the . after et you'll see all its various properties that you can set as you like.

then when you're ready you either set it as the setContentView(et);// here it's the only thing on the screen

or

add it to whatever layout you have already set.

Upvotes: 0

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

If i don't understand question wrongly,you need to create an EditText programatically.then,Try using:

EditText et=new EditText(context);
et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

EDIT :

Import for LayoutParams:

import android.view.ViewGroup.LayoutParams;

Upvotes: 6

Related Questions