35

I recently updated my android SDK and build tools to API 26 in Android Studio and I directly noticed that Android Studio was marking my view casts as "redundant" when I do something like this:

TextView itemName = (TextView) findViewById(R.id.menuItemName);

After some research, I found that since SDK 26, findViewById uses Java 8 features to return the same object type, but what I wanted to know is if it is completely safe to remove all casts. Will this cause any issues on Android prior to 26? Any more info on this would be helpful as I didn't find much on the internet. Thanks in advance.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Amr El Aswar
  • 3,395
  • 3
  • 23
  • 36
  • It is not and Answer, Just Suggestion In Old version(SDK Below 26) `TextView name=(TextView) findViewById(R.id.TextView);` Above SDK 26 `TextView name= findViewById(R.id.TextView);` – Rajesh Naddy Nov 13 '17 at 13:15

1 Answers1

42

The method signature changed as you noticed and now it looks like:

public <T extends View> T findViewById(int id);

compared to the old (pre SDK 26) one:

public View findViewById(int id);

so as long as you use SDK 26 (or newer) to compile your project you can safely remove the casting from your code as you will be using new findViewById() which no longer requires it.

so having a lower minSdk than 26 will not cause an issue ?

No, neither minSdk nor targetSdk really matter. What matters is compileSdk which must be 26 or higher.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 3
    so having a lower minSdk than 26 will not cause an issue ? Could you also explain why the support library plays a role in this ? Does the support library manage the casting for you by itself on android versions prior to 26? – Amr El Aswar Jul 13 '17 at 23:57
  • 1
    Yeah basically the android support library makes it easier for you to implement newer functionality without having to check which SDK version is the device running. You can learn more about it [here](https://developer.android.com/topic/libraries/support-library/index.html) – Hugo sama Jul 14 '17 at 00:06
  • 4
    This isn't something that requires the support library - just compiling with SDK 26 is enough. – ianhanniballake Jul 15 '17 at 01:05
  • @ianhanniballake that was exactly what I was thinking, the View class from sdk 26 is the one that changed, doesn't seem to berelated to the support libraries, that is why I was asking why the support libraries would be necessary – Amr El Aswar Jul 15 '17 at 12:51
  • Does it mean that reflections are used in SDK 26? Or can Java 7/8 make type casting automatically? I am confused. – soshial Aug 22 '17 at 12:30
  • No, no reflection is neither used nor really needed here. – Marcin Orlowski Sep 06 '17 at 17:36
  • is there any way to remove all of the castings automatically instead of one by one? – HendraWD Oct 30 '17 at 05:57
  • You can try global replace like `(TextView)findViewById(` by `findViewById(` but you'd need to repeat that for each class you cast to. From other hand I do not expect you have that many so I'd just run `lint` on your project and then fix all casts it would point out by hand. – Marcin Orlowski Oct 30 '17 at 06:33
  • Could you explain how it works as the View class is taken not from support library but from bundles on users' OS? So if there is an old android before 26 api, how such method could be found? Whatever compileSdk I use. Do I include the whole sdk to my apk or what? – Oleksandr Albul Nov 18 '17 at 08:33
  • It's not about `View` class at all but even if it would all widgets extends the same `View`, so inheritance is the same. – Marcin Orlowski Nov 19 '17 at 19:40