While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Related
I'm trying find if a specific View is selected following a key press. For this purpose I was trying to use the onKeyDown method on my main activity.
I already tried to set a setOnKeyListener on the fragment but it wasn't registering any input.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
// Check if it's selecting a specific View
}
return super.onKeyDown(keyCode, event);
}
Any idea how I could do this?
one from many approach that may use, fun n silly maybe, but it was work for me
on Global declaration
Integer iLastView=0;
on View that selected before
iLastView=iView;
and last in the inKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if(iLastView==iView1){
//something here
}else if(check){.....}
}
return super.onKeyDown(keyCode, event);
}
In my android application I changed the back button functionality so that it goes to the main screen of my game , now that it's on the main screen how should I exit the whole application with back button ?
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
return super.onKeyDown(keyCode, event);
}
If you have a mechanism that you can use to see which screen is showing you could do something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
if(mainScreenIsShowing == true){
//If the main screen is showing let the back button
//have its default behavior.
return super.onKeyDown(keyCode, event);
}else{
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can also check if the user enters the back button two times.
boolean backPressed = false;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && !backPressed) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
backPressed = true;
return true;
}
backPressed = false;
return super.onKeyDown(keyCode, event);
}
This is a debateable subject, but I see nothing wrong or with an application exiting when the back button is pressed. After all, a call to finish() is the default behaviour of the back button. If the activity handling your main screen is at the bottom of your activity stack, then a call to finish() will exit your application.
I propose the following: Let your MainMenuScreen be handled in a separate activity, MainMenuActivity, which is the main activity. finish() other activities when going back to the MainMenuActivity, and handle onKeyDown like this in MainMenuActivity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
this.finish()
}
}
I need a listener to identify the keypress in the soft keyboard/on screen keyboard.
I tried with addtextchangelistener textwatcher but this one give the good result but it shows the change also when some text is pasted into it.
I need to identify only the key press by the user.
Is there any possible way to detect the key press.
see this keyevent and use following code to identify which key is pressed by Users.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// Do Code here
}
else if(keyCode == KeyEvent.KEYCODE_0)
{
}
else if(keyCode == KeyEvent.KEYCODE_1)
{
}
return super.onKeyDown(keyCode, event); }
When handling keyboard events with the KeyEvent class and related
APIs, you should expect that such keyboard events come only from a
hardware keyboard. You should never rely on receiving key events for
any key on a soft input method (an on-screen keyboard).
see: Handling Keyboard Actions
See this if can help you.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do Code here
}
return super.onKeyDown(keyCode, event);
}
i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.
How to capture when user presses "Search Soft key" in android.
Can we do it in onKeyDown() and checking the keycode?
Try this in your Activity code:
#Override
public boolean onKeyDown( int keyCode, KeyEvent event )
{
if ( event.getKeyCode() == KeyEvent.KEYCODE_SEARCH )
{
// Catch the search
// do something
// return true to consume press or false to pass it on
}
return super.onKeyDown( keyCode, event );
}
I think that you are right. You can check for the key code that is given in the following list.
And prefer to check this onKeyUp.
http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_SEARCH
I've done something like this for capturing the search softkey when progress dialog is showing and it worked fine for me:
progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
});