• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Modify alignement after a click

aurelien

Lurker
Jan 29, 2016
5
0
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>
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones