Launch application on 2 finger swipe ANDROID - java

I have this code which doesn't return any errors. However it doesn't work like it's supposed to. I'm trying to launch an application when I swipe up with two fingers but nothing is happening. Can anyone see what the problem with my code?
public class HomeActivity extends Activity {
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
}
}
else {
//do something
}
}

Try debugging handleTouch() to see exactly why its not firing. My guess would be because you have your check on MotionEvent.ACTION_DOWN, which in real world is a bit difficult to happen exactly at the same time for two fingers.
Move the check to MotionEvent.ACTION_MOVE (which is what you want anyway cause you want swipe, not tap) and try again.

MotionEvent.ACTION_DOWN means one finger. MotionEvent.ACTION_POINTER_DOWN means secondary touches (eg. two fingers).
So moving your code to case MotionEvent.ACTION_POINTER_DOWN: should solve your issue.
Documentation.

Related

Button long click does not work

I'm working on a soundboard and I want to implement a long click to share the sound.
I am working with a switch Case for each button
public void MainMMP(View view){
switch (view.getId()) {
case R.id.button1:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx1);
MainMMP.start();
break;
case R.id.button2:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx2);
MainMMP.start();
break;
case R.id.button3:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx3);
MainMMP.start();
break;
And now I want to implement the long click. I tried a lot of different code here but it is not working for me.
I do not know where to put the onLongClick statement and how.
Can somebody show me a working method and in case of long click it should just send me a Toast that I know the method works?
You could add the OnLongClickListener where you want, in the onCreate method for example.
Try to use the following code:
Button button = (Button)findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Your code
return false; // True if you want to execute simple click code too
}
});
You can use this
private View.OnLongClickListener listener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
switch (view.getId())
case R.id.button1:
// Do something...
break;
case R.id.button2:
// Do something else...
break;
// If you still want to get normal click callbacks return true,
// if you do not then return false.
return true;
}
}
Somewhere in your code
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnLongClickListener(listener);
button2.setOnLongClickListener(listener);
Or better this
One common recommended way to get onClick/onLongClick/whatever callbacks is to make the Activity implement the callback interfaces.
class YourActivity extend Activity implements View.OnLongClickListener {
#Override
public boolean onCreate(/* ... */) {
// ...
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
}
#Override
public boolean onLongClick(View view) {
// Same code as the one above
}
}

How do I get an image view to continually move across the screen when a button is held down?

I have searched high and low for an answer to this and can't find one anywhere that works.
For my university assignment i need to create an adventure game in android studio. I want it so when I click down and hold an arrow button (so in the case given its the up button), that the ImageView (player) will continually move across the screen until I release the button. Ive tried OnTouchListeners and mouse events using ACTION_UP and ACTION_DOWN and that works but not for what i need as it still only moves one step when clicked.
ImageView IV_player;
Button ButtonUp;
IV_player = (ImageView) findViewById(R.id.IV_player);
ButtonUp = (Button) findViewById(R.id.ButtonUp);
ButtonUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IV_player.setY(IV_player.getY() - 32);
}
});
Treat your touch listener like a state machine. When an ACTION_DOWN event occurs, start doing whatever action you want to do. When an ACTION_UP/ACTION_CANCEL event occurs stop your action. So how do you go about implementing this?
Your state flag can be a simple boolean:
boolean shouldCharacterMove = false;
Define your touch listener for the view.
ButtonUp.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
setShouldCharacterMove(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setShouldCharacterMove(false);
break;
}
return true;
}
});
Before we define setShouldCharacterMove we need to figure out a way to move items. We can do this through a Runnable that runs after X milliseconds.
private final Runnable characterMoveRunnable = new Runnable() {
#Override
public void run() {
float y = IV_player.getTranslationY();
IV_player.setTranslationY(y + 5); // Doesn't have to be 5.
if (shouldCharacterMove) {
IV_player.postDelayed(this, 16); // 60fps
}
}
};
Now we can define setShouldCharacterMove:
void setShouldCharacterMove(boolean shouldMove) {
shouldCharacterMove = shouldMove;
IV_player.removeCallbacks(characterMoveRunnable);
if (shouldMove) {
IV_player.post(characterMoveRunnable);
}
}

