Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you cast (EditText) findViewById to (EditText)?

Tags:

java

android

I am beginning to learn Android programming. When you declare objects of type EditText and initialize them later, why do you cast findViewById's contents to EditText?

What does findViewById return that needs to be casted?

like image 238
J.J. L Avatar asked Oct 03 '22 12:10

J.J. L


1 Answers

As daniel_c05 pointed out, findViewById return type is View (which is the parent class for EditText and other classes for visual widgets).

In Java, you need to cast to a specific type in this case - that's the requirement of the language.

like image 64
Szymon Avatar answered Oct 07 '22 17:10

Szymon