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;
}
}
Related
I have this code for my MotionEvents in Android:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getActionMasked();
switch (action){
case MotionEvent.ACTION_UP:
mGameview.moveUp(30);
break;
case MotionEvent.ACTION_DOWN:
mGameview.moveDown(30);
break;
}
return true;
}
However, instead of these things only triggering when I drag my finger up/down, they both happen simultaneously no matter what I do in the app, for example when I tap, drag left and right.
Try to get action by:
int action = event.getAction() & MotionEvent.ACTION_MASK;
I have the following code, all I want is to detect touch on a edit text and scroll the scroll view:
View.OnTouchListener buttonFocus = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("TOuch");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("TOuch down");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case MotionEvent.ACTION_UP:
System.out.println("TOuch up");
((ScrollView) findViewById(R.id.m_scroll)).fullScroll(View.FOCUS_DOWN);
v.performClick();
break;
default:
break;
}
return false;
}
};
While as the keypad pops up at the start of activity the scroll does not occur, it only scroll when I touch the view for the second time. Am I doing something wrong here? Will implementing a thread help?
Returning true in that last line of yours might solve your problem.
Returning false means that the onTouch has been used completely and won't entertain any further touch events
I'm very new to android development and I just started to study. So sorry for this simple question.
When I long press button it will pass string successfully but when I release button click it does not pass second string... please let me know where is the problem.
Hii you can use what #pervez other wise you can use ToggleButton for example you can use like this.
ToggleButton myButton=(ToggleButton)findViewById(R.id.myToggle);
myButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
if(myButton.isChecked()){
doYourMethod1();
}else{
doYourMethod2();
}
});
Long press only fires once use onTouchListener if you want two events to be fired one at ACTION_DOWN and other at ACTION_UP.
EDIT: Use this only if you want two events to be fired one when the user touch the view and other when user lifts the finger from the view.The code can be like this...
textView.setOnTouchListener(new onTouchListener)
{
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("DOWN","DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d("MOVE","MOVE");
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
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 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