Marco
Marco

Reputation: 13

Android project - Define an EditText size

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

Answers (3)

Will Tate
Will Tate

Reputation: 33509

Marco,

EditText inherits TextView. This means you can use the inherited setWidth() method.

Upvotes: 1

ferostar
ferostar

Reputation: 7082

Just do:

userName.setWidth(half);

Upvotes: 1

BFil
BFil

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

Related Questions