Daniel Arteaga Iriarte Daniel Arteaga Iriarte - 1 year ago 131
Android Question

Android detects instanceof EditText and instanceof TextView as the same

I have dynamically created Layout. It has some edittexts, textview, spinners, etc.

After it, I have to get the info I introduce on those views.

So Im doing something like this

for(int i=0;i <= childs;i++){
View v=parent.getChildAt(i);
if (v instanceof TextView) {
//do something
else if (v instanceof EditText) {
//do OTHER thing

the problem here is that Android detects "v" always as TextView when the View is either TextView or Edittext (I have no problem with spinners or button).

How can I solve this?

Answer Source

That's because EditText extends TextView.

switch the order of checking:

if (v instanceof EditText) {
     //do something
} else if (v instanceof TextView) {
     //do OTHER thing
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download