5

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?

Geobits
  • 22,218
  • 6
  • 59
  • 103
J.J. L
  • 51
  • 2
  • 13
    findViewById returns an object of the type View. So you need to cast it to the appropriate type when you call it. See reference: http://developer.android.com/reference/android/app/Activity.html#findViewById(int) – daniel_c05 Nov 02 '13 at 01:25

4 Answers4

4

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.

Szymon
  • 42,577
  • 16
  • 96
  • 114
0

findViewById finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle). Refer to-:http://developer.android.com/reference/android/app/Activity.html#findViewById(int)

Barun
  • 312
  • 3
  • 16
0

I do not know how much of programming experience you have with different languages. But this is a fairly used concept in many languages. And it is all but very logical. Let me explain this using 2 approaches :-

1) If you have any coding experience with C, you would find it very much similar. So, in C, let say when you use malloc() to allocate a chunk of memory, you just say

malloc(numberOfBytesYouNeed);

Now what malloc does is that it reserves the requested number of bytes for you (if available) and returns a void * pointing at the starting address of the allocated chunk of memory.

Now although you have the void pointer with you, you would still want to access the memory allocated in a certain way. Right ? So you might want to access the allocated memory with a scale factor of 2 (for integers) or 1 (for character) etc.

Moreover, the pointer that malloc() returned will be accessible to you only if you assign it to one of your own pointers. Now as I mentioned above, if you would want to store 2 byte integers in the newly allocated memory, ,then to access this array of memory of integers, you would need an integer pointer.

Since you would need an integer pointer, you will have to assign the void pointer returned by malloc() to your integer pointer. Something like this :-

int * myPointer= malloc(numberOfBytesYouNeed);

But as per the language semantics, this would be a compile time error. Because you can assign only similar pointers to each other.

Hence, you would need to typecast the void * returned by malloc() to an int * . Something like this :-

int * myPointer= (int*)malloc(numberOfBytesYouNeed);

Now similar is the concept in Android (or any other programming language for that matter) as well. As explained by the others above, findViewById return type is View (which is the parent class for EditText and other classes for visual widgets). But since, you would actually want to access this View object returned by findViewById as an EditText object, you would need to typecast it to one.

2) Now the other example, would be very basic. Let's say you know to speak only in German. Now you happen to meet a hot chick who knows to speak only in Hindi. But she does understand German. Now if you ask her in German, if she would be interested to go out with you on a date,(findViewById), she would definitely understand it. But since she does not know to speak German, she would reply back in Hindi. Now even if her answer was YES, it would not really be much useful to you. What you would need here is a translator to convert (typecast) her answer, which is in Hindi (View Object), into something that you would understand, i.e. German (EditText object)

Hope the answer helps !

qre0ct
  • 5,680
  • 10
  • 50
  • 86
0

As of API 26, the cast may not be necessary any more. The documentation now states

Note: In most cases -- depending on compiler support -- the resulting view is automatically cast to the target class type. If the target class type is unconstrained, an explicit cast may be necessary.

This is because the return type is now:

<T extends View> T  findViewById(int id)

as noted in this answer.

John Cummings
  • 1,949
  • 3
  • 22
  • 38