So I’m figuring this stuff out in baby steps. And just got my button to toggle the way i want it. But now i want to add more buttons.
`public class Menu extends Activity{
ImageButton select;
int isClicked = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
select = (ImageButton)findViewById(R.id.select);
select.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked == 0){
select.setImageResource(R.drawable.select_pressed);
isClicked = 1;
}
else{
select.setImageResource(R.drawable.select);
isClicked = 0;
}
}});
}
}`
So say i were to copy that ImageButton method. Where exactly would i insert it, if i were going to use the code for a new button?
`<ImageButton
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
</ImageButton>`
You can use the tag attribute on the button to store the state. Then you could put your state logic in a separate method like this:
This method you could call from each ImageButton’s clicklistener, without saving the state of numerous ImageButtons in the application context.