Skip to content

Problem with pressed Button in AndroidStudio. I want to hold down on an imageview-button and drag it to an specific location, which should open afterwards an activity #83

Open
@Zererat

Description

@Zererat

Honestly I dont know how to proceed. I looked up some feeds on this website and on stackoverflow got a bit confused and dont know how to get it done. The imageview button should be dragged (while holding it down) at a specific location of an other imageview and as long as my pressed thumb relocates to the other image, a activity should be called. In an earlier version, the activity was functional but not while moving the button while it was held down by my thumb. Hopefully someone knows an answer for this (for me a very challenging question.)


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;

import android.annotation.SuppressLint;
import android.app.RemoteInput;
import android.content.Intent;
import android.graphics.Rect;
import android.media.effect.Effect;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ImageView imageViewPlayButton, imageView2, imageView3, imageView4;
    private Rect imageRect;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detectorCompat = new GestureDetectorCompat(this, new GestureListener());
        imageViewPlayButton = findViewById(R.id.PlayButton_Image_White_Base);
        imageView2 = findViewById(R.id.register_Icon_Base);
        imageView3 = findViewById(R.id.navigation_Background_Image_Register);
        imageView4 = findViewById(R.id.Login_Icon_Base);

        imageViewPlayButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    imageView2.setVisibility(View.VISIBLE);
                    imageView3.setVisibility(View.VISIBLE);
                    imageView4.setVisibility(View.VISIBLE);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    imageView2.setVisibility(View.INVISIBLE);
                    imageView3.setVisibility(View.INVISIBLE);
                    imageView4.setVisibility(View.INVISIBLE);
                }
                return false;
            }
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (imageRect == null) {
            imageRect = new Rect();
            imageView2.getGlobalVisibleRect(imageRect);
        }
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (imageRect.contains(x, y)) {
            openActivity();
        }

        return true;
    }

    private void openActivity() {
        Intent intent = new Intent(this, Register.class);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions