for(int i=0;i<n;i++)
{
Button btn = new Button(this);
btn.setLayoutParams(new LinearLayout.LayoutParams(150, 45));
btn.setId(i);
linearLayout.addView(btn);
try {
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent userTry = new Intent(Ed.this,Op.class);
startActivity(userTry);
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("onCreate", "Activity Not Found", anfe);
}
Android : I want to dynamically create button such that it passes it id to the next activity it goes to. But i am getting an error. Thanks for the help
You’ll have to pass the Bundle with the id:
Also, you are setting the id in a wrong way. The method setId (int) is expecting an id which is registered in your resources (e.g, R.id.itemId)