Apps Modify alignement after a click

aurelien

Lurker
Hi everyone,

I'm programming an app but i have a problem...
I want that when i click on the screen of my phone, my image goes where i clicked.
(I just want to change the "x" alignement of the image).

This is the code of the Java Activity :


public class gameActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);

final IShip ship = new StandardShip();
final ImageView iv_ship = (ImageView)findViewById(R.id.iv_ship);
final RelativeLayout rl_game = (RelativeLayout)findViewById(R.id.rl_game);
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();

display.getSize(size);

iv_ship.setImageResource(R.mipmap.img_standardship);

iv_ship.setOnTouchListener(new View.OnTouchListener() {
@override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
ship.setX(0 - size.x / 2 + event.getX());
iv_ship.setX(ship.getX());
}
return true;
}
});
}
}

This is the code of the layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="aurelien.pizza.gameActivity"
android:id="@+id/rl_game">


<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iv_ship" />
</TableRow>
</RelativeLayout>
 
D

Deleted User

Guest
Sorry, we seem to be going round in circles here. That link I posted tells you how to set the absolute position of a view, using setLayoutParams().
Have you tried that?
 
Top