I was wondering how one uses OnTouchListener. (I'm using Android Studio.)
This is my code and when I press the "Vibrate" button, the button image doesn't change to it's pressed state:
vibrateButton = (Button) findViewById(R.id.button_vibrate);
vibrationInstance = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
vibrateButtonPressed = false;
if (!(vibrationInstance.hasVibrator())) {
vibrateButton.setEnabled(false);
}
vibrateButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
vibrationInstance.cancel();
}
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
vibrationInstance.vibrate(vibrationPattern, 0);
}
return false;
}
});
Is this how one uses OnTouchListener and what is the need for return false;? Thanks!
I guess your application is crashing because you forgot to add the permission to the manifest file. Make sure this is in it: <uses-permission android:name="android.permission.VIBRATE" />
And, by the way, if you want to receive all the touch events occuring to the View, you should return true in the OnTouchListener.
Related
I am trying create an OnTouchListener to my editText ("search").
I took the code from post : Click listener for drawableleft in EditText in Android?
(Along with other posts) I am getting an error saying
Custom view EditText has set OnTouchListener called on it but does not
override PerformClick
Not quite sure where to go from here, as there is not much written about this that I can find. Thanks from your help!
search.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
if(event.getX() <= (search.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width()))
{
// your action here
return true;
}
return false;
}
});
Thanks
You can suppress the error by using #SuppressLint("ClickableViewAccessibility")
Check out this question for more information on your error.
I've seen plenty of answers on how to use onTouchListener such as this code:
imageButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Do what you want
return true;
}
return false;
}
});
but it's never clear to me where I'm supposed to put this code in my project ? So far I've only been editing my MainActivity.java file, will I need to create another java file to house this code?
Nope. Widget properties can just go into your MainActivity code. It should look something like this:
ImageButton imageButton = (ImageButton) findViewById(R.id.button); //instantiate the button
imageButton.setOnTouchListener(new OnTouchListener() {...}) //set the listener
The other (less common) way to do this is through XML. See this SO post for more on that.
I am having this problem when I try to change the source/resource of a image(a button) when is pressed and released.
Btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
/*if(event.getAction() == MotionEvent.ACTION_DOWN) {
ImageBtn.setImageResource(R.drawable.btn1);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageBtn.setImageResource(R.drawable.btn0);
}*/
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
}
return false;
}
});
This is the code, I tried what is in the comment too, but no result. I do have to mention that the button changes its resource just when touching it, but after releasing it, it doesn't change.
The button listener is on a relative layout(also defined as a relative layout in the xml code), in java however is defined as a button.
The image of the button doesn't change from when you touch it to when you release it because you are setting the same background resource.
Both cases contain:
ImageBtn.setBackgroundResource(R.drawable.btn1);
Set one of them to a different image.
I'm running an emulator for google glass as seen here, works pretty flawlessly by showing settings, main display and even my activity (which I pretend to be an interactive static card).
http://mobilevangelist.com/2014/01/02/gdk-and-the-android-emulator/
I've seen that the motion gestures are captured using onKeyUp or onKeyDown events but neither are working and I don't understand why.
Here is my code.
public class LiveCardMenuActivity extends Activity {
private TextView textView;
#Override //isn't catching a thing, even with onKeyDown (mouse taps or slides in the emulator)
public boolean onKeyUp(int keycode, KeyEvent event){
Log.d("tag","keyUp");
if(keycode == KeyEvent.KEYCODE_DPAD_CENTER){
Log.d("tag","keypadcenter");
textView.setText("tap");
}else if(keycode == KeyEvent.KEYCODE_BACK){
Log.d("tag","swipedown");
textView.setText("down");
}
return true;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setContentView(R.layout.live_card);
//does successfully, I can see the layout in the emulator
//and I can swipe it to the left (returning to the main display successfully)
textView = (TextView) findViewById(R.id.textView);
Log.d("tag","attached to window");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.live_card, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stop:
// Stop the service which will unpublish the live card.
stopService(new Intent(this, LiveCardService.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
// Nothing else to do, finish the Activity.
finish();
}
}
Can someone help me on this one? Tyvm!
Try calling setFocusable(true) on your content view.
Though I don't know much about how this emulator works, this is how you might get this working for standard Android.
You're using onKeyUp() when you need to be using onKeyDown(). Here's an example I'm using in my Glass project (but I have a physical Google Glass, not using an emulator):
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch( keyCode ) {
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() TAP/KEYCODE_DPAD_CENTER");
// DO SOMETHING
return true;
case KeyEvent.KEYCODE_BACK:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() SWIPE/KEYCODE_BACK");
// DO SOMETHING
return true;
default:
Log.wtf("GESTURE_EVENT", "PostVideoActivity.onKeyDown() DEFAULT -- SHOULDNT BE HERE!");
return false;
}
}
edit Maybe this is an issue with the emulator? I doubt it though but you can test this theory by creating some dummy Android project for a phone/phone emulator that is known to work. If this snippet doesn't even work for the other Android emulation then maybe its something else?
I'm trying to create a view programmatically, and change its background according to its click state.
I know I can do this with xml drawables, but I want to learn to program it too.
But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)
My code is this :
Edit:
I solved the problem. Here is the working code :
mainContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(0xaaffffff);
return false;
}
if(action==MotionEvent.ACTION_MOVE)
{
}
if(action==MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(0x00ffffff);
}
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
return false;
}
});
As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.
What am I doing wrong ?
Thanks for any help.
What about returning true in your ACTION_UP aswell?
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}