How to use butterknife's Bind() in show dialog function in android

996 views Asked by At

This is my showDialog function, which gets call when someone clicks on a button in activity.

private void showCouponCodeDialog() {

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialoge_apply_coupon);
    dialog.setTitle(R.string.coupon_code);

    final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    Button btnApplyCoupon = (Button) dialog.findViewById(R.id.btnApplyCoupon);
    btnApplyCoupon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);

        }
    });
    dialog.show();
}

when i do

@Bind(R.id.progressBar) ProgressBar progressBar;

It gives error @Bind not applicable to local variable.

This works fine.

final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);

How to use butterknief's Bind() in this case?

1

There are 1 answers

0
R. Zagórski On

Using Butterknife you might use such bind methods:

Butterknife.bind(Dialog dialog)

or

Butterknife.bind (@NonNull Object target, @NonNull Dialog source)

The second one is better as it suggests, where (in which class) binds are hold. Remember, that those functions return Unbinder and it is really imprortant to unbind, when dialog is destroyed, otherwise memory leaks might occur.