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
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.
I've been learning android for almost a month, and I want to make a simple game with a custom class that extends from View, and it's included on main_activity.xml. In Main Activity.class I create an instance of the View, and control the movement of the sprite on the GameView with buttons, so each button has a method that control sprite's movement like this:
public void move_up(View v){
gameview.sprite.move(Dir.UP);}
The problem is that it only works when the button is released, and it's executed one time. I want the method to be executed while the botton is pressed but I can't figure out how to do this.
I'm assuming by the name of your method that you're adding the onClick property to the xml layout file in order to handle the event. While this may seem convenient at first it will not give you what you need.
Instead, you should implement the OnTouchListener which gives you information about what touch event is currently happening, in your case you'd want to handle the ACTION_DOWN action:
findViewById(R.id.btn_up).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}
return false;
}
});
Although you could make your Activity implement the listener and handle multiple buttons there. This is achieved simply by telling your activity to implement OnTouchListener, imeplementing the method onTouch as in the code above but instead as a method of your activity.
And the resulting set, would be simplified to findViewById(R.id.btn_up).setOnTouchListener(this); where R.id.btn_up is the id you've defined in the xml file.
This would make it start moving when they press, instead of when they release, if what you want is to make it move until they release (which would make sense), do something like:
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}else
if(event.getAction() == MotionEvent.ACTION_UP){
gameview.sprite.stop(); //Or whatever you call the stop method
return false;
}
I am using onTouchListener for a layout. I want to take the click outside the layout. I set onTouchListetener for the layout. But motion event always shows ACTION_DOWN. Even i touchOutside the view, It is not showing ACTION_OUTSIDE. Could anyone help me to find out why it is not showing constant ACTION_OUTSIDE. Here is the code i am using
Layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("action",event.getAction()+"");
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Toast.makeText(getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
If you return false, then you're signifying that you do not wish to receive further touch events. You need to return true to continue getting motion events.
Very simple to fix by adding this:
override fun onTouchEvent(event: MotionEvent): Boolean {
return true
}
The event listener itself listens to one event action at a time. The first of course is the ACTION.DOWN, in which your toast shows.
Just like what Jason Robinson and user936414, you have to return it to true so that the object or the listener could here the second event action, ACTION.OUTSIDE.
You will get ACTION_OUTSIDE when the touch is outside the activity and the flag FLAG_WATCH_OUTSIDE_TOUCH is set. Refer http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_WATCH_OUTSIDE_TOUCH
I call this function in my activity :
#Override
public boolean dispatchTouchEvent(MotionEvent touchEvent)
That allows me to process action before any components get focused or even deny the focus to these elements.
PROBLEM : I was wondering how I could know what component (View) has been touched in this function, then I could choose if I want to consumme the event or not.
UGLY SOLUTION : I'm currently having an ugly solution which is : I know the position of the component that is allowed to get the event, and I do a plenty of condition to approximately decide if the user clicked on this component.
Thanks.
You probably want to use the OnTouchListener
private OnTouchListener mOnTouchListener= new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.id1):
// Do stuff
break;
case R.id.id2:
// Do stuff
break;
}
return false/true;
}
};
view.getid==R.id.//id in layout// condition can be checked for the required view is clicked
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