I created a popupwindow to show a menu. I found that if I press the cancel button on keyboard, the popupwindow will dismiss, and setFocusable() only disable the buttons like menu, but cancel button still works.
If there exists a method to make popupwindow invalid for popupwindow, or define the action myself when a cancel button is pressed? Thanks.
Well I mean back button when i say cancel button. And Thanks for sachy and other people who reply me.
By cancel button do you mean back button? If yes than u can simply override onKeyDown().
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d("back", "back button pressed");
}
return true; //to prevent this event from being propagated further.
}
Please explain more to now exactly what you want to achieve. If you just want to disable the button you might try this.
Button button = (Button)findViewById(R.id.button1);
button.setEnabled(false);
Or you want to override the cancel button? Check this out then: Back button behavior
Related
I have a problem in all my activities, when I press the back button it works properly, but when I press the context menu button before, nothing happens when I press back.
I am talking about the phone buttons, not the toolbar icon for back navigation. Has anyone ever dealt with this?
EDIT:
No menu shows, onPrepareOptionsMenu() is removed, the phone vibrates when I press back but no action.
I see that if I inflate a menu, it worsk properly after the first back that closes te popup.
I see that this happens only if I set the toolbar with setSupportActionBar(toolbar);
I have nearly the same problem, this is my case:
AppCompatActivity with toolbar set with setSupportActionBar(toolbar);
I'm using a navigation Drawer (dont know if this can affect)
onBackPressed() is overriden to prompt logout dialog
All works fine until i press the device menu button, then the app begins to ignore the back button.
This is what i have seen after pressing the device menu button
The method onBackPressed() is not called after this
If you set an onKeyUp(int keyCode, KeyEvent event), it receives the event (and you can check that de keyCode is the same as KeyEvent.KEYCODE_BACK)
I don't know why the onBackPressed stops from being called, tried without overriding onBackPressed and onKeyUp and the problem is still the same.
I know this is not the best solution, but to fix this problem i'm using the next code:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
Hope this helps.
I am using a device having keypad(Hardware) attached on phone device.Now I want to get the event after clicking OK button on keypad.
i found we use DPAD_CENTER for specifiying OK Button.
Can anyone helps me on this. How set listener for this OK button. Thanks
Override onkeydown function and you can do actions for whichever hardware key is pressed
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
// Do what you have to do here
}
return false;
}
I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that? If that is not possible then is it possible that some other action is performed when hone is pressed. the whole idea is user should not leave the app directly from any screen.
update:can someone tell me how to define my app as launcher?
No, you cannot. Whenever Home button is pressed, the framework will always throw you back on android's home screen. Sorry, you are out of luck. :)
you cant make it work as you want, but you can disable it
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
and you can tell user that home button is disabled:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context,"Home button is disabled",1);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that?
Make your app be the home screen. The user can still remove your app by rebooting in safe mode.
How to add a listener to home button? i.e I want to add some functionality when home button is pressed. How do I do it?
I did the following, but it is not working for home button. It's working only for back button.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// some functionality
}
if (keyCode == KeyEvent.KEYCODE_HOME)
{
// some functionality
}
// // TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
don't do that. its not correct to do that because the only use of home button is to exit from the application at any point. you can use isFinishing() if u want to execute any code while leaving an activity. have a look into this.
You must override onAttachedToWindow() first.
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
In addition, you need to add the permission:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD "></uses-permission>
You can't do that. The Android policy forbids that a App can overwrite or listen to the Homebutton.
Maybe you can accomplish what you want with overwriting onDetachFromWindow.
You can't but you could use onPause(). The application will be paused when the home button is clicked.
How to add a listener to home button?
You don't. The HOME button always brings up the user's chosen home screen. You can elect to make your app be a home screen, by supporting CATEGORY_HOME in an activity, but then it had better really work as a home screen.
This problem may seem trivial but I wasn't able to find any nice and simple solution.
I've got an activity with a EditText and a software 'back' Button which simply calls finish() method of activity.
When I click on the EditText, there is a soft keyboard shown to input the text.
I want to achieve the following functionality when clicking the 'back' button (exactly the same as it is with the hardware back button):
- when the Keyboard is hidden, the onClick method should call finish() to end the activity
- when the Keyboard is shown, the onClick methond should hide the keyboard.
Is there any simple way to do that?
Keyboard Pasition
finding if keyboard is hidden or not?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Selection of Back Button
1)First you have to detect the back key in functionality :
here is code:
start changing the ‘Back’ button, behavior, you need to override the onKeyDown()
method and than check if the desired button has been pressed:
//Override the onKeyDown method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
}
return true;
}
at least for Android 2.0.
#Override
public void onBackPressed()
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
return;
}