Im using seek bar to control volume of the device. Im able to change the volume of the device using thumb of the seek bar just by dragging it on touch pad.
But when user presses volume(side) keys i need to set seek bar thumb position accordingly.
Hw i can do this please let me know
Thanks
I got solution, by overriding onkeydown event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
int index = seekbar.getProgress();
seekbar.setProgress(index + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
int index = seekbar.getProgress();
seekbar.setProgress(index - 1);
return true;
}
return super.onKeyDown(keyCode, event);
}
Edited for further reference
Related
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
I'm trying to figure out how long the volume up and volume down buttons are pressed, but so far, all I've found is this answer here: Android - Getting volume button long clicks
I can't find anything less abstract than this so I have no idea how to go about implementing it; could someone point me to an example? Any help would be greatly appreciated.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
int action = event.getAction();
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
if (action == KeyEvent.ACTION_DOWN) {
volUpPressed = System.currentTimeMillis();
}
else if (action == KeyEvent.ACTION_UP) {
totalTimePressed = System.currentTimeMillis() - volUpPressed;
}
}
return true;
}
I am making a keyboard app. I want to receive the touch coordinates when the user starts swiping inside the keyboard and goes outside the keyboard view.
I'm overriding the onTouchEvent(MotionEvent me) as shown below:
#Override
public boolean onTouchEvent(MotionEvent me){
int action = me.getAction();
if(action == MotionEvent.ACTION_DOWN){
}
else if(action == MotionEvent.ACTION_MOVE){
Log.d("testing", "touchY = " + me.getY());
return true;
}
else if(action == MotionEvent.ACTION_UP){
}
else if(action == MotionEvent.ACTION_OUTSIDE){
}
return super.onTouchEvent(me);
}
But once the user goes out of the KeyboardView, me.getY() keeps returning zero even when the user slides his finger vertically. Interestingly me.getX() gives the correct value even when finger moves out of the KeyboardView
My guess is it has something to do with Permissions or Window LayoutParams flags or similar.
I use SeekBar for setting volume. I do the following:
I get current volume by getStreamVolume() and pass it to SeekBar element by setProgress().
I listen messages from the SeekBar element by onStopTrackingTouch() and set volume by setStreamVolume().
It works.
But user can change volume by hardware volume controls. I'd like to catch it and change SeekBar pointer position appropriately. How can I do it?
Try the following
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//set your SeekBar
return true;
}
return super.onKeyDown(keyCode, event);
}
The question/answer that biegleux pointed you to is only for the "media keys" on an attached headset, not for the hardware volume up/down keys on the phone itself.
I am playing a video in my app using VideoView. When I press Volume up/down buttons on the phone, the Media Volume is changed. Instead of that I want to receive notifications (methods OR intents etc.) so that my app can do customized action when Volume up/down is pressed.
Try this code, to hack volume key
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//Do your thing what you want...
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}