I'm using the following function (in Activity) to detect volume buttons clicks:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP){
if(mAudioManager != null){
int curVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Intent intent = new Intent();
intent.setAction(VerticalSeekBar.ACTION_VOLUME_CHANGED);
intent.putExtra(VerticalSeekBar.ARGUMENT_VOLUME_VALUE,curVolume);
sendBroadcast(intent);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thing is I have popupwindow which represents volume bar as Vertical seek bar in it's view.
when the popupwindow is shown the onKeyDown in my activity is not being called untill I dimissed the popupwindow.
any suggestion how to solve this issue? is there another way to detect volume buttons clicks? thanks very much for the help
Try set focusable false for your PopupWindow class.
mMyPopupWindow.setFocusable(false);
Not sure how you are actually using that Popup but if you use a Dialog instead you have access to the onKeyDown(); for the Dialog.
http://developer.android.com/reference/android/app/Dialog.html#onKeyDown%28int,%20android.view.KeyEvent%29
Related
I have a alertDialog prompt during onCreate of Application Class
(i added <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />)
and I have set setCancelable to false and it did succeessfully prevent user from pressing back button.
This is what I have achieved so far and this is also what I want.
However, when I press home button, the app did "disappear" but somehow the dialog still showing on home screen, and this is what I DON'T WANT.
What I expecting is that the back button should do nothing(this is what I have achieved).
and when user click home button, the whole app Including the Dialog should disappear. But somehow when I click home button the Dialog still appearing on the home screen...
This is because when you are creating the AlertDialog, you probably pass an ApplicationContext instead of just Context.
By following the android framework guideline, you should not make any UI changes in your Application class. Do it in your Activity of Fragment.
So do this in your Activity class:
new AlertDialog.Builder(this)
or this in your Fragment:
new AlertDialog.Builder(getContext)
Then it will disappear if your application goes into the background state.
Try this,
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog1, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
This will do the trick:-
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogs, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Do your stuff here...
return true;
}
return false;
}
});
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 have a Button/Edittext to reset my App-Settings....
If a press the button(no long click), I want to get a Contextmenu with a Button where i can confirm the reset or not!
Which listener do i need for my Button to call a ContextMenu?
Is a also possible to create a contextmenu by this method?
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//create Contextmenu to confirm or not
return true;
}
return super.onKeyDown(keyCode, event);
}
and how can i get a Textview or a Button in a ContextMenu() ?!?
Do u have any suggestions?
I don't think you can actually start the ContextMenu on a Button click(this menu could be handled directly by the Android system).
You should use a Dialog(or AlertDialog) to get your user choice.
I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them
For my android app I have multiple edittexts on the main screen. If i have the first edittext in focus the menu/back buttons operate fine, if i have any of the other edittexts in focus than neither of them work at all. I'm not doing anything special regarding the menu/back buttons relative to that edittext, i'm not sure what the cause is? Has anyone run into a similar issue and/or know of the cause/solution?
Thanks!
I was have the same problem and I found solution for I was have OnKeyListener that return true; when I change it to return false; the problem was fixed
my_editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// you can use the next line to ensure back button & menu button will return false
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU) return false;
// any other key you don't want to call other listener for it
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
OnKeyListener.onKey(android.view.View, int, android.view.KeyEvent)
easy fix for me, I had removed the only keyboard app.
just install a keyboard app and the buttons worked again