Slobodan Vrhovac
Slobodan Vrhovac

Reputation: 25

Android:How to set listener on multiple image buttons?

First of all, I'm using Andorid and Java only for a week and I'm a total newbie.

I need to know which button user clicked and then compare it with good or bad answer.

Here is the code

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



        Integer[] flags = {
            R.drawable.flag_albania,
            R.drawable.flag_andorra,
            R.drawable.flag_angola,
            R.drawable.flag_avganistan,

        };

        String[] questions = {
            "What is the flag of Albania",
            "What is the flag of Andorra",
            "What is the flag of Angola",
            "What is the flag of Avganistan",

        };



        TextView tv = (TextView) findViewById(R.id.textView1);
        int arraySize = flags.length;
        Random rand = new Random();

        ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1);
        int index = rand.nextInt(arraySize);
        int questionIndex1 = index;
        button1.setImageResource(flags[index]);
        flags[index] = flags[--arraySize];

        ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
        index = rand.nextInt(arraySize);
        int questionIndex2 = index;
        button2.setImageResource(flags[index]);
        flags[index] = flags[--arraySize];

        ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3);
        index = rand.nextInt(arraySize);
        int questionIndex3 = index;
        button3.setImageResource(flags[index]);
        flags[index] = flags[--arraySize];

        ImageButton button4 = (ImageButton) findViewById(R.id.imageButton4);
        index = rand.nextInt(arraySize);
        int questionIndex4 = index;
        button4.setImageResource(flags[index]);
        flags[index] = flags[--arraySize];

        Integer[] question = {
                questionIndex1,
                questionIndex2,
                questionIndex3,
                questionIndex4
        };

        int questionArraySize = question.length;

        int questionArray = rand.nextInt(questionArraySize);

        tv.setText(questions[question[questionArray]]);

My idea is to compare questionIndex that is randomly selected to a button id but I really don't know how to implement it. Every help is appreciated.

Upvotes: 1

Views: 4232

Answers (5)

Pragnani
Pragnani

Reputation: 20155

Use this reduced code

ImageButton button1,button2,button3,button4;
            ImageButton imagebuttons[]={ button1,button2,button3,button4};
        int ids[]={R.id.imageButton1,R.id.imageButton2,R.id.imageButton3,R.id.imageButton4};

        for(final int i=0;i<imagebuttons.length;i++)
        {
            imagebuttons[i]=(ImageButton) findViewById(ids[i]);
    int index=rand.nextInt(arraySize);
             imagebuttons[i].setImageResource(flags[index]);
             flags[index] = flags[--arraySize];
             indexes.put(i,index);
            imagebuttons[i].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if( questionArray==indexes.get(i))
                    {

                    }

                }
            });

I hope this will help you

Upvotes: 1

Dulan Dissanayake
Dulan Dissanayake

Reputation: 793

You can do the it this way also. You have to use some int variables to keep the tag value of the button. And also you have to create a onClickListner for the button actions. Coding is as follow

 public class MyActivity extends Activity{

    //Define the Tag values for image buttons.
    private static final int TAG_IMAGE_BTN_1 = 0;
    private static final int TAG_IMAGE_BTN_2 = 1;
    private static final int TAG_IMAGE_BTN_3 = 2;
    private static final int TAG_IMAGE_BTN_4 = 3;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        initActivity();

    }

    private void initActivity(){
            //Initialize the image buttons via xml or from the code itself.
            //Add the onClickListner_BTN_CLICK and the corresponding tag to the image button

           imageBtn1.setTag(TAG_IMAGE_BTN_1);
           imageBtn1.setOnClickListener(onClickListner_BTN_CLICK);

           imageBtn2.setTag(TAG_IMAGE_BTN_2);
           imageBtn2.setOnClickListener(onClickListner_BTN_CLICK);
           .......................................................
           .......................................................
    }

    OnClickListener onClickListner_BTN_CLICK = new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int iTag = (Integer)v.getTag();

                switch (iTag) {
                case TAG_IMAGE_BTN_1:

                    break;
                        case TAG_IMAGE_BTN_2:

                    break;

                        case TAG_IMAGE_BTN_3:

                    break;

                        case TAG_IMAGE_BTN_4:

                    break;

                }
            }
        };


    }

The advantages of using the tag values for capture the action, 1)If we use R.id....... then we have to change the value in switch case if we change it in the xml file 2)We can use only one onClickListner for every action in an activity.

Upvotes: 0

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Write below code

public class MainActivity extends Activity implements OnClickListener{ 
    protected void onCreate(Bundle savedInstanceState) {

        /// Your Above Code ///

        tv.setText(questions[question[questionArray]]);

        Imagebutton1.setOnClickListener(this);
        Imagebutton2.setOnClickListener(this);
        Imagebutton3.setOnClickListener(this);
        Imagebutton4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == Imagebutton1){
            // Write Your Code Here
        } else if(v == Imagebutton2){
            // Write Your Code Here
        } else if(v == Imagebutton3){
            // Write Your Code Here
        } else if(v == Imagebutton4){
            // Write Your Code Here
        }
    }
}

Upvotes: 1

GrIsHu
GrIsHu

Reputation: 23638

Try out this way:

button1.setOnClickListener(onClickListener);
  button2.setOnClickListener(onClickListener);
  /**
    *  Common click listener
    */
 OnClickListener onClickListener = new OnClickListener()
  {
    @Override
    public void onClick(View p_v)
    {
        switch (p_v.getId())
            {
                case R.id.imageButton1:
                    //Your logic here.
                    break;
                case R.id.imageButton2:
                    //Your logic here.
                    break;
            }
    }

}

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You can do it this way: First, make your activity implements onClick Listener:

public class MyActivity extends Activity implements View.OnClickListener{ 

Then set the action listeners to your buttons:

button1.setOnClickListener(this);
button2.setOnClickListener(this);
.....

then in the listener, check the id:

public void onClick(View v) {
   switch(v.getId()){
   case R.id.imageButton1:
   break;
   case R.id.imageButton2:
   break;
   ....
   }
}

Upvotes: 0

Related Questions