Casting view findviewbyid to textview is redundant là gì năm 2024

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.infoangkotdescription));

While the new way would be like this

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

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.

Common Problem

If you facing a problem make sure you setup your android library and your google library to the latest (currently version 26.0.0 & 11.0.4) in your app build.gradle.

@wbrunette @linl33 While I was going through the code-base of Android Common for finding the Redundant Castings, there were several instances of suggestion by Android Studio to replace some Anonymous Methods with Lambda Expressions. Same is the case with Survey and Tables also. For example, consider the following code snippet -

new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
          //Some Code
          }
}

The above method was being suggested to be replaced with the following -

(dialog, which) -> {

          //Some Code
        }
This replacement would reduce the number of lint warnings and also make some imports unusable and hence we can remove them too. So shall I replace these Anonymous Methods with the Lambda Expressions?

findViewById trong Android là gì?

findViewById là nguồn gốc của nhiều bug xảy ra trong Android. Nó có thể liên kết đến một id mà không có trong layout tham chiếu của bạn - tạo ra null và lỗi crash. View binding thay thế findViewById một cách ngắn gọn và an toàn hơn.nullSử dụng view binding thay thế cho findViewById trong Android - Vibloviblo.asia › su-dung-view-binding-thay-the-cho-findviewbyid-trong-andro...null