You can use array to handle several button click listener in android like this: here i am setting button click listener for n buttons by using array as:
Button btn[] = new Button[n];
NOTE: n is a constant positive integer
Code example:
//class androidMultipleButtonActions
package a.b.c.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class androidMultipleButtonActions extends Activity implements OnClickListener{
Button btn[] = new Button[3];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn[0] = (Button) findViewById(R.id.Button1);
btn[1] = (Button) findViewById(R.id.Button2);
btn[2] = (Button) findViewById(R.id.Button3);
for(int i=0; i<3; i++){
btn[i].setOnClickListener(this);
}
}
public void onClick(View v) {
if(v == findViewById(R.id.Button1)){
//do here what u wanna do.
}
else if(v == findViewById(R.id.Button2)){
//do here what u wanna do.
}
else if(v == findViewById(R.id.Button3)){
//do here what u wanna do.
}
}
}
Note: First write an main.xml file if u dont know how to write please mail to: [email protected]