No need to throw findViewById result?

I recently discovered that AndroidStudio reminds me to remove a class. I remember that in the old days we had to select the result of findViewById, but now this is not necessary.

The result of findViewById is still a View, so I want to know why we don't need to throw a class?

I can not find any documents mentioned, can anyone find any document?

+133
android casting findviewbyid
Jul 04 '17 at 9:52 on
source share
5 answers

Starting with API 26, findViewById uses output for the return type, so you no longer have to do it.

Old definition:

 View findViewById(int id) 

New definition:

 <T extends View> T findViewById(int id) 

So if your compileSdk at least 26, that means you can use this :)

+211
Jul 04 '17 at 10:23
source share

According to this article :

The following function is based on the automatic Javas generics type inference to eliminate the need for manual casting:

 protected <T extends View> T findViewById(@IdRes int id) { return (T) getRootView().findViewById(id); } 
+12
Dec 07 '17 at 12:34 on
source share

In older versions:

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

From Android Studio 3.0 with SDK 26:

 AutoCompleteTextView name = findViewById(R.id.autoCompleteTextView); 
+11
Nov 05 '17 at 16:42 on
source share

Android Studio reminds you to remove casting if you use common attributes from the View class, such as 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); 
+1
Jul 04 '17 at 10:28
source share

Android 0, clear casting

One of the things Google announces in IO 2017 is what is called "throw away" :). An Android developer does not have to cast manually for findViewById (). For example, the old way to get a textual representation using findViewById () would look something like this.

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

While the new path will be like this

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

This is a simple change. But for an experienced programmer, clean code like this can make you very happy, and it helps you tune in to programming :)

To do this, all you had to do was set the compiled SDK version of your project to version 26 in your build.gradle application.

You can still use an earlier version of the SDK, so these are not intrusive changes.

Now the real problem is how you clear that old code that uses cast all this time. Especially when you have hundreds of activity files. You can do it manually, or perhaps hire an intern to do it . But, fortunately for all these interns, the android studio is ready to help us with this.

When you put your carriage (or click on the redundant cast), the android studio will offer 2 options for processing the extra conversion.

You will first be asked to remove this redundant cast, or you can choose to clear the code. This will remove all unnecessary conversions for this file. This is better, but we want more. We do not want to open each file and clear it one by one.

A feature of IntelliJ's idea is that this function is called intentional action. All you have to do is press Ctrl + Shift + A and then type clean. And select "Clear Code", and select the entire project volume. With these few simple steps, your code will be much cleaner.

One of the important points is that you do this with some version control system of code. This way, you can compare the changes made by the intent action and discard any files you want.

Copied from the original post:

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

0
Apr 27 '19 at 6:34
source share



All Articles