1. Are you ready for the Galaxy S20? Here is everything we know so far!

Modify alignement after a click

Discussion in 'Android Development' started by aurelien, Jan 29, 2016.

  1. aurelien

    aurelien Lurker
    Thread Starter

    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>
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

  3. aurelien

    aurelien Lurker
    Thread Starter

    But it will not help me about my onTouchListener problem?
     
  4. Deleted User

    Deleted User Guest

    What's your onTouchListener problem?
     
  5. aurelien

    aurelien Lurker
    Thread Starter

    When i click to set the ship where i clicked. He isn't set where i clicked but somewhere else i don't know why
     
  6. Deleted User

    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?
     
  7. aurelien

    aurelien Lurker
    Thread Starter

    Thanks for your help
     
Loading...

Share This Page

Loading...