0

I'm trying to write listen for button

button = (Button) v.findViewById(R.id.choose_group_button); 

how to write lister for button?

Andrej
  • 828
  • 1
  • 15
  • 25
  • possible duplicate of [Android OnClickListener - identify a button](http://stackoverflow.com/questions/3320115/android-onclicklistener-identify-a-button) – span Dec 07 '13 at 22:51

2 Answers2

4

like this.

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do your stuff             
            }
        });
5er
  • 2,506
  • 7
  • 32
  • 49
0

Two ways, either:

  1. Assign an on click event to your button.

    findViewById(R.id.button_id).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
        }
    });
    
    • For this, in your xml file for your button view you'd set:

      android:onClick="onClick"
      
    • And in your class you'd have your on click method which would look like:

      @Override
      public void onClick(View view){
          switch(view.getId()){
              case R.id.button_id:
              // do stuff
              break;
          }
      }
      
Joe Birch
  • 371
  • 2
  • 7
  • FYI: First option has problems with casting, second options mixes up two other approaches. If you use the `onClick` attribute, you don't need an `OnClickListener` - just a `public void` method taking a single `View` arg. If you go with the `implements` route, just `setOnClickListener(this)`. – laalto Dec 08 '13 at 08:36
  • oh yea, was late last night ha, the 'OnClickListener' wouldn't be needed. – Joe Birch Dec 08 '13 at 10:26