Using Butterknife to inject an array of views

Currently I have this array of views:

ImageView activityImageViews[] = {
    (ImageView) rootView.findViewById(R.id.img_activity_1),
    (ImageView) rootView.findViewById(R.id.img_activity_2),
    (ImageView) rootView.findViewById(R.id.img_activity_3),
    (ImageView) rootView.findViewById(R.id.img_activity_4)
};

Is there a way I could use Butterknife to inject all those views? I need to keep them in an array (or in a way so I can iterate over them).

  • Destroy item from the ViewPager's adapter after screen orientation changed
  • Building Android from source - emulator and AVDs
  • How to add the bubbles to textview android?
  • <size> attribute not useful when using layer-list?
  • Camera.open() returns null
  • Android WebView style background-color:transparent ignored on android 2.2
  • Android ListView headers
  • Center text in a toast in Android
  • what causes “constant expression required” errors for the generated R.id.xxx values in switch statements?
  • How to make an android app return to the last open activity when relaunched?
  • Android intent filter for a particular file extension?
  • ADB Error codes
  • 2 Solutions collect form web for “Using Butterknife to inject an array of views”

    Currently it is possible to inject several views as array. From ButterKnife documentation (see VIEW LISTS section)

    @InjectViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
    List<EditText> nameViews;
    

    Same thing for multiple click listeners:

    @OnClick({ R.id.door1, R.id.door2, R.id.door3 })
    public void pickDoor(DoorView door) {
      if (door.hasPrizeBehind()) {
        Toast.makeText(this, "You win!", LENGTH_SHORT).show();
      } else {
        Toast.makeText(this, "Try again", LENGTH_SHORT).show();
      }
    }
    

    I suppose that the elements added to the Array are dynamically, otherwise you can simply declare them in your header one by one and afterwards add them to the array:

    // in the header:
    @InjectView(R.id.imageview1) ImageView imageView1;
    @InjectView(R.id.imageview2) ImageView imageView2;
    @InjectView(R.id.imageview3) ImageView imageView3;
    
    // inside your code:
    ImageView activityImageViews[] = {
        imageView1, imageView2, imageView3
    };
    

    But however, if the elements are dynamic (in amount of elements/id of elements etc.) the only thing you can do is to use ButterKnife.findById which will spare the ImageView cast – you can’t inject them via annotation because Butterknife does not know what to generate/inject on compile time (the information is first available on runtime).

    ImageView activityImageViews[] = {
        ButterKnife.findById(view, R.id.img_activity_1),
        ButterKnife.findById(view, R.id.img_activity_2),
        ButterKnife.findById(view, R.id.img_activity_3),
        ButterKnife.findById(view, R.id.img_activity_4)
    };
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.