Senior Software Engineer @ Originate
// method signature
public <T extends View> T findViewById(int id);
// prior to api 26
public View findViewById(int id);
// java
TextView textView = (TextView) findViewById(R.id.my_text_view);
// prior to api 26
TextView textView = findViewById(R.id.my_text_view);
// kotlin
val textView: TextView = findViewById(R.id.my_text_view)
// prior to api 26
val textView: TextView = findViewById(R.id.my_text_view) as TextView
// plugin
apply plugin: 'com.jakewharton.butterknife'
// dependencies
api "com.jakewharton:butterknife:$butterknifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"
@BindView(R.id.my_text_view) TextView textView;
@BindString(R.string.my_string) String string;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
ButterKnife.bind(this);
}
// app/build.gradle
android {
...
dataBinding.enabled = true
}
<layout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_string" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MyLayoutBinding binding =
DataBindingUtil.setContentView(this, R.layout.my_layout);
binding.my_text_view.setText("Hello OC Android");
// app/build.gradle
apply plugin: 'kotlin-android-extensions'
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Instead of findViewById<TextView>(R.id.textView)
textView.setText("Hello, world!")
}
}
public View _$_findCachedViewById(int var1) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View var2 = (View)this._$_findViewCache.get(var1);
if (var2 == null) {
var2 = this.findViewById(var1);
this._$_findViewCache.put(var1, var2);
}
return var2;
}
// app/build.gradle
android {
...
viewBinding.enable = true
}
// can also pass ViewGroup parent, boolean attachToParent to inflate
MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflator)
// if in activity
setContentView(binding.root)
binding.my_text_view.text = "Hello OC Android"
public final class FragmentSecondBinding
implements ViewBinding {
@NonNull
private final ConstraintLayout rootView;
@NonNull
public final TextView myTextView;
private FragmentSecondBinding(
@NonNull ConstraintLayout rootView,
@NonNull TextView myTextView) {
this.rootView = rootView;
this.myTextView = myTextView;
}
@Override
@NonNull
public ConstraintLayout getRoot() {
return rootView;
}
@NonNull
public static MyLayoutBinding inflate(@
NonNull LayoutInflater inflater) {
return inflate(inflater, null, false);
}
@NonNull
public static MyLayoutBinding inflate(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup parent,
boolean attachToParent) {
View root = inflater.inflate(
R.layout.my_layout, parent, false);
if (attachToParent) {
parent.addView(root);
}
return bind(root);
}
@NonNull
public static MyLayoutBinding bind(
@NonNull View rootView) {
// The body of this method is generated
// in a way you would not otherwise write.
// This is done to optimize the compiled
// bytecode for size and performance.
String missingId;
missingId: {
TextView myTextView = rootView.findViewById(
R.id.my_text_view);
if (myTextView == null) {
missingId = "myTextView";
break missingId;
}
return new MyLayoutBinding(
(ConstraintLayout) rootView, myTextView);
}
throw new NullPointerException(
"Missing required view with ID: "
.concat(missingId));
}