Android Dialog OnKeyUp (KeyEvent)? - android

I have a dialog and on in this dialog I have a button. When pressed search is suppose to appear. And this works completely fine.
BUT I want to able to set up OnKeyUp event so if the user clicks on the actual search button on the phone (not on the screen), search view appears.
I was able to do this in an activity this way:
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Search) {
// do something here
}
return base.OnKeyUp (keyCode, e);
}
But that's an override method. How do I call that for a Dialog?
This is how I've created the Dialog:
multiSelectDialog = new Dialog (context);
multiSelectDialog.RequestWindowFeature (1);
multiSelectDialog.SetContentView (dialogView);
multiSelectDialog.Show ();
Dialog has the OnKeyUp property.
multiSelectDialog.OnKeyUp(KeyCode.Search, KeyEvent e);
But I am not sure how to create a KeyEvent.
Thank you for your time.

onKeyUp() is a callback on Dialog, it will be called on a key-up event, you don't call it yourself.
Instead you have to extend Dialog and override onKeyUp(), for example:
multiSelectDialog = new Dialog(context) {
#Override
public boolean onKeyUp (int keyCode, KeyEvent event) {
// do something here
return true; // or false, depending on what you want to do
}
};
multiSelectDialog.requestWindowFeature(1);
multiSelectDialog.setContentView(dialogView);
multiSelectDialog.show();

Related

How to intercept the back button on an Android Service?

I'm displaying a layout over all the other apps with ApplicationOverlay and the WindowManager.
When I click the back button. The OnBackPressed() override of my activity doesn't fire because of the overlay.
How can I detect and cancel the back button from a Service on android so that when I press back I can make my application change its layout?
If you have the overlay attached to the WindowsManager, you just need to add a listener to the view:
view.setFocusableInTouchMode(true);
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
stopSelf();
return true;
}
return false;
}
});
When you create WindowManagerLayoutParams add the below flags:
layoutParams.Flags = WindowManagerFlags.NotTouchModal;
layoutParams.Flags = WindowManagerFlags.NotFocusable;
then you could fire the OnBackPressed() method in your activity.

android - why alertDialog showing on homescreen?

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;
}
});

Cannot cancel a KeyEvent

I have a Bluetooth barcode scanner that acts as a hardware keyboard. I implemented these methods in my Activity (just testing):
#Override
public boolean onKeyUp (int keyCode, KeyEvent event) {
Log.d("debug", "up: "+KeyEvent.keyCodeToString(keyCode));
return true;
}
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
Log.d("debug", "down: "+KeyEvent.keyCodeToString(keyCode));
return true;
}
Yet, whatever I return (be it true or false), the keys are always also handled by the default handler, resulting in unwanted behavior.
Update it seems to be missing the KEYCODE_ENTER, which is handled by a default handler. I tried explicitly setting setDefaultKeyMode(DEFAULT_KEYS_DISABLE), but it had no effect.
Am I missing something here?
There are some buttons on the Activity's view that were focused. The focused Button would get my KEYCODE_ENTER and handle it, leaving nothing for my Activity.

Inside the settouch interceptor unable to call the functions in android?

In my application I am using a popup window. For dismissing the popup window when the user touches outside, I am using the ORDER LIST PW CODING coding below, the pop window dismiss function works perfectly but not able to call
Submenu_listtask = new Submenu_list();
Submenu_listtask.execute();
this function inside setTouchInterceptor its not working. Can any one know please help me.
ORDER LIST PW CODING
order_list_pw.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//Unable to call this function
Submenu_listtask = new Submenu_list();
Submenu_listtask.execute();
order_list_pw.dismiss();
Log.v("Name", "Name");
return true;
}
return false;
}
});
Return true at the end of onTouch() as returning false only enables you to catch ACTION_DOWN event

How to handle hardware search button android?

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

Categories

Resources