setVolumeControlStream() not working - android

I'm calling setVolumeControlStream(AudioManager.STREAM_MUSIC); in the onCreate() methods of every of my three Activities in the application, but none of them is working. In fact, when I'm pressing the volume control buttons - nothing happens at all. Here's a quote from the documentation:
It is not guaranteed that the hardware volume controls will always change this stream's volume (for example, if a call is in progress, its stream's volume may be changed instead).
All that my application is doing is playing music in the background and playing some sounds. Why should it not work? Thanks in advance.

When you have overriden onKeyDown() etc make sure that you do call super.onKeyDown() as well for keys that you do not handle or at least for the volume up/down keys, e.g. in your view subclass:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// do your stuff here...
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
return super.onKeyUp(keyCode, event);
}
return true;
}

Related

LibGdx: Move camera with Keyboard visible, capture Android BACK button

I have Actors that I need to move when the keyboard becomes visible (When I press a TextField), or they are stuck behind it. I do this by moving the camera up:
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/3, 0);
stage.getViewport().getCamera().update();
This works fine. It also works fine to move it back when I touch something outside the TextField and call stage.unfocusAll();
My problem is, when I'm in a TextField and press Androids Back button, it hides the keyboard, but does not call the code I have inside the Inputprocessor (THIS CODE CAPTURES BACK-BUTTON ALWAYS EXCEPT WHEN INSIDE A TEXTFIELD AND KEYBOARD IS VISIBLE):
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) )
{
moveBack();
}
return false;
}
};
I looked around and read that it is not possible to catch the back-button when inside a TextField. Which leads me to my questions:
This must be a common scenario (moving UI to work with keyboard), how do other people do it?
If other people do like me (move camera), how do you handle the Android back-button?
EDIT: This answer captures the back-key while inside a TextField, however it has to be done in the Android Launcher, so I can't reach the elements I need to reach. I also overwrites all other calls to the BACK-button from inside the LibGdx project.
Ok so with the help of This I got a solution working, but it's not very pretty.
Basically I rewrite my GDX Launcher class, and use the following code as the layout:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
game.setMoveBack(true); }
}
return super.dispatchKeyEventPreIme(event);
}
};
Then, in the render-method of my MainScreen (Screen that all other Screens inherit from), inside the render-method I call:
if(game.isMoveBack()){
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/2, 0);
stage.getViewport().getCamera().update();
game.setMoveBack(false);
}
If anyone has an easier way to do this please post and Ill accept that as an answer, but for now, this works if anyone finds themselves where I am :)

Android onKeyDown() not execute on pressing delete button

I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

Handle event after DismissOverlayView (Android Wear)

Is it possible to handle some finishing stuffs when user press the X button in DismissOverlayView? Like save some files, etc.
Alternatively, I would like to pause the app before the X button and resume if X is not selected.
I imagine it would call your Activity's onPause(), onStop() and onDestroy() methods, which is normally where you handle the things you mentioned such as saving data. You should take a look at Android's Activity's lifecycle, which also applies to Android Wear. In particular, here's what it says about onPause(): "Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc." Sounds like what you want.
This will get the event of selecting around the edge of the DismissOverlayView:
DismissOverlayView dismissOverlayView = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
dismissOverlayView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("Cancelled DismissOverlayView");
}
return false;
}
});

setVolumeControlStream() in a multiple activity application

I've got three activities in my application and I have problems with calling the above mentioned method. First of all, do I have to call it inside every activity's onCreate() method? Right now I've tried it this way, and suddenly it works only in one activity of three. In other two the default volume control bar is not shown and the volume is not adjusted. What's the right way of controling volume stream in a multiple activity application? Thanks in advance.
When you have overriden onKeyDown() etc make sure that you do call super.onKeyDown() as well for keys that you do not handle or at least for the volume up/down keys, e.g. in your view subclass:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// do your stuff here...
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
return super.onKeyUp(keyCode, event);
}
return true;
}

can't handle keyboard events android

I am trying to handle the back button event on my app but its not working at all. I have inplemented ActivityGroup in my app according to the post Android: TabActivity Nested Activities
I have added the following code according to many posts in this website
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(this.getClass().getName(), "back button pressed: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d(this.getClass().getName(), "back button pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
but for some reason i still dont know i am not getting the lines to be logged, it goes back to the home screen. I know that the onBackPressed will not work for me because I need to have this app implemented using api level 4 and it is not available at this level.
My ActivityGroup has only two activities, one list view and a details view. I have put this code on all the three classes to try something different, but still cant get it working. I see "No keyboard for id 0" in the logs, but i dont think it means something that can be related to the problem.
I do appreciate any answer to this.
Many thanks
T
Add a log statement right above the return line and see what KeyEvent is happening.
like this:
Log.w(keyCode, "This is the key code that is returned");
return super.onKeyDown(keyCode, event);
Now look at the returned value and verify/compare it to KeyEvent.KEYCODE_BACK and this may point you in the direction of your problem.

Categories

Resources