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
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!