I'm trying to test out setting the position of an ImageView
when the user taps on the GameView
. Whenever I tap on the emulator, the app crashes without any error messages. Same result for when I try setX
/setY
and setTop
/setLeft
I can't figure out what I'm missing here.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.test.GameView
android:id="@+id/groupLayout"
android:layout_width="300dp"
android:layout_height="300dp">
<ImageView
android:id="@+id/flagImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/flag"
/>
</com.example.test.GameView>
</LinearLayout>
public class GameView extends LinearLayout {
private ImageView flagImage;
public GameView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLACK);
flagImage = findViewById(R.id.flagImage);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
LayoutParams par = (LayoutParams)flagImage.getLayoutParams();
par.leftMargin += 30;
flagImage.setLayoutParams(par);
return super.onTouchEvent(event);
}
}