Home » Android » android – No need to cast the result of findViewById?

android – No need to cast the result of findViewById?

Posted by: admin March 10, 2020 Leave a comment

Questions:

Recently i found that AndroidStudio reminds me to remove some class cast. I remember that in the old time, we have to cast the result of findViewById, but now it’s not necessary.

The result of findViewById is still View, so i want to know why we don’t need to cast the class?

I can’t find any documents mentioned that, can anyone find any document?

How to&Answers:

Starting with API 26, findViewById uses inference for its return type, so you no longer have to cast.

Old definition:

View findViewById(int id)

New definition:

<T extends View> T findViewById(int id)

So if your compileSdk is at least 26, it means that you can make use of this 🙂

Answer:

According to this article:

The following function relies on Java’s generics automatic type inference in order to eliminate a need for manual casting:

protected <T extends View> T findViewById(@IdRes int id) {
    return (T) getRootView().findViewById(id);
}

Answer:

In older versions:

AutoCompleteTextView name = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

From Android Studio 3.0 with SDK 26:

AutoCompleteTextView name = findViewById(R.id.autoCompleteTextView);

Answer:

Android Studio reminds to remove casting, if you use common attributes from View class, like visibility or some common methods, like onClick()

For example:

((ImageView) findViewById(R.id.image_car)).setVisibility(View.VISIBLE);

In this case you can simply write:

findViewById(R.id.image_car).setVisibility(View.VISIBLE);

Answer:

Android 0, clean up casting

One of the things that google announce in IO 2017 is something that’s called ‘cast away’ :). Android developer don’t have to do a manual casting for findViewById(). For example the old way to get a text view using findViewById() would be something like this.

TextView txtDesc = (TextView) findViewById(R.id.textViewDesc);
txtDesc.setText(getString(R.string.info_angkot_description));

While the new way would be like this

TextView txtDesc = findViewById(R.id.textViewDesc);
txtDesc.setText(getString(R.string.info_angkot_description));

It’s a simple change. But for a seasoned programmer, a clean code like this can make you very happy and it helps with your coding mood 🙂

To be able to do this you only needed to set your project compiled sdk version to version 26 in your app build.gradle.

You can still target earlier sdk version too, so it’s a non-intrusive changes.

Now the real problem, how do you clean that old code that uses casting all this time. Especially when you have like hundreds of activity files. You can do it manually, or maybe hired an intern to do it 😛. But luckily for all of those intern, android studio already prepared to help us with this.

When you put your caret (or click on the redundant casting) android studio will suggest 2 option to handle the redundant casting.

First it will suggest to remove that redundant cast or you can select clean up code. It will remove all the redundant cast for that file. This is better, but we want more. We don’t want to open each file and do this clean up one by one.

One of the things that make IntelliJ idea Special is that a feature that is called intent action. All you have to do is push ctrl+shift+A and then type clean. And select Code Clean up action, and select the whole project scope. With this few simple steps your code will be a whole lot cleaner.

One important point is that you do this with some code versioning system. This way you can compare the changes that being made by the intent action and revert any files you want.

Copied from original post:

https://medium.com/@abangkis/android-0-clean-up-casting-c30acec56cef