Android java Screen as an button - java

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

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!

How to catch the OnTouchEvent just in a particular situation?

I am about to write a little quiz app. I have 4 Buttons which represent the possibilities of answers. When one button is depressed It should blink red or green to indicate right or wrong. So far no problem.
But the next step should be to slide out the old question and to slide in the new one. This should initiate by a touch of the user anywhere on the screen.
How can I achieve this? Which methods I have to use?
Code:
#Override
public void onClick(View v) {
Button button = (Button)findViewById(v.getId());
if(button.getText().equals(quizWorld.getCurrentQuestion().getRightAnswer())){
//right
}else{
//wrong
}
}
#Override
public boolean onTouchEvent(MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}
Just for Explanation: In the onClick the Programm compares the text on the button with the saved answer and initiate the blinking (not coded yet). And on the onTouchEvent it changes the question.
And now: onTouchEvent should be only "activated" when the Programm has just finished the checking process. After that the Programm shouldn't react on OnEventTouch.
If you don't need to handle the touch event, just return false onTouchEvent and let the touch pass through.
#Override
public boolean onTouchEvent(MotionEvent event){
if (I don't need this touch)
{
return false;
}
switch(event.getAction()){
case MotionEvent.ACTION_UP:
//Game Over?
if(quizWorld.swapQuestion()==false){
viewFlipper.setDisplayedChild(1);
}
break;
}
return true;
}

Android- Swiping Across Multiple Buttons

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;
}

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.

How to handle touch or press down events in Android?

Alright so i have 4 image views..
How can i control what happens when they are pressed down and up
So say like:
one of the images is press down a textview gets changed to 1 but when they let go of that one the text will change back to 0?
You need to use an OnTouchListener and have it listen for TouchEvents on your Views.
For Example:
MyTouchListener l = new MyTouchListener();
view.setOnTouchListener(l);
Here is an example of what MyTouchListener could look like:
class MyOnTouchListener implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
}

Categories