0

I am stuck on a problem for a while now. I have 2 buttons on my app (button1 and button2) and I want to have it so when my finger moves over the first button it will say some thing like "In button 1" and then when I move my finger into button 2 it will say "In button 2" in the logcat.

I have it at the moment where I am able to click these buttons to get the text to appear but I don't want that. I want to be able to move my finger around without taking it off the screen to display the text. I would appreciate any help at all, thank you!

My code:

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

        buttonOne = (Button) findViewById(R.id.buttonOne);
        buttonTwo = (Button) findViewById(R.id.buttonTwo);



   buttonOne.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Log.d(TAG, " In button 1 ");
       }
   });

   buttonTwo.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Log.d(TAG, " In button 2 ");
       }
   });

Using the link I changed my code, but it didn't seem to work:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    rect1 = new Rect(buttonOne.getLeft(), buttonOne.getTop(),
            buttonOne.getRight(), buttonOne.getBottom());

    rect2= new Rect(buttonTwo.getLeft(), buttonTwo.getTop(),
            buttonTwo.getRight(), buttonTwo.getBottom());
}


public boolean onTouch (View v, MotionEvent event){
    if (event.getActionMasked() == (MotionEvent.ACTION_DOWN | MotionEvent.ACTION_MOVE)) {
        if (rect1.contains((int) event.getX(), (int) event.getY())) {
            Log.d(TAG, " in button1: ");
        }
        if (rect2.contains((int) event.getX(), (int) event.getY())) {
            Log.d(TAG, " in button2: ");
        }
    }
    return true;
}
Eoin
  • 252
  • 2
  • 15
  • Please share what you have done so far. – Ali Kanat Mar 11 '19 at 11:35
  • Added my code there. – Eoin Mar 11 '19 at 12:00
  • change to this: YourLayout b=(YourLayout) findViewById(R.id.yourlayoutID); b.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch (View v, MotionEvent event) { if (event.getActionMasked() == (MotionEvent.ACTION_DOWN | MotionEvent.ACTION_MOVE)) { if (rect1.contains((int) event.getX(), (int) event.getY())) { System.out.println(" in button1: "); } } return true; } }); – ArunKumar M N Mar 11 '19 at 12:59
  • Hi, I'm getting errors on the first line of that code, on the YourLayout part? It's not giving me much feedback it just says create class as a solution. – Eoin Mar 11 '19 at 14:06

0 Answers0