Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use the same OnClickListener for different buttons?

Tags:

android

button

In android, can I use the same OnClickListener for different buttons? If yes, how do I get which button the click is generated from? I currently have 4 buttons and each button have their own OnClickListener. Each of the OnClickListener are doing the same exact thing with the exception of getting the text of the button being clicked. I want to create a single OnClickListener but I can't figure out how to determine which button is being clicked. Thank you

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        setContentView(R.layout.home);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);

        //Load First Word
        button1.setOnClickListener(button1ClickListener);
        button2.setOnClickListener(button2ClickListener);
        button3.setOnClickListener(button3ClickListener);
        button4.setOnClickListener(button4ClickListener);

    }

Code for the listener with the the different part in bold

private OnClickListener button1ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button1);**
        handleButtonClick(button);
    }
};

private OnClickListener button2ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button2);**
        handleButtonClick(button);

    }
};

private OnClickListener button3ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button3);**
        handleButtonClick(button);

    }
};

private OnClickListener button4ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button4);**
        handleButtonClick(button);

    }
};

Code for handleButtonclick

private void handleButtonClick(Button button) {
        if(button.getText().equals(currentWord)){
            currentScore += availableScore;
            TextView score = (TextView)findViewById(R.id.textViewScore);
            score.setText(String.valueOf(score));
            currentIndex++;
            availableScore = 4;
            InitializeGame();
        }
        else{
            availableScore--;
            button.setEnabled(false);
        }
    }

With recommendation from Karthik I modified my code to the following:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        setContentView(R.layout.home);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);

        //Load First Word
        button1.setOnClickListener(buttonClickListener);
        button2.setOnClickListener(buttonClickListener);
        button3.setOnClickListener(buttonClickListener);
        button4.setOnClickListener(buttonClickListener);

    }

OnClickListenerCode

private OnClickListener buttonClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            handleButtonClick((Button)arg0);
        }
    };
like image 322
atbebtg Avatar asked May 26 '11 03:05

atbebtg


2 Answers

Yes it is possible. I have written an example below that should be relatively straight forward.

As usual, add the OnClickListener for all the buttons as follows:

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

then add the onClick() event as shown below:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == btn1){
        //Things to do  
    }
    if(v == btn2){
        //Things to do      
    }
    if(v == btn3){
        //Things to do  
    } 
}

This should work just fine. Don't forget to implent the View.OnClickListener for your class in which your onCreate is present otherwise all those statements that set the OnClickListener above will be incorrect.

like image 80
Karthik Avatar answered Sep 30 '22 05:09

Karthik


    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int arg0) {
                    // TODO Auto-generated method stub

                }
            });
            setContentView(R.layout.home);
            Button button1 = (Button)findViewById(R.id.button1);
            Button button2 = (Button)findViewById(R.id.button2);
            Button button3 = (Button)findViewById(R.id.button3);
            Button button4 = (Button)findViewById(R.id.button4);

            //Load First Word
            button1.setOnClickListener(button1ClickListener);
            button2.setOnClickListener(button2ClickListener);
            button3.setOnClickListener(button3ClickListener);
            button4.setOnClickListener(button4ClickListener);

        }

    @Override
        public void onClick(View v)

    {

    int id = v.getId();
    switch (id) {  
    case R.id.button1:  
    //your code
    break;
 case R.id.button2:  

    break;
 case R.id.button3:  

    break;
    }
    }
like image 20
Tanmay Mandal Avatar answered Sep 30 '22 05:09

Tanmay Mandal