detect middle (rotary) button click on android waer - android

I am developing an app for Android Wear and are using the hardware buttons.
I can manage to catch the buttons with the onKeyDown override:
public boolean onKeyDown(int keyCode, KeyEvent event){
Log.i("CLICK", Integer.toString(keyCode));
if (event.getRepeatCount() == 0) {
if (keyCode == KeyEvent.KEYCODE_STEM_1) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
// Do stuff
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I would also like to catch a click on the middle (rotary) button,
but when I click this button, the watch is going back to the default 'home'(watchface)-screen,
and no event is being logged.
I can manage to catch the rotary scrolling, but it's the click I'd like to use.
#Override
public boolean onGenericMotionEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
// // Note that we negate the delta value here in order to get the right scroll direction.
// float delta = -RotaryEncoder.getRotaryAxisValue(ev)
// * RotaryEncoder.getScaledScrollFactor(getContext());
// scrollBy(0, Math.round(delta));
return true;
}
return super.onGenericMotionEvent(ev);
}
Is this possible, or is it impossible to override the functionallity of the middle watch button?

AFAIU It should be getting KEYCODE_HOME as key event on pressing RSB in onKeyDown().
If it is the case then it is controlled by Android framework only and apps can't do anything with it.
Here is the official description
Key code constant: Home key. This key is handled by the framework and
is never delivered to applications.
https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME

Related

Which key action does the nexus phone fire when back button pressed?

I'm wondering which KeyEvent action is called when a user presses the little upside-down triangle in nexus phone when soft keyboard is open.
In normal mode Nexus looks like this and the normal code works fine:
Nexus without keyboard
But when keyboard pops up it looks like this and the code won't work:
Nexus with keyboard
For android API up to 5:
#Override
public void onBackPressed() {
// your code.
}
For android before API 5 you must use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Please refer to How to handle back button in activity
EDIT:
This methods works only if the keyboard is hidden..
according with this answer: Detect back key press - When keyboard is open
The best action to be implement is dispatchKeyEventPreIme.
An example is:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
Where mActivity is your activity class (this).

How can i disable all bottom navigation button for lockscreen..?

I'm creating lock screen application. and all activity work perfectly, But when screen lock activity comes up on screen on, all Home and task navigation button works. Only back button is not working because I have return nothing in onBackPressed() method.
Now I don't know how to disable this navigation buttons to work in my lockscreen application.
I have tried this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
//this is where I can do my stuff
return false;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
//Do Code Here
// If want to block just return false
return false;
}
But Only back button stops working all other remain as it is.

Detect ongoing touch event

I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}

OnTouch with multitouch in android

I am havig troubles counting how much touches are being made in each "half" of the screen.
When I touch in the upper side it counts correctly, and the same with the lower side. BUT, when I touch the upper side, dont release it, and touch the lower side, then it counts the wrong side, what is wrong in my implementation?
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
if(event.getY()<400)
{
}
else
{
}
}
if (action == MotionEvent.ACTION_UP) {
if(event.getY()<400)
{
zenbat=zenbat+1;
tv.setText(String.valueOf(zenbat));
}
if(event.getY()>400)
{
zenbat1=zenbat1+1;
tv1.setText(String.valueOf(zenbat1));
}
}
if (action == MotionEvent.ACTION_POINTER_DOWN) {
if(event.getY()<400)
{
}
else
{
}
}
if (action == MotionEvent.ACTION_POINTER_UP) {
if(event.getY()<400)
{
zenbat=zenbat+1;
tv.setText(String.valueOf(zenbat));
}
if(event.getY()>400)
{
zenbat1=zenbat1+1;
tv1.setText(String.valueOf(zenbat1));
}
}
return true;
}
});
If you want to correctly handle multitouch events you need to use the pointer index to correctly identify the finger generating the event.
I've answered a similar question in Android multi-touch interference where I posted a code example how to do it.
To correctly identify the finger, you should refer to the fingerId in the code posted.
Regards.

How to redefine the "volume" button on android

I'd like to redefine the "volume" button on an android phone. For example,When I press the increase or decrease, the volume will not be change, but only to print a word.
Just override the OnKeyDown method like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
//Do whatever you want to do on Volume Up
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
//Do whatever you want to do on Volume Down
return true
}
return false;
}
PROTIP: If you want this behaviour on all your activities and not only one, just do this in MainActivity.java (or whatever you want to call it) and make every other Activity extend MainActivity.
PROTIP 2: Don't do this unless it is absolutely necessary and you notify the user it works like that. Android users usually complain about it not having a common behaviour between apps.
You have to override dispatchKeyEvent() method like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
Toast.makeText(this,"Your First Word",Toast.LENGTH_SHORT).show();
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
Toast.makeText(this,"Your Second Word",Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}

Categories

Resources