Reputation: 13
I have two OnTouchListeners in my code and every time I run it - even if I change the name of the listener - it does not work.
//here is one
public boolean onTouch(View view, MotionEvent event){
// this is for being able to move a spawned image with no id and id not on the activity till created
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
if (no == 1) ;
//do nothing
else {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
return true;
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(" exit")
.setMessage("Are you sure you want to exit?." +
"your progress will not be saved")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(new Intent(getApplicationContext(), level.class));
}
})
.setNegativeButton("No", null)
.show();
}
Button left, right;
ImageView i1, pic2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1_game);
_root = (ViewGroup) findViewById(R.id.root);
addnumbers = new TextView(this);
addnumbers.setText(Integer.toString(number));
_root.addView(addnumbers);
left = (Button) findViewById(R.id.button1);
right = (Button) findViewById(R.id.button2);
left.setOnTouchListener(this);
right.setOnTouchListener(this);
i1 = (ImageView) findViewById(R.id.imageView2);
findViewById(R.id.button2).setVisibility(View.INVISIBLE);
findViewById(R.id.button1).setVisibility(View.INVISIBLE);
findViewById(R.id.button3).setVisibility(View.INVISIBLE);
findViewById(R.id.button4).setVisibility(View.INVISIBLE);
findViewById(R.id.imageView2).setVisibility(View.INVISIBLE);
}
Here is the second which is to move a character around the screen
public boolean onTouch1(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.button1:
case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
findViewById(R.id.imageView2).setBackgroundDrawable(getResources().getDrawable(R.drawable.realguy1));
mParams.leftMargin -= 20;
i1.setLayoutParams(mParams);
break;
}
}
As I run this code, the one with the OnTouch listener works, but the other does not.
I have done a lot of research, and everything I have tried simply does not work.
Upvotes: 0
Views: 189
Reputation: 192013
I have two View.OnTouchListeners
Actually, there's only one, and here's why...
onTouch1
means nothing to the SDK. All you did was define a method. Your compiler might even be telling you that the method is unused.
Both these are going to onTouch
because that's how this
here is implied.
left.setOnTouchListener(this);
right.setOnTouchListener(this);
Therefore, this
is the only View.OnTouchListener
and it's only method is onTouch
.
If you annotated your methods correctly you'd see that. For example,
@Override // This is okay
public boolean onTouch(View view, MotionEvent event){
}
@Override // Not okay, nothing to override
public boolean onTouch1(View view, MotionEvent event){
}
Now, you could do something like this instead...
Use the one and only onTouch
method and add a switch (view.getId())
with a case
on each button.
@Override
public boolean onTouch(View view, MotionEvent event){
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (view.getId()) {
case R.id.button1:
// left
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
}
return true;
case R.id.button2:
// right
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
return true;
}
return false;
}
Of course, it is your decision to refactor that into many separate methods as you see fit.
For example, if you want to keep onTouch1
, you can delegate onTouch
to it directly.
@Override
public boolean onTouch(View view, MotionEvent event){
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (view.getId()) {
case R.id.button1:
// left
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
}
return true;
case R.id.button2:
// right
return onTouch1(view, event); // delegate to other method
}
return false;
}
Also, case MotionEvent.ACTION_DOWN:
should never be triggered on a switch using the View ID, so you should remove it
switch (v.getId()) {
case R.id.button1:
case MotionEvent.ACTION_DOWN: // Shouldn't work
Upvotes: 3