-2

When I create an Android app, I use a switch statement in a separate class to handle the behavior of multiple buttons. When I click a button, how can I detect which button was pressed, from another class?

The other class is not an activity class. Can anyone comment on the code below, so I'll better understand how to implement this correctly.

My first method in class A is

 public void onClick(View v){
    switch (v.getId()){
                case R.id.button1 :
                    GifView.mymethod(R.id.button1);
                final Dialog dialog1=new Dialog(context);
                dialog1.setContentView(R.layout.dialog);

                Button finish1= (Button) dialog1.findViewById(R.id.finish);
                finish1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog1.dismiss();
                    }
                });
                dialog1.show();
                    break;

I would like to pass this button id to another class, which would have the business logic for handling all button clicks.

private void init(Context context) {
    switch(?????){
        case R.id.button1:
            setFocusable(true);
            gifInputStream = getResources().openRawResource(R.drawable.hen);

            gifMovie = Movie.decodeStream(gifInputStream);
            movieWidth = gifMovie.width();
            movieHeight = gifMovie.height();
            movieDuration = gifMovie.duration();
            break;

what can i do inside the switch statement?

public static int mymethod(int id) {
    int buttonId=id;
    return buttonId;
}
Roy Falk
  • 1,685
  • 3
  • 19
  • 45
  • The correct way in Android is to have each button call a different onClick method. Any reason why you're going this route? Are you trying to create a whole bunch of buttons dynamically and programmatically? – Roy Falk Feb 21 '16 at 17:22
  • I think that [this answer](http://stackoverflow.com/a/34178641/4871526) can help you. – Jose Angel Maneiro Feb 21 '16 at 17:33
  • brother , i access this button click from another class. but when i wants to access this inside a method using switch statement from second class. which is the way to access this?@ Roy Falk – Akib Mahmud Feb 21 '16 at 17:41
  • 1
    @RoyFalk: this `The correct way in Android is to have each button call a different onClick method` is not true. – Marcin Orlowski Feb 21 '16 at 17:45
  • thanks eveyone.this probleam is solved. – Akib Mahmud Feb 21 '16 at 18:08
  • Akib. If the answer below solved it, please accept it. If the comment by Jose did, flag the post as duplicate. – Roy Falk Feb 21 '16 at 19:07
  • @marcin-orlowsky correct is probably too strong in this case. For most cases, especially the simple ones, I do not see the value of not following the Google tutorial. I googled this and people recommend this as a way to consolidate listeners and then give toy examples such as Log.d("button a pressed"); However, since I've been wrong before, could you explain or link to a use case for this? thank you. – Roy Falk Feb 21 '16 at 19:19

1 Answers1

-1

You could have a public static global variable and then inside your switch do something like:

public static int buttonClicked = 0;

...

switch(id){

  case R.id.button1:
    //doWhatever()
    buttonClicked = R.id.button1;
    break;
  more cases:
  break;
}

And then if you want you can create a getter method for buttonClicked so you can get the actual value whenever you want from outside the Activity.

This way you're not passing the actual reference of the clicked button, which could be problematic, you're just passing its R.id. If you do it like this, you won't be able to modify the Button you've clicked from outside the Activty, but you will be able to know what button was clicked through its R.id.

Miquel Perez
  • 445
  • 1
  • 6
  • 17