NavigationDrawer & KeyEvent propagation - android

When in my Activity i override onKeyDown function to control keyback action on opened NavigationDrawer like this snippet
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if (mLeftDrawer.isShown()) {
mDrawerLayout.closeDrawer(mLeftDrawer);
return false;
}
}
return super.onKeyDown(keyCode, event);
}
returning false instead of true, where is received the resulting propagation (to up) of the event?
Thanks

Hi GPack I do the same in my app, control keycode_back for control a little bit navigation drawer but I use a different code, like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//Control Navigation
//Floors level
if(keyCode == KeyEvent.KEYCODE_BACK && ActualFragment == 0)
{
if(drawerMenu.isDrawerOpen(Gravity.LEFT)){
drawerMenu.closeDrawers();
}
else{
RemoveAndReplaceFragment_ToFloors(getFragmentManager());
this.moveTaskToBack(true);
}
return true;
}
return false;
}
I think this it's that you needs... Advice me if you need more help or same!!
Good luck GPack!

Related

Android back button not working after onKeyUp() is overridden

#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
...
return false;
}
Once the above method is added to an activity, the back button stops working but the other two buttons on the navigation bar - home and overview - still work perfectly.
Could anyone shed some light on this?
Because You're always returning false from it. It should actually be conditional & in other cases, you should call super.onKeyUp(keyCode, event);
Example:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (condition == true){
//do something
return false;
}
return super.onKeyUp(keyCode,event);
}

How to obtain the currently selected View onKeyDown method?

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

Disable Back button in Digits

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

android : how could I exit the app?

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

How to listen the keypress in the soft keyboard?

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

Categories

Resources