Android button does not trigger when in different order in XML file -


not urgent have work around, understand whether there reason behaviour observe when make changes below.

i have simple layout edittext , button in xml file.

it displays fine, , code below works fine (triggering onclick)

<relativelayout android:id="@+id/group"     android:layout_width="wrap_content" android:layout_height="wrap_content">      <edittext android:id="@+id/topic" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:layout_toleftof="@+id/addtopic"         android:layout_alignparentleft="true" android:ems="10" />      <button android:id="@+id/addtopic"         android:layout_alignparentright="true" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:layout_weight="1"         android:onclick="onclick" android:textsize="10dp" android:text="add topic" />  </relativelayout> 

however, if flip objects around, below, button not trigger - onclick not run , nothing happens.

<relativelayout android:id="@+id/group"     android:layout_width="wrap_content" android:layout_height="wrap_content">      <button android:id="@+id/addtopic"         android:layout_alignparentright="true" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:layout_weight="1"         android:onclick="onclick" android:textsize="10dp" android:text="add topic" />      <edittext android:id="@+id/topic" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:layout_toleftof="@+id/addtopic"         android:layout_alignparentleft="true" android:ems="10" />  </relativelayout> 

i might missing obvious here, , doesn't matter in sense works in first structure, matter in sense know why. ideas appreciated.

combining information posted in comments, reason fact mistakenly use @+id/ when referring other objects, instead of @id/. specificly edittext contains android:layout_toleftof="@+id/addtopic". + sign should used when introducing new ids.

in first example not harmful listener, since button declared after edittext , @+id/ works should, in second example id of button might overriden when declare edittext.

tl;dr: in edittextreplace android:layout_toleftof="@+id/addtopic" android:layout_toleftof="@id/addtopic". if use "work around".


Comments