user2875404
user2875404

Reputation: 3218

ANDROID onTouchListener on button array

I have put a lot of buttons (16*16) in a button array. The buttons numbers are directly related to changes they should do in another array (e.g. button[12][7] sets the value of stat[12][7] to 1)

So I thought it's possible to put on single line in the onTouch method that reacts to every button. Example (of course, not working)

public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
if(arg0 == button[int a][int b]){stat[a][b]=1};

In this pseudocode, the button would create 2 ints that describe the 2 dimensions of the array which get passed to the stat array.

If anyone had a solution for this, he would save me a few hours this night.

Thanks for your answers.

Upvotes: 4

Views: 572

Answers (4)

damian
damian

Reputation: 2011

Try something like this:

Button[][] myButtonMatrix = new Button[] {
 new Button[] { button11, button12, button13, button 14 },
 new Button[] { button21, button22, button23, button24 }
};

public class MatrixButtonListener implements View.OnClickListener {
        private int x;
        private int y;

        public MatrixButtonListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() { return x; }
        public int getY() { return y; }

        @Override
        public void onClick(View v) {
            stat[x][y] = x-y; // changes were made only in relation to x and y, nothing else
            // for example:
            if(x == 0) { 
                // button in first row
                // do something
            }
        }
    };

// to apply to each button in matrix:

for(int i=0; i<myButtonMatrix.length; i++) {
    for(int j=0; j<myButtonMatrix[i].length; j++) {
         myButtonMatrix[i][j].setOnClickListener(new MatrixButtonListener(i,j));
    }
}

What this is supposed to do:

Create a generic OnClickListener class, which takes the x and y position as parameter, so each onClickListener has the same behaviour, but differnet x and y positions, depending on the button itself.

Note: This is not tested.

EDIT:

Another way would be a custom button class, which you use, which contains the X/Y coordinates as well. Simply add onClickListener to each button, cast it back to your custom view, and ask for x/y.

Upvotes: 2

NigelK
NigelK

Reputation: 8490

As you add each button to the array, set a tag that indicates its indices. Tags are there for adding properties to a view without having to resort to another data structure.

For example:

button[12][7].setTag("12|7");

If your button were pre-defined in XML, you could do the same with:

android:tag="12|7"

Then in the touch listener (I assume the same one is attached to all the buttons), get the tag from the view that was touched:

String tag = (String) view.getTag();

Then substring out and use the two indexes as required:

String indx1 = tag.substring(0, tag.indexOf("|"));
String indx2 = tag.substring(tag.indexOf("|")+1);
stat[Integer.parseInt(indx1)][Integer.parseInt(indx2)] = 1;

Upvotes: 2

Ismail Sahin
Ismail Sahin

Reputation: 2710

I think HasMap is a better solution

private HashMap<Integer,Button> btnMap = new HashMap<Integer, Button>();

private void init(){
    Button yourFirstBtn = (Button) findViewById(R.id.yourFirstBtn);
    btnMap.put(yourFirstBtn.getId(), yourFirstBtn);

    for(Button tempBtn: btnMap.values()){
        tempBtn.setOnClickListener(this);
    }
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    Button clickedBtn = btnMap.get(v.getId());
}

Upvotes: 2

Josh Li
Josh Li

Reputation: 101

Are you adding the onTouchListener to the buttons' container?

Your best bet is to add an onTouchListener to each button, and then arg0 will correspond to the specific button.

Another option would be to use a GridView, which has an setOnItemClickListener you can use. http://developer.android.com/reference/android/widget/GridView.html

Upvotes: 2

Related Questions