Android- Swiping Across Multiple Buttons - java

I've implemented a grid of buttons using a grid layout. I'm trying to allow for a single swipe to activate multiple buttons. When the user touches any one of the buttons, I call a function with the specific button pressed to take the according action. But, currently, I can only activate one button per touch. Multi-touch works, but not a single swipe. The problem is that while the onTouch function is called continuously the view object that I use to determine the button pressed is only updated on the initial touch. What I need to do is get the id's of all of the buttons that were swiped across.
Thanks.
#Override
public boolean onTouch(View v, MotionEvent event)
{
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
switch (v.getId())
{
case R.id.padZeroZero:
padTouch(0,0);
break;
case R.id.padZeroOne:
padTouch(0,1);
break;
case R.id.padZeroTwo:
padTouch(0,2);
break;
//There's a lot more cases (it's a 9x8 grid), but they all do the same thing.
}
}
return false;
}

Related

How to make a "swipe up" function like in many custom launchers?

I'm doing my own custom launcher for Android at the moment. Everything works so far. But there is one point where I need help.
I would like to do something like a swipe up on the home screen to display all installed apps. Therefore I do not want to start a new activity, cause of the delay.
Maybe it's possible to change the layout file with something like an animation when a gesture is detected? And how would I detect the swipe?
It's really easy to detect motion:
someViewLikeLinearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//put finger on screen
return true;
case MotionEvent.ACTION_UP:
//release the finger
return true;
}
}
});
Just switch event and you can get many actions via MotionEvent
Good luck!

Detecting the location where a button was clicked

I was wondering if there is a way to know exactly where a button was tapped, and take different actions based on where the user tapped it. Something like:
fooBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch(onClickLocation){
case LEFT:
foo();
break;
case RIGHT:
bar();
break;
case MIDDLE:
baz();
break;
}
}
});
Not using an OnClickListener. OnTouchListener gives you a MotionEvent that you can use to determine where the actual touch event occurred.
For example, here I register both an OnClickListener and an OnTouchListener on the same View (called row):
row.setOnClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
row.setOnTouchListener(new View.OnTouchListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean onTouch(View v, MotionEvent event) {
v
.findViewById(R.id.row_content)
.getBackground()
.setHotspot(event.getX(), event.getY());
return(false);
}
});
}
In this case, I don't need to know where the widget was touched for processing the click, but I do need to know where the widget was touched for adjusting the RippleDrawable background, so the ripple appears to emanate from where the user touched. Returning false from onTouch() means I am not consuming the touch event, and so eventually my onClick() method will also be called.
In your case, either:
do the actual work in onTouch(), or
cache the last-seen touch point in onTouch() but do not do the work until onClick()
My gut tells me that the latter should give you better results (e.g., you won't misinterpret a long-click), but I have not tried doing what you are seeking.

2 Simultaneous Android Touches / not triggered

So hi there.
I have a simple Layout with 2 Views in it. Both have an onTouchListener attached.
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
System.out.println("Touching");
return false;
}
});
But when I open the application on my phone and touch the first view and do NOT relase my finger and touch the second view with another finger, the second view wont trigger the touch event. why is this so?
I think in this case both touches are passed to the first view as a multi-touch event. So this is one event but contains (I forgot the details) both touch positions.

Why is my onTouchListener called for neighbour views?

In my application I want to add a simple animation to buttons and other views acting as buttons.
To do this I set a custom onTouchListener to all views and call startAnimation on them.
My onTouch method looks like this:
public boolean onTouch(View v, MotionEvent event) {
// Only show animation when enabled.
if (v.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.startAnimation(shrink);
break;
case MotionEvent.ACTION_UP:
v.startAnimation(grow);
break;
}
}
return v.onTouchEvent(event);
}
This works ok as the views are resized to a smaller size while the user presses the button and returns to the original size when the user releases the finger.
However for some reason other buttons that lie near the touched button also get the UP event so they get a small animation flicker as well.
Why is this, and more importantly, how do I prevent this annoying behaviour?
Edit: Just to be clear. The neighbour views are also registered to the same listener instance, but they are not touched by my finger.
The view to wich you have registered listener then should only get notified then also use following approach.
You can explicitly check what is id of View and then only start animation.
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.desired_view_id) {
// Only show animation when enabled.
if (v.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.startAnimation(shrink);
break;
case MotionEvent.ACTION_UP:
v.startAnimation(grow);
break;
}
}
}
return v.onTouchEvent(event);
}
The problem was using the same Animation instance on several views. Creating seperate instances for each view fixed the problem.

Android java Screen as an button

How can I make the screen as a button?
I want to make a game where you click on the hole screen not just a button in Xml.
I want this code to be in Java not Xml.
Anyone with a tip out there?
Your question is very generic, perhaps this will help you to get started. If you tell us what you're trying to accomplish with this 'Screen Button' perhaps it would be easier for us to point you in the right direction.
You need to implement the following method in your View :
public boolean onTouchEvent(MotionEvent event) {}
Then you'll be able to grab the coordinates were the screen was touched with :
event.getX() and event.getY()
EDIT : You can detect wether the action is a click or release by checking the MotionEvent like so :
#Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
// Do click work here ...
break;
case MotionEvent.ACTION_UP:
// Do release work here ...
break;
}
return super.onTouchEvent(event);
}
Other type of actions can also be checked via other MotionEvent :
MotionEvent.ACTION_CANCEL
MotionEvent.ACTION_MOVE

Categories