0

I am currently trying to make that you have to press both ImageViews simultaneously to move them to a random position on the screen. Since I can't do it with OnClickListener I tried it with the following code example. After hours of searching for an answer I found this code: Link

public class MainClass extends Activity { 
// ...
btn1 = (CustomButton)findViewById(R.id.button_one);
btn2 = (CustomButton)findViewById(R.id.button_one);
btn1.ignoreMotionEvent(true);
btn2.ignoreMotionEvent(true);
// ...

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if(r1 == null)
    {
        //r1 and r2 are Rect that define the absolute region of the button.
        //They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here)
    }
    CustomButton btn = null;
    MotionEvent mevent = null;
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            int x = (int)event.getX();
            int y = (int)event.getY();
            if(r1.contains(x, y))
            {
                btn = btn1;
            }
            else if(r2.contains(x, y))
            {
                btn = btn2;
            }
            if(btn != null)
            {
                mevent = event;
            }
            break;
    }
    if(btn != null)
    {
        btn.ignoreMotionEvent(false);
        btn.onTouchEvent(mevent);
        btn.ignoreMotionEvent(true);
    }
    return super.onTouchEvent(event);
}
}

I googled again for an hour or so and began understanding OnTouchEvent and MotionEvent. My main problem currently is that I don't understand what this means.

//r1 and r2 are Rect that define the absolute region of the button.
        //They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here)

Do I need to somehow define an area of my ImageView by somehow getting its size?

My own code looks like this.

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;

public class GameMode2 extends AppCompatActivity {

    public int startTime = 15000;
    public int interval = 1;
    public int counter;
    boolean neoncircle2Clicked = false;
    boolean neoncircle3Clicked = false;
    boolean restartGame2ButtonClicked = false;
    ImageView neoncircle2;
    ImageView neoncircle3;
    TextView startGame2;
    TextView scoreGM2;
    TextView timerGM2;
    Button restartGame2Button;


    CountDownTimer timer = new CountDownTimer(startTime, interval) {
        @Override
        public void onTick(long millisUntilFinished) {
            final int timeLeft = (int) millisUntilFinished;
            timerGM2.setText(Integer.toString(timeLeft / 1000));
        }

        @Override
        public void onFinish() {
            timerGM2.setText("Time is up");
            restartGame2Button.setVisibility(View.VISIBLE);
            neoncircle2Clicked = true;
            neoncircle3Clicked = true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_mode2);

        neoncircle2 = (ImageView) findViewById(R.id.neoncircle2);

        neoncircle3 = (ImageView) findViewById(R.id.neoncircle3);

        startGame2 = (TextView) findViewById(R.id.scoreGM2);
        scoreGM2 = (TextView) findViewById(R.id.scoreGM2);
        timerGM2 = (TextView) findViewById(R.id.timerGM2);
        restartGame2Button = (Button) findViewById(R.id.restartGame2Button);

        restartGame2Button.setVisibility(View.INVISIBLE);

        restartGame2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!restartGame2ButtonClicked) {
                    startActivity(getIntent());
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        ImageView neocnircle2 = null;
        MotionEvent mEvent = null;
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                int x = (int) event.getX();
                int y = (int) event.getY();

                if (neoncircle2.contains(x, y)) {
                    //do that and this
                }
        }

        return super.onTouchEvent(event);
    }

    public static Point getDisplaySize (@Nullable GameMode2 context) {
        Point point = new Point();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getSize(point);
        return point;
    }


    public void setCirclesRandomPosition (ImageView neoncircle2, ImageView neoncircle3) {
        int randomX2 = new Random().nextInt(getDisplaySize(this).x - neoncircle2.getWidth());
        int randomY2= new Random().nextInt(getDisplaySize(this).y - (3 * neoncircle2.getHeight()));

        int randomX3 = new Random().nextInt(getDisplaySize(this).x - neoncircle3.getWidth());
        int randomY3 = new Random().nextInt(getDisplaySize(this).y - (3 * neoncircle3.getHeight()));

        neoncircle2.setX(randomX2);
        neoncircle2.setY(randomY2);

        neoncircle3.setX(randomX3);
        neoncircle3.setY(randomY3);
    }

    public void backToMenuGM2Clicked(View view) {
        Intent intentBackToMenuGM2 = new Intent(this, MainActivity.class);
        startActivity(intentBackToMenuGM2);
        this.finish();
    }

    public void onBackPressed () {
        Intent obp2 = new Intent(this, MainActivity.class);
        startActivity(obp2);
        this.finish();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/botGameFragGM2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">


    <ImageView
        android:id="@+id/neoncircle2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/restartGame2Button"
        android:layout_alignStart="@+id/restartGame2Button"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/neoncircle2" />

    <ImageView
        android:id="@+id/neoncircle3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignEnd="@+id/restartGame2Button"
        android:layout_alignRight="@+id/restartGame2Button"
        android:layout_alignTop="@+id/neoncircle2"
        app:srcCompat="@drawable/neoncircle3" />

    <TextView
        android:id="@+id/startGame2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#39FF14"
        android:textStyle="bold"
        android:textSize="25dp"
        android:text="Tap the green circle to start!"
        android:gravity="center"/>

    <Button
        android:id="@+id/restartGame2Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="11dp"
        android:background="#000000"
        android:text="Restart Game"
        android:textColor="#39FF14"
        android:textSize="30dp"
        android:textStyle="bold"
        android:gravity="center"/>

    <TextView
        android:id="@+id/scoreGM2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#39FF14"
        android:textSize="200dp"
        android:text=""
        android:alpha=".1"/>

</RelativeLayout>

The error in the code is here

if (neoncircle2.contains(x, y)) {
      //do that and this
}

2 Answers2

0

the r1 and r2 may be a Rect Object(https://developer.android.com/reference/android/graphics/Rect.html), and your neoncircle2 is a ImageView Object, there is no function named contains(int, int) in ImageView.

jiar wang
  • 360
  • 2
  • 12
  • I did what you said and I got it slighty working. I created two Rect objects and passed in the widths and heights from my ImagewViews. My main problem is now how to I connect my Rect objecs with my ImageViews? Threre are just two Rect objects with the size of my ImageViews. I want to put them on top of my ImageViews to when I touch my ImageView on screen I touch both at the same time. How do I do this? – Cagri Ersöz Jan 23 '18 at 17:40
0

You can call onClickListener of two view at the same time. Just you have to do is on the click of first view, you have to call onClick() for the second view programmatically.You can do it using view.performClick()

For example if you have twi view view1 and view2 then you can do like this

view1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      view2.performClick();
                       }
                });
view2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      view1.performClick();
                       }
                });

here on click of any one view, onClickListener for both view will call.

Mayank Panchal
  • 355
  • 1
  • 10