Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What codes am i going to use if i will change the buttons here into ImageButtons where i can play with pictures and not letters. Here's my code using Buttons only.

Java
btn1 = (Button) findViewById(R.id.btn1);
       btn2 = (Button) findViewById(R.id.btn2);
       btn3 = (Button) findViewById(R.id.btn3);
       btn4 = (Button) findViewById(R.id.btn4);
       btn5 = (Button) findViewById(R.id.btn5);
       btn6 = (Button) findViewById(R.id.btn6);

       btn1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckButton(btn1, btn2);
               CheckButton(btn1, btn4);

           }
           });
           btn2.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckButton(btn2, btn1);
               CheckButton(btn2, btn3);
               CheckButton(btn2, btn5);
           }
           });
           .........and so on.
   }

        private void CheckButton(final Button btn1, final Button btn2) {
                   if (btn2.getText().equals(""))
                   {
                       btn2.setText(btn1.getText().toString());
                       btn1.setText("");
                   }


            }



Here's my codes for Image Buttons which has problems with ".setBackground(null)".


Java
ibtn1 = (ImageButton) findViewById(R.id.ibtn1);
       ibtn2 = (ImageButton) findViewById(R.id.ibtn2);

       ibtn1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckiButton(ibtn1, ibtn2);


           }
           });
       ibtn2.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               CheckiButton(ibtn2, ibtn1);


           }
           });
private void CheckiButton(final ImageButton ibtn1, final ImageButton ibtn2) {
                   if (ibtn2.getBackground().equals(""))
                   {
                       ibtn2.setBackground(ibtn1.getBackground());
                       ibtn1.setBackgroundResource("");
                   }


            }
Posted

1 solution

You must also change the type in the layout (found in res\layout). It is XML and there the actual control is defined as Button and needs to be changed to ImageButton.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900