Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

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

EDIT :

Import for LayoutParams:

import android.view.ViewGroup.LayoutParams;

Solution 2:

Its not a TextBox, its EditText in Android.

Anyway, you can create it run time using:

EditTexted=newEditText(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(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  

Solution 3:

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.

Solution 4:

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);

Post a Comment for "Android: Creating A Textview Programmatically"