What do I need to do to make my button (or sth else, please tell me what should i use):
Start playing music on
ACTION_HOVER_ENTER
Keep playing (without reset, just do nothing) on
ACTION_HOVER_MOVE
Stop playing and reset music on
ACTION_HOVER_EXIT
Work when I start my move from anywhere on the screen, then without release
hover enter
my buttonHave no problem with starting the same activity but triggered by another button located in another part of the screen - without finger's release
I should use onHoverListener()
, am I right?
Please write code sample if you can :)
Here is what do I have so far:
package com.example.android.appname;
import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class DisplayActivity extends AppCompatActivity {
ImageView b1;
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (ImageView) findViewById(R.id.button2);
b1.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
if (mPlayer == null)
mPlayer.start();
break;
case MotionEvent.ACTION_HOVER_MOVE:
break;
case MotionEvent.ACTION_HOVER_EXIT:
mPlayer.release();
mPlayer = null;
break;
}
return false;
}
});
}