1

I notice that while using the new compileSdkVersion 26 casting is no longer needed in Fragments (android.support.v4.app.Fragment). But I can't get the same method in AppCompatActivity

I have some preliminaries findings of the issue:

a. Inside a Fragment the new findViewById() method is always used and correspond to this:

public <T extends android.view.View> T findViewById(int id) { /* compiled code */ }

b. Initially I though this will mean that has something to do then with the Activity, but using getActivity().findViewById() does the same.

c. That leads me to think that the Activity inherits from another class, but when I trace upwards the getActivity() and AppCompatActivity I found both extends 'FragmentActivity'

d. While trying to use the findViewById() method in an Activity both methods are suggested, but finally casting is always needed despite selected the desired one.

e. I have found a similar question here. There is stated that the new findViewById() method belongs to View so I try to found a View inside a View, something like this TextView tv = otherView.findViewById() but still the casting is needed.

So, how can I use the new findViewById() method in an Activity?

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • 1
    "But I can't get the same method in AppCompatActivity" -- are you using `26.0.0` of `appcompat-v7`? [`AppCompatActivity` was updated to return `T` from `findViewById()`](https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#findViewById(int)). – CommonsWare Aug 08 '17 at 21:15
  • @CommonsWare your answer help me to figure it out! I was using `compile 'com.android.support:appcompat-v7:26.+'` and couldn't change it before because even when I try with the `0.0` versions then the sync would fail because the dependency was missing, that lead me to this answer https://stackoverflow.com/a/45481709/4017501 so there is only to wait for people on Android Studio make it by default – cutiko Aug 08 '17 at 21:36

2 Answers2

0

The code is actually very simple and you can add it yourself to any class that needs it (or to a common base class):

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

You can even rename it to anything you want in order to resolve potential name collisions.

Have been using it long before Google finally added it to the framework.

Vasiliy
  • 16,221
  • 11
  • 71
  • 127
  • This is a great work around, but I'm looking specifically to use the new method, thanks for your help – cutiko Aug 08 '17 at 21:08
0

I want to thanks, @CommonsWare because he's comment lead me to the solution.

I was using compile 'com.android.support:appcompat-v7:26.+' and couldn't change it before because even when I try with the 0.0 versions then the sync would fail because the dependency was missing, that lead me to this answer.

Again, as @CommonsWare point out the solution is to use the AppCompatActivity provided in the library 'com.android.support:appcompat-v7:26.0.0'. For doing it so you have to include Maven Google in your project dependencies url "https://maven.google.com".

This is kind of a bummer because now there is only left to wait for people on Android Studio make it by default.

gradle:project

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

gradle:app

dependencies {
    compile 'com.android.support:appcompat-v7:26.0.0'
}
cutiko
  • 9,887
  • 3
  • 45
  • 59