TextView textView = new TextView(this);
textView.setText(R.string.textView);
textView.setId(View.generateViewId());
Button blueButton = new Button(this);
blueButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
// I can't textView.getId() because textView isn't in this scope
TextView textView = (TextView) findViewById(textView.getId());
textView.setText("You clicked the Button!");
}
}
);
My question is: how do I generate an Id, without just hardcoding some magic number
ie: textView.setId(1);
while still being able to actually access the Id if the variable's scope has died.
Answer: You can use blueButton.setTag(textView) and then in the onClick(View view) method, you can call view.getTag();