I was trying to do a memory game on android, but i can't figure out the code that matching 2 button with the same image..
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class Memory extends Activity {
TextView tv;
int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.one);
btn1.setBackgroundResource(R.drawable.background);
Button btn2 = (Button) findViewById(R.id.two);
btn2.setBackgroundResource(R.drawable.background);
btn1.setOnTouchListener(btn1OnTouchListener);
btn2.setOnTouchListener(btn2OnTouchListener);
String match1 = "btn1=btn2";
String ans1a = match1.substring(0, match1.indexOf("="));
String ans1b = match1.substring(match1.indexOf("=") + 1, match1.length());
}
OnTouchListener btn1OnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.puppy2);
if (count < 2)
count = count + 1;
//matching code?
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
while (count==2) {
count = 0;}
//matching code?
}
return false;
}
};
OnTouchListener btn2OnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.puppy8);
if (count < 2)
count = count + 1;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
}
return false;
}
};