-1

I was following an Android tutorial about RelativeLayout which just made a basic layout of two EditTexts (first name and last name fields) and a Button(not important). The first name EditText view had a layout_width of 0dp, but was still visible on the screen. I messed around with the width value and it seemed that no matter what value I gave it, the size of the EditText stayed the same. Why is that? Does it have something to do with the layout_toLeftOf statement? Below are links to the tutorial along with a picture of my xml code and the resulting output.

Tutorial Link: http://tinyurl.com/jjnfw7z

note: the button code/output is not a worry

2]

1 Answer 1

1

The width of the EditText with id first_name is completely specified by these two constraints:

android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/last_name" 
  • android:layout_alignParentLeft="true" implies that the left edge of this EditText must match the left edge of the parent view.
  • android:layout_toLeftOf="@+id/last_name" implies that the right edge of this EditText must match the left edge of the sibling view with id last_name.

By design, these take priority over any specified layout_width value, which is why you can change that dp width with no visible effect. It is common to set the width to 0dp in such cases; since the layout_width attribute is required, a value of zero is the clearest possible indicator that the view's width is being determined elsewhere (by other attributes; in code, etc.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.