Zafar Kurbonov
Zafar Kurbonov

Reputation: 2077

Android - replace findViewById with Butterknife

I have successfully replaced findViewByIds with Butterknife library. Unfortunately, one problem has occured:

final Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup); //popupView.findViewById(...) problem!!!

How can I change this line of code in Butterknife: as Butterknife.bind(this); takes the whole activities Views

Upvotes: 3

Views: 2227

Answers (2)

Zafar Kurbonov
Zafar Kurbonov

Reputation: 2077

First of all, there two thing which is added in dependency to use ButterKnife library:

Buil.gradle

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Then, after using @BindView above onCreate method inside the method write ButterKnife(this, "any other unrelated views to this layout")

"any other unrelated views to this layout" can be another layout's contents such as Popup window or ....

Upvotes: 0

Emanuel
Emanuel

Reputation: 8106

Bind your popupView to your main view using

View popupView = View.inflate(getContext(), R.layout.yourPopup, null);
ButterKnife.bind(this,popupView);

Or you may want to bind it directly using

Button btnPopup = ButterKnife.findById(popupView, R.id.btn_popup);

Upvotes: 4

Related Questions