Sound not replaying on onTouchListener

So I have a button that, when held, will play a sound. However, it does not seem to "reset" and never plays that sound again when I hold down the button again. I get a
E/MediaPlayer﹕ start called in state 0
E/MediaPlayer﹕ error (-38, 0)
error in log when I try to hold the button down again. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
final MediaPlayer bark = MediaPlayer.create(this, R.raw.bark);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()) bark.stop();
break;
}
return true;
}
});
}
Instead of calling start/stop you can call start/pause with seekTo():
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()){
bark.pause();
bark.seekTo(0);
}
break;
If you want to use start/stop, make sure you call prepare() before calling start() again.
The reason why your code isn't working is because according to the state diagram stop() stops playing and puts the MP in a stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
EDIT : Place your music file in the assets folder and use the file descriptor. This way you can reset the MP and replay:
Using START/STOP:
#Override
protected void onCreate(Bundle savedInstanceState) {
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("bark.mp3"); //replace with right extension
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()){
bark.reset();
bark.setDataSource(fileDescriptor.getFileDescriptor());
bark.prepare();
bark.start();
}
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying())
bark.stop();
break;
}
return true;
}
});
}

Creating switch case onClickListener for TextView

I've just started out in Java programming and am having a bit of trouble implementing an OnClickListener switch case for my clickable TextViews. I've managed to make a switch case for menu items, but i'm obviously not understanding it enough to make a more general case.
Here's the bits of my code that are important to it
public class MyActivity extends Activity implements SensorEventListener {
TextView tv, tv1, tv2, tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
//get textviews
tv = (TextView) findViewById(R.id.xval);
tv1 = (TextView) findViewById(R.id.yval);
tv2 = (TextView) findViewById(R.id.zval);
tv3 = (TextView) findViewById(R.id.scalar);
And then I setup individual on click listeners for each TextView, e.g.
tv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do things
}
}
});
But i'm trying to set it up so i have a a combined OnClickListener, like:
#Override
public boolean onClickListener (View v) {
switch (tv.findViewById()) {
case tv:
//Do things
return true;
case tv1:
//Do things
return true;
case tv2:
//Do things
return true;
case tv3:
//Do things
return true;
}}
I'm aware that code is very wrong, but i can't seem to wrap my head around it. I've already assigned my findViewById so i'm not sure what else to put into the switch!
Thankyou!
I'll provide an alternative answer. First you have to create an OnClickListener, which will receive your OnClick events:
OnClickListener listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.xval:
//code
break;
case R.id.yval:
//code
break;
case R.id.zval:
//code
break;
case R.id.scalar:
//code
break;
default:
break;
}
}
};
Then, you have to associate that listener to every TextView you have:
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);
Once you click one of the TextViews, your OnclickListener onClick() callback will be called and it will check the TextView id you have clicked and run the code accordingly, dependeing on the case.
tv.setOnClickListener(this);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
#Override
public boolean onClick (View v) {
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}}
Create one listener, add it to all TextView. Switch on the view id, which is a simple int
View.OnClickListener listener = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}
}
};
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);

Lower continuously my media volume with a button

I want to lower (or upper) my media volume in my application when an "OnLongClickEvent" is detected.
Here my sources :
buttongauche.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playSound(R.raw.volumevoixdiminue);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
return true;
}
});
Actually, it's work : when I do a longClick on my "buttongauche", the volume is decreased by 1.
Now I would like to know how could I do if I want to lower the sound continuously (for example, decrease sound by 1 every 2 seconde when the button is down).
My button "buttongauche" has already an "onClickEvent", who do other things (change the index of a menu).
Thanks
Declare field boolean touching = false; that says whether or not you are touching the button and use OnTouchListener to change it. When you start touching also start volumeThread that lowers the volume every 1 second, and dies when you stop touching.
buttongauche.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touching = true;
Thread volumeThread = new Thread() {
public void run() {
while (touching) {
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
volumeThread.start();
break;
case MotionEvent.ACTION_UP:
touching = false;
break;
}
return false;
}
});

Categories

Resources