How to check for hold click Android studio/java - java

I want to make in my app when I hold click it does something and when I release the button it does something else. A variable will change to true when I hold and false when I release. How do I do that
(Android Studio)
Thanks

You have to use TouchListener and MotionEvent class. The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class.
Something like this
findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// button pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// button relased.
}
return false;
}
});

You can use the onTouchListener for your use case:
boolean isClicking = false;
myView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// the view is pressed
isClicking = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the view is released.
isClicking = true;
}
return false;
}
});

Related

Make onTouchListener to update?

I haven't seen an answer for what I am trying to do. I want to update code for as long as I am pressing down on a button and when I let go, the code stops updating. Any advice helps please. Here's my code:
forward.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Code updates every frame
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP){
// Code stops updating
return true;
}
return true;
}
});
Here is the example:
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
text1.setText("Text1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
text1.setText("Text2");
}
return true;
}
});

Check keyboard KeyEvent.KEYCODE_ENTER click from editText

I have an editText and I want to take the user to another Activity when the enter action of the keyboard is pressed. I tried adding this to the editText's xml:
android:imeOptions="actionSend"
And this is my code:
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
Intent goDetails = new Intent(RecipesList.this, RecipeDetails.class);
goDetails.putExtra("Keyword",searchField.getText());
startActivity(goDetails);
return true;
}
return false;
}
});
The enter button has its name changed but the action isn't fired. Can you help me understand where's the problem with the code?
Try this
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.KEYCODE_ENTER) {
// do stuff here
}
return false;
}
});
Edited
KeyEvents can also be listened this way.
searchField.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
// do stuff here
return false;
}
});

JAVA / Android - Ontouchstart instead of onClick

We have an onClick function on our relativeLayout but the method triggers when we release our finger from the screen (onTouchEnd). We want our function to trigger on the moment my finger touches the screen.
android:clickable="true"
android:onClick="click"
public void click(View v){
releaseItem();
}
Use View.setOnToucListener(listener)
yourRelativeLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Your stuff here
return true;
}
return false;
}
});

Animation Starts when touch and Stops when UnTouched

So my question is how can I make an animation Starts when touch and Once it's not Touched it plays another animation ..
(sorry for my English..)
main.java :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView is = (ImageView) findViewById(R.id.img1);
is.setBackgroundResource(R.animator.animup);
final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
final ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource(R.animator.anim);
final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
is.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true, true);
anim.start();
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.setVisible(true, true);
animup.start();
//perform your animation when button is released
}
return false;
}
});
};
;
}
+Another Problem......
EDITED
I reckon this link will help you.
Edit
For implementing touch and release you have to use onTouchListener rather than onClickListener.
Below is the code-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//perform your animation when button is touched and held
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//perform your animation when button is released
}
}
};
Edit 2
In the else if block you are not setting the visibility of anim to false. I feel that is the problem. Rewrite your code as this-
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
anim.setVisible(true,true);
anim.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
anim.stop(); //perform your animation when button is released
animup.setVisible(true,true);
animup.start();
}
}
};

How to set up an onTouchListener that changes an ImageView image resource?

I want to setup up an onTouchListener that changes an ImageViews image resource on ACTION_UP and ACTION_DOWN. Here is my code:
// Setup a basic upDownListener that registers UP and DOWN actions and changes the
// view image resource accordingly
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};
This code does not compile because View does not have the method setImageResource, only ImageView. But how do I tell the listener that an ImageView is being used?
Cast the incoming View to an ImageView.
Sample code:
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv.setImageResource(R.drawable.button_white_pressure);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
iv.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
View.OnTouchListener upDownListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView v = (ImageView) v;
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Button was pressed, change button background
v.setImageResource(R.drawable.button_white_pressure);
return true;
} else if(event.getAction() == MotionEvent.ACTION_UP) {
// Button was released, reset button background
v.setImageResource(R.drawable.button_white);
return true;
}
return false;
}
};

Categories

Resources