I want to detect Back Key event in my CustomView (e.g., EditText). In many case, it has been achieved by overriding the onKeyDown() or dispatchKeyEvent(), under the condition that my CustomView obtains focus.
CustomView.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK) {
.....
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
However, if an Activity including the CustomView is also overriding the onKeyDown() or dispatchKeyEvent(), it couldn't work much. That is, the Activity has obtained the Back-KeyEvent before the CustomView has.
I preferentially want to catch the Back-KeyEvent before Activity does.
please tell me some ideas about this problem.
Thank you.
You need to implement this to capture the BACK button before it is dispatched to the IME:
http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int,android.view.KeyEvent)
Override onKeyDown in your Activity and return false. So that the event gets propagated to other views as well.
If you handled the event, return true. If you want to allow the event
to be handled by the next receiver, return false.
You can try to use
setFocusableInTouchMode(true)
setFocusable(true)
requestFocus()
on your customview
Related
Is there any way I can force show Android's SoftKeyboard in NumberPassword mode without having an actual EditText in my activity?
I managed to show the keyboard when the activity starts by adding android:windowSoftInputMode="stateAlwaysVisible" on my AndroidManifest.xml, make it impossible to close by overriding onKeyPreIme in my CustomView class that extends TextView, and handling the touch events by myself by overriding onKeyUp in my Activity.
If I add android:inputType="numberPassword" directly in CustomView's XML Layout, Activity's onKeyUp gets bypassed and the keyboard write characters in my CustomView and KEYCODE_ENTER closes my keyboard.
What I want to achieve is:
SoftKeyboard always out, both on Activity Start and Resume from the background
Cannot be closed with KEYCODE_ENTER or KEYCODE_BACK
9-digit layout + Backspace
Handle by myself key pressure to make it do something else instead of writing characters
Taken from https://developer.android.com/training/keyboard-input/commands.html for convenience:
Both the Activity and View class implement the KeyEvent.Callback
interface, so you should generally override the callback methods in
your extension of these classes as appropriate.
I suggest you to override the default implementation of onKeyUp in your CustomView class, and make the CustomView.onKeyUp method redirect the event to your Activity's onKeyUp method.
As an example:
public class CustomView extends TextView {
private KeyEvent.Callback myKeyEventCallback;
public void setCustomKeyEventCallback(KeyEvent.Callback callback) {
myKeyEventCallback = callback;
}
...
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return myKeyEventCallback.onKeyUp(keyCode, event);
}
}
And in your Activity do this:
CustomView view = ...; // here you take the reference to your custom view
view.setCustomKeyEventCallback(new KeyEvent.Callback() {
// ... other methods
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// this calls your activity's implementation of onKeyUp
MyActivity.this.onKeyUp(keyCode, event);
return false; // prevent event from firing twice
}
});
This will help you redirect the onKeyUp method calls from your CustomView to your Activity's onKeyUp implementation.
Is it possible to override all the buttons in the Android navigation bar?
Is it maybe possible to add a new button?
How can I change the appearance of the buttons? I want to set an other background bitmap.
I am using a Nexus 7.
I just know onBackPressed() but what about the other two buttons?
And how are they called?
I think that you need to override this methode !
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
OnClick click = new OnClick();
click.onClick(findViewById(R.id.btn_cancel));
}
return super.onKeyDown(keyCode, event);
}
I just used this methode to handle save event for my form and you can use to handle pushing other keys using KeyEvent parameters.
Good luck.
I have a layout in my activity with some UI elements in it. I want to handle long press for the whole activity.Also I need to handle click events for all the UI events seperately. How can I do it? Pls help.
your class can use the interface onLongClickListener
like yourclass extends Activity implements View.OnLongClickListener which has the method that get notified on long press.
Note: Don't forget to set yourView.setOnLongClickListener(this); for all the views you need
Rename your layout just the biggest of(linear layout, relative layout or...) in xml file and use this coDe
Layout ly=(Layout) findVewById(R.id.yourlayout);
ly.setOnLongClickListener (new. Onlongclicklistener(){
#Override
public boolean onTouchEvent(MotionEvent event) {
//do something
}
}
Example of code an Activity subclass can use to implement special actions for a long press
CALL key:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL) {
// a long press of the call key.
// do our work, returning true to consume it. by
// returning true, the framework knows an action has
// been performed on the long press, so will set the
// canceled flag for the following up event.
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Check http://android-developers.blogspot.in/2009_12_01_archive.html
I need to change the KeyEvent dispatch in TabActivity. If the current tab content activity/view can handled the KeyEvent.KEYCODE_Back, let it handled it. If not, show dialog to tip like this:"Would you want exit?". How can do this?
I have tried by this code in my TabActivity:(can't implement my demand)
#Override
public boolean dispatchKeyEvent(Event event){
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
if(!mTabHost.getCurrentView.dispatchKeyEvent(event)){
showDialog(0);
return true;
}
}
return super.dispatchKeyEvent(event);
}
I think this link can make you clear.
you should override onKeyDown method, and directly call mTabHost.onKeyDown(...) method, check it return result, if false, it means you should show the exit message to user.
hope can help you.
Hi guys i am developing an application with Extending fragment activity,here is my problem when i have used the normal activity is i have used below method.
onKeyDown(KeyEvent.KEYCODE_BACK, newKeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_BACK));
For activity what i need to do?
How can i achieve this goal
I found this link to help. Sorry that its old, but it kept coming up on the google.
Fragment: which callback invoked when press back button & customize it
Setup the callback in the activity. Then the fragment doesnt need an #Overrride.
note this will work for the back button, as well as onKeyDown.
I've implemented onBackPressed in my (Fragment)Activity and simply forwarded an appropriate message to the relevant fragment.
Furthermore can't you implement your onKeyDown event in your parent activity and do what you need to do which may be passing messages to your fragments?
Hi Please try below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i1 = new Intent(Activity1.class, Activity2.class);
startActivity(i1);
return false;
}
return super.onKeyDown(keyCode, event);
}