0

When you have many buttons in a view and all the button have listener. Your main activity gets dirty. Anyone know how to organize listeners ? Currently I used this way and implement onClickListener.

    spotify =(Button)findViewById(R.id.spotifyBtn);
    superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
    libraryBtn = (Button) findViewById(R.id.libraryBtn);
    buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
    capstoneBtn= (Button) findViewById(R.id.capstoneApp);


    spotify.setOnClickListener(this);
    superDuoBtn.setOnClickListener(this);
    libraryBtn.setOnClickListener(this);
    buildBiggerBtn.setOnClickListener(this);
    capstoneBtn.setOnClickListener(this);
fiddlest
  • 1,262
  • 2
  • 17
  • 42

6 Answers6

2

You could set the property:

android:onClick="buttonClicked"

in the xml file for each of those buttons, and use this in the java code:

public void buttonClicked(View view) {

        if (view.getId() == R.id.button1) {
                // button1 action
            } else if (view.getId() == R.id.button2) {
                //button2 action
            } else if (view.getId() == R.id.button3){
                //button3 action
            }
    }
Sujithrao
  • 789
  • 3
  • 12
  • 27
1

You can implement onclicklistner for multiple buttons using swith case

@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.firstButton:
        // do your code
        break;

    case R.id.secButton:
        // do your code
        break;

    case R.id.thirdButton:
        // do your code
        break;

     ......

    default:
        break;
    }

}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
1

Ya...It s the best way to use multiple onClickListener.

spotify =(Button)findViewById(R.id.spotifyBtn);
    superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
    libraryBtn = (Button) findViewById(R.id.libraryBtn);
    buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
    capstoneBtn= (Button) findViewById(R.id.capstoneApp);



   spotify.setOnClickListener(this);
    superDuoBtn.setOnClickListener(this);
    libraryBtn.setOnClickListener(this);
    buildBiggerBtn.setOnClickListener(this);
    capstoneBtn.setOnClickListener(this);



@Override
    public void onClick(View v) {

        Intent intent = null;

        switch (v.getId()) {
            case R.id.spotifyBtn:
                intent = new Intent(this, SimpleSingleExample.class);
                break;

            case R.id.superDuoBtn:
                intent = new Intent(this, CustomExample.class);
                break;

            case R.id.libraryBtn:
                intent = new Intent(this, SequenceExample.class);
                break;

            case R.id.buildItBiggerBtn:

                Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
                break;
        }

        if(intent!=null){
            startActivity(intent);
        }
    }
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48
1

If you want better way than you have to use Android Annotations, its simple and useful, you can find here

Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
1

Add those View object references to some type of list, iterate through it usin a for-each loop, then call the setOnClickListener on each element which will reduce those lines to just 2 lines for you.

ArrayList <View> list = new ArrayList <>(spotify,superDuoBtn,libraryBtn, buildBiggerBtn, capstoneBtn);
for (View view : list) {
    view.setOnClickListener(this);
}
Trash Can
  • 6,608
  • 5
  • 24
  • 38
0

The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks. As far as I know, there are four different ways to add listeners for handling button clicks. If you know of other ways, please post a comment and share them with us.

Xml

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:text="Inner Class (btn1)" android:id="@+id/Button01"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
        </Button>
        <Button android:text="Anonymous Inner Class (btn2)"
            android:id="@+id/Button02" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </Button>
        <Button android:text="Implementing an Interface (btn3)"
            android:id="@+id/Button03" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </Button>
        <Button android:text="Calling From XML Layout (btn4)"
            android:id="@+id/Button04" android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="btn4Listener">
         </Button>
    </LinearLayout>

in MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity implements View.OnClickListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      //method 1 - uses an inner class named btn1Listener...
      Button btn1 = (Button)findViewById(R.id.Button01);
      btn1.setOnClickListener(btn1Listener);


      //method 2 - use an anonymous inner class as a listener...
      Button btn2 = (Button)findViewById(R.id.Button02);
      btn2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              showToastMessage("You clicked btn2 - uses an anonymouse inner class");
          }
      });

      //method 3 - note that this class implements
      //the View.OnClickListener interface
      //which means that we must implement the onClick()
      //method (which you'll find below)..
      Button btn3 = (Button)findViewById(R.id.Button03);
      btn3.setOnClickListener(this);
      //method 4 - look at the method btn4Listener() below       
  }


  //here's the inner class used as a listener for btn1...

  private View.OnClickListener btn1Listener = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
      }
  };


  //here's a method that you must have when your activity implements the
  //View.OnClickListener interface...
  @Override
  public void onClick(View v) {
      showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
  }
  //here's the handler for btn4 (declared in the xml layout file)...
  //note: this method only works with android 2.1 (api level 7), it must be public and
  //must take a single parameter which is a View

  public void btn4Listener(View v) {
          showToastMessage("You clicked btn4 - listener was set up in the XML layout");
  }
  private void showToastMessage(String msg){
      Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
      toast.show();
  }
}
Mr Robot
  • 1,747
  • 6
  • 35
  • 67