Reputation: 13
Hi guys I'm trying to set the width of an EditText. My goal is to set the length of the EditText exactly 1/2 of the total screen.
I wrote this code:
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.mainRelativeLayout);
int width = rl.getWidth();
int half = width/2;
EditText userName = (EditText) this.findViewById(R.id.userName);
userName.SET_SOMEHOW_THE_SIZE(half);
But i can't find a working method to set the width :(
Thanks Marco
Upvotes: 0
Views: 3183
Reputation: 33509
Marco,
EditText
inherits TextView
. This means you can use the inherited setWidth()
method.
Upvotes: 1
Reputation: 13096
Better done within the layouts, don't use code
the following creates an EditText that takes half screen, and a TextView that takes the other half, just learn to play with the layouts, they should let you get the result you expect
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Upvotes: 3