findViewById() as Type

Why do we use view.findViewById as EditText for example? What is the importance of the as Object type? The GeoQuiz didn’t have it.

I’m going to take a guess here that it’s like casting the type for the compiler

That doesn’t answer why GeoQuiz didn’t have it.

As I have noticed big nerd ranch baby steps code and introduces bits of styles and algorithms which is the best way to learn so they threw in the as keyword in my opinion but I found this in a search for as keyword and this popped up hope this helps you
https://kotlinlang.org/docs/reference/typecasts.html

Again, I know what casting is, my question is why Geoquiz didn’t use casting.

In other words my question is not about Kotlin but about Android.

Geoquiz:

val button = findViewById(R....)

CriminalIntent:

val button = itemView.findViewById(R....) as Button

I believe it would be a style choice. Kotlin is able to do smart casting so as Button is not required.

There may be a slight performance boost by doing the explicit typecast for when the code gets compiled down to bytecode.

Except that’s not the case since it’s literally a syntax error (type inferance failed…).

You guys do realize I posted this question under chapter 8 specifically and not randomly, right?

Imgur

The picture helped. I’d need to dig through the documentation for the difference in styles, but there are several ways to use findViewById()

Version 1

private val solvedImageView: Button = itemView.findViewById(R.id.crime_solved)

Version 2

private val solvedImageView = itemView.findViewById<Button>(R.id.crime_solved)

In both of these, the data type is known and can be inferred for the casting.

However, if the data type and the cast are omitted, then an error is generated (type inference failed)

private val solvedImageView = itemView.findViewById(R.id.crime_solved)

I know that doesn’t fully answer the question, but does that help? Are there specific listings in GeoQuiz and CriminalIntent that you can point to for further examples?

I think the poster misunderstanding something.

In GeoQuiz
Before they use findViewById(), they use private lateinit var trueButton: Button
so they can use type inferred in the next button = findViewById(R...)

If you don’t use lateinit var ... and just use val ..., like the poster did, you’ll have to use one of three kinds of style
private val button: Button = findViewById(R...)
private val button = findViewById<Button>(R...)
private val button = findViewById(R...) as Button

In project CriminalIntent CrimeFragment.kt
Since they have declaration
private lateinit var titleField: EditText
private lateinit var dateButton: Button
private lateinit var solvedCheckBox: CheckBox

Use as EditText is not necessary at all, you can delete as EditText and it still compiled because it can be type inferred.
titleField = view.findViewById(R.id.crime_title) as EditText
dateButton = view.findViewById(R.id.crime_date) as Button
solvedCheckBox = view.findViewById(R.id.crime_solved) as CheckBox

3 Likes

Thank you, that finally answered the question.

I really appreciate what you’re trying to do but you completely missed the point since the beginning…

I actually had the same question thank you octantis124 I’m new to kotlin but I didn’t realize the ‘as’ kotlin keyword was in conjunction to ‘lateinit’ kotlin keyword version 4 book briefly explained lateinit but not as, maybe in their next update they can explain the use of as keyword in conjunction with lateinit, but anyways I wasn’t trying to act like you (LeNDuyA) didn’t know what casting was it was all I could find on ‘as’ keyword just trying to help with the same question,thanks

1 Like