1

Hey I am making an android app that will have ~256 buttons. Because I dont want to write the very same code for everyone of these I thought it might be possible to realize an easier solution via arrays. My approach in the onCreate to set the listeners was:

1    for (int i=1; i<32; i++)
2               {
3                   button[i] = (Button)findViewById(R.id.button[i]);
4                   button[i].setOnTouchListener(this);
5               }

I set the Button[] like that: Button[] button=new Button [64];

Now, eclipse tells me in line 3 "button cannot be resolved or is not a field" and it just underlines the word "button", so I think it ignores/just does not recognize the [i] (array)-stuff.

The rest of my code seems to get on with that perfectly because it gets recognized as an object (correct me if I said that wrong) but the findViewById() doesn't get on with it ..

Thanks for the replies, Alex

2
  • 1
    You cant't do this. Better You go with dynamically adding buttons. Commented Jul 26, 2013 at 14:31
  • Really? Damn.. Well do you mean I should add all Buttons in my code? Will there be a difference? I am afraid of re-writing my whole code just to notice that I am standing in front of the same problem, again. Commented Jul 26, 2013 at 14:35

2 Answers 2

1

You can't do what you proposed in your solution. A better way to go about it is to add the buttons dynamically in code. For instance,

View parentView = (LinearLayout) findViewById(R.id.parentView);
// declare button array above
for (int i=1; i<32; i++)
{
    Button btn = new Button(context);
    // EDIT: adding a background resource
    btn.setBackgroundResource(R.layout.button_layout);
    btn.setText("This is my text");
    btn.setOnTouchListener(this);
    button[i] = btn;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer, but I really did it like that and it really works - what should be wrong about it? I can send you the code if you don't believe me. I don't want to add the buttons like you said because I have different images for these buttons in use and I think it is just easier then to do it through the XML
0

User "Horschtele" answered it in a perfect way but he deleted his answer on his own (don't know why).

Horschtele, if you read that, I just want to say that this solution is just perfect!

I have to (or at least I think I have to) do this for every tableRow but this saves me an infinite amount of time. Thanks again Horschtele (are you german? :))

My modified version of Horschtele's answer if you already have your buttons in a table:

ViewGroup container = (ViewGroup) findViewById(R.id.tableRow1);

            for(int i=0; i<container.getChildCount();i++){
                System.out.println(container.getChildCount());
            Button button = (Button)container.getChildAt(i);
            button.setOnTouchListener(this);
            }

(don't wonder about the println, you can easily check if the system correctly recognizes the container you are refering to).

If you did it my way with an array of Button then this is the way to go:

button[i] = (Button)container.getChildAt(i);
button[i].setOnTouchListener(this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.