In Activity, created a variable

private TextView txtTitle; 

and in the onCreate() method I refer to it like this

 txtTitle = (TextView) findViewById(R.id.txtTitle); 

Studio gray displays the type to which I cast the variable. Those. now it is not necessary to write (TextView) ?

  • And the question is what? - post_zeew
  • The question is whether you still need to specify the type or not? - Kolhoznik

1 answer 1

In the SDK 26, the signature of the findViewById(...) method has changed. Previously, she was like this:

 public View findViewById(int id); 

and, accordingly, it was necessary to explicitly cast the type. In the SDK 26+, the method is as follows:

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

and type is not necessary.

  • Got it. Thank. - Kolhoznik