4

I have a LinearLayout comprising of a few Buttons and I add this to my activity in the onCreate(..) method with setContentView(R.layout.myscreen). No surprises so far.

How do I get a reference to an iterator to these buttons? I'd like to add listeners to them but I'd rather not directly reference the Button's using their android:id.

Similar questions have been asked here and here but they don't quite answer my question.

Community
  • 1
  • 1
Tim
  • 5,767
  • 9
  • 45
  • 60
  • Is every `Button` going to have the same listener? – Octavian Helm Feb 28 '11 at 11:57
  • At the moment I was going to just create a new listener for each button but I'm open to suggestions. I just want to add an onClickListener to get the text in the button to do something with it. Is there a simpler way to just add a generic listener to the entire View? – Tim Feb 28 '11 at 12:01

3 Answers3

6

Try something like this provide an id root_layout in xml to LinearLayout

LinearLayout mLayout = (LinearLayout) findViewById(R.id.root_layout);
    for(int i = 0; i < mLayout.getChildCount(); i++)
    {
        Button mButton = (Button) mLayout.getChildAt(i);
        mButton.setOnClickListener(this);
    }

Where mLayout is object of you Linear Layout and Your activity must implements OnClickListener and here goes general listener

@Override
public void onClick(View v)
{
    Button mButton = (Button)v;
    String buttonText = mButton.getText().toString();

}

NOTE: For this to work properly you Linear Layout must only contains button no other views

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • Thank you Tox1c. I hate asking this question but how do you access the Layout from an Activity? This is one of the things that I hate in Android - I **know** something is in there somewhere to do something but because the API is so vast I just can't find it. There is a `setContentView(..)` method but no `getContentView(..)` – Tim Feb 28 '11 at 12:28
  • If I pretend to keep a reference to the view for the whole life of the activity, should I use a weakreference or nah – Lay González Oct 15 '14 at 17:02
4

You should take a look at my answer here.

In short. I'd assign the buttons a listener by setting the onClick attribute in the XML layout on each Button.

Inside of your Activity you'll need a public method like the one below which basically is what you want to do in your listener.

public void myFancyMethod(View v) {
    // do something interesting here
}
Community
  • 1
  • 1
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • Thank you for your reply Octavian. Didn't know about that XML setting.However in attempting to keep the initial question short and clear I left out the fact that I also want to attach an `onLongClickListener` and `OnFocusChangeListener` to the buttons so I still need to get a reference to the buttons. As a last resort I can just directly reference the Buttons using their `android:id` but it's a little inflexible. Any ideas? cheers – Tim Feb 28 '11 at 12:17
  • btw octavian.. I've tried to also vote your answer up as it is useful also but its not letting me as I was messing around. Can you just edit it slightly (add whitespace) so I can re-vote. – Tim Feb 28 '11 at 12:56
0

If you want to go for accessing other elements you may try following syntax:

<ElementClass> <referencevariable> = (<ElementClass>) findViewById(R.id.<id_of_the_element>);

For Example:

TextView textView= (TextView) findViewById(R.id.t1); //I used t1 to refer my textview in the Layout.

This might work. Then you can use these views with their inbuilt methods to perform as many as work you want.

Veer Shrivastav
  • 5,434
  • 11
  • 53
  • 83