How to refer to each EditText individually?

59 views Asked by At

I have two layouts, main.xml and it has a viewstub element and hidden.xml. the setContentView is set main.xml.

And In both of the layouts, there is an edittext with @+id/latEt. After I inflated the main.xml with hidden.xml how can I refer to each edittext individually? Or, I have to simply change the id of each edittext?

4

There are 4 answers

0
nitzanj On BEST ANSWER

I agree that it's not recommended to have the same ID on more than one view, however sometimes it's unavoidable. In that case, as long as the two views are not direct children of the same viewgroup, you can use findViewById on their respected viewgroup.

For example when you inflate the stub you usually have a reference the newly inflated View - simply call findViewById on that view and you're guaranteed to get the view that's in that inflated stub and not the other one.

0
einschnaehkeee On

Read this: http://developer.android.com/guide/topics/ui/declaring-layout.html#id

Ids of Views need to be unique inside a view tree (or a subtree you're searching for a view by id). Android always returns the View with the first occurrence of the ID it finds.

0
Opiatefuchs On

It is not possible to give two same ids. just Change the id to a unique one

0
Shlublu On

You have to define a unique ID for each EditText as they are both part tof the same resulting view. The use of findViewById() won't be reliable otherwise, plus other bugs that may occur.