Himanshu
Himanshu

Reputation: 1086

How to give Layout width, height to edit text programatically?

I have a problem I have created EditText run time in my code now I want to give size(height and width) for that Edittext how can I do the same, like in xml file we give android:layout_heigtht and layout_width. How to do it programatically?

Thanks

Upvotes: 0

Views: 5313

Answers (4)

star angel
star angel

Reputation: 520

    addQuestion = new Button(myContext);
    addQuestion.setId(134);
    addQuestion.setTag("addQuestion");
    addQuestion.setBackgroundDrawable(getResources().getDrawable(R.drawable.add_button));
    addQuestion.setGravity(Gravity.LEFT);

    LinearLayout.LayoutParams addQuestion_Params = new LinearLayout.LayoutParams(24, 24);  
    addQuestion_Params.leftMargin=20;
    addQuestion.setOnClickListener(myScreenClickListener);        
    questionAndAddButtonContainer.addView(addQuestion, addQuestion_Params);

above given is an example of dynamically setting height and width of a button.

LinearLayout.LayoutParams addQuestion_Params = new LinearLayout.LayoutParams(24, 24);  

here 24 is d width and height of the button.

Upvotes: 2

varuaa
varuaa

Reputation: 389

Use LayoutParams, to set the layout in the code. I haven't tried this, but saw something similar to this in a related post.

Also this is provided as an answer by someone else, so it works, it seems.

Upvotes: 1

Rasmus Øvlesen
Rasmus Øvlesen

Reputation: 510

You should just be able to set them with the setWidth(int width) and setHeight(int height). Something like this:

EditText edit = new EditText(this);
edit.setWidth(100);
edit.setHeight(50);

Upvotes: -1

Luke Vo
Luke Vo

Reputation: 20678

Use setWidth and setHeight method. There's no layout property in code design. For more about TextView and EditText, look at the documentation: TextView EditText

And if you need to set the size in "dip", there's no dip when coding, you need the DisplayMetric, then calculate the size yourself.

Upvotes: 0

Related Questions