0

I am developing a piano app, currently I am working on something to make my buttons work when I slide them over with my finger.

Here is more comprehensive explanation:

Here is what happens when i move my finger outside of this button, without releasing my finger

Here is what I would like to have when I slide my finger to another button - button which was touched first, stops working, becomes green and button which i enter with my finger works, so it turns red

My code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/imageButtonSelector"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/new_button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

new_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/redcolor"
        android:state_pressed="true" />
    <item android:drawable="@drawable/orange"
        android:state_focused="true" />
    <item android:drawable="@drawable/greencolor" />

</selector>

MainActivity.java

package com.example.android.focus;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button imageButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    imageButton = (Button) findViewById(R.id.imageButtonSelector);

    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(MainActivity.this,
                    "ImageButton (selector) is clicked!",
                    Toast.LENGTH_SHORT).show();

        }

    });

}

}

What do i need to do to make my app work as it is supposed to? Any help will be appreciated!

Old York
  • 73
  • 1
  • 9
  • http://stackoverflow.com/questions/12980156/detect-touch-event-on-a-view-when-dragged-over-from-other-view – Roee N Nov 02 '16 at 10:57
  • No, no. Not an onClickListener, you need an onTouchListener, because you need to find out when the finger is down, up (after having been down), and when your finger is moving. Also, since it's a piano and people rarely play only one key at a time, you need to find out how Android handles multi-touch events. – Stephan Branczyk Nov 02 '16 at 10:57

1 Answers1

0

Since you will be sliding over multiple buttons i dont think implementing onclick listeners on each button would accomplish what you want

but i could suggest you two approaches try creating a custom view ( maybe using a bunch of rectangles create your own piano layout with android view groups and handle your touch gestures , action move , down up .. )

or maybe if you still need buttons i guess youll have to android multitouch events and create create your custom button extending the appcompatbutton class, so that it detects swiping over a button

hope this helps you out

Sajidh Zahir
  • 526
  • 4
  • 13
  • Could you explain the last part of your answer? I would be thankful – Old York Nov 03 '16 at 12:24
  • You could do something like this public class CustomButton extends AppCompatButton and override its touch events so that you could change how the touch works in a regular button but i prefer the first suggestion that would much easier way since its a view group and youll have to handle the touch from the scratch you have a huge of flexibility to add your gestures – Sajidh Zahir Nov 03 '16 at 17:58