Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Aug 15, 2020 · 0 comments

Comments

@Zererat
Copy link

Zererat commented Aug 15, 2020

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);
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant