cauchy000
cauchy000

Reputation: 25

use annotation click when button is created in onCreate

I want to add the @Click Android Annotation to button which is not defined in the layout.xml, but created in onCreate and added programmatically to a SearchView:

@EActivity
public class NewRecipeActivity extends BaseActivity{

  @ViewById(R.id.buttonLoadExtra)
   Button buttonLoadExtra;

  @ViewById(R.id.listViewSearch)
   ListView listViewSearch;

   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newrecipe);

    buttonLoadExtra =  new Button(this);
    buttonLoadExtra.setId(R.id.buttonLoadExtra);
    buttonLoadExtra.setText("Weitere Laden...");


    listViewSearch.addFooterView(buttonLoadExtra);
 } 
 @Click
  void buttonLoadExtra(){ //Do stuff }
  }

Where the id buttonLoadExtra is defined in a resource file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <item name="buttonLoadExtra" type="id" />
</resources>

But it does not work like this. This might be obvious, because the button does not already exist, when linking to the click action is done. So is there a way to solve this or can i use the annotation just for an "existing button".

Thank you in advance

Upvotes: 0

Views: 165

Answers (1)

WonderCsabo
WonderCsabo

Reputation: 12207

Unfortunately the @Click annotation is not working if you are adding the view later, because AndroidAnnotation is setting the listener during setContentView(), and you are adding the view after that runs.

Upvotes: 0

Related Questions