Listener when hovering view Android - android

The user can draw over view (with a pencil) and i want a listener to know if the user was drawn over other views. When the user is drawing over a view, i want to change the view's color.
I tried with OnTouchListener, OnHoverListener, OnFocusChangeListener i also tried to override the method onTouchEvent, but none of these were called.
Screenshot of the sample:
http://imgur.com/u3tkwRN

You should handle onTouchListener by calling ViewObject.setOnTouchListener the OnTouchListener passes to argument . The first is the Calling View and the second is a MotionEvent object . The motion event has a getAction() method than returns a integer you should write a if statement and check whether the getAction() value equals to MotionEvent.ACTION_DOWN . In the block of the if statement write your code . Also you can handle unhover event by write another if statement and check whether the getAction() integer equals to MotionEvent.ACTION_UP and write another code in if block statement . Your problem will be solved completely.

Related

Can I get onBackPressed event inside a custom ViewGroup?

I am creating a custom Android component that needs sometimes to consume the onBackPressed event (e.g. there's a popup menu inside the custom ViewGroup, if it's showing, the back button event closes it and is consumed, otherwise it's ignored). Is that possible? Can I intercept this event from inside my ViewGroup subclass and how?
Edit:
I tried overriding onKeyPreIme as the Android documentation implies, the method is never invoked from within ViewGroup.
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
return true;
}
In the onKeyPreIme documentation it says:
Handle a key event before it is processed by any input method associated with the view hierarchy. This can be used to intercept key events in special situations before the IME consumes them; a typical example would be handling the BACK key to update the application's UI instead of allowing the IME to see it and close itself.
The reason why onKeyDown or onKeyPreIme are never invoked is because the ViewGroup does not have focus.
The solution was to request focus in the ViewGroup subclass' constructor:
this.setFocusable(true);
this.setFocusableInTouchMode(true);
this.requestFocus();
onBackPressed belongs to Activity and views/viewgroups are something placed inside Activity. So technically, you cant get this done. However, you may use some kind of observer/observable pattern to pass any kind of information to your views.
Or perhaps try using onKeyDown event inside your view class and track the back key event code.

implementing ontouchListener on emulator

I want to implement ontouchlistener but i don't have actual device to test it.I have already implemented onclicklistener.how can I implement both onclickLisrener and ontouhLisrener with emulator.
thank you
Return values of onClick method and onTouch method set to false.
If you set "return true;", the Android stop to find event listeners.
So you want to enable two or more event listeners, you have to set return value false.

Onkeydown what's the diference between return false and return super.onkeydown()?

I put a list view inside a linearlayout,and I want to override onkeydown() method in ListView
,and I don't control the focus, just change some variables in onkeydown(),I want system do as if I haven't override the onkeydown method.what should I do?return false? or return super.onkeyDown()?.It will be very nice for any help. thank you in advance.
there are three listviews,and a b c are three item of listviews. when c is focused, when I press left arrow on keybord, then a get focus by default. And I want the first item in listview2 get focus how can I do?
ntc is not exactly correct. OS does not use reflection to get your base class' handler.
If you return false, you explicitly tell OS that you do not want to handle this event; OS calls the View's parent handler then (as your test shows); this happens until event got handled or top View is reached.
If you call super.OnKeyDown() you allow your base class to process event. Note that it's a base class' handler serving same view, not parent view; don't confuse here.
So, using one or the other depends on your view's behavior desired.
I supose in your case you need return super.onKeyDown();
super.onkeyDown()- this puts the burden on super class to handle the onKeyDown event by your own wish (You explicitly say to handle it). when you return false, android assumes that you have not handled the onKeyDown event and super.onKeyDown() gets called by default (without you calling it).

How to get onTouchEvent, long click and context menu working together?

In our application we have a custom view (that extends ImageView) and in it we handle the touch events to record data. I wanted to add context menu functionality to this view and followed the guidelines in the official Android documents.
The onTouchEvent code works fine by itself. The context menu code also works fine. However, if I add them both, the context menu code stops working. I found out that with both pieces of code added, onCreateContextMenu is never called, therefore context menu is never displayed.
According to my interpretation of the Android documentation, returning false from onTouchEvent indicates that the event is not consumed, so it should be used for further processing. For some reason, it is not happening here. I would appreciate if anybody can tell me what I am missing. BTW, the target is Nexus One running 2.3.4 ROM.
Here's the code for the onTouchEvent in the custom view:
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
// Add event coordinates to an arraylist
break;
}
return false;
}
Thank you in advance for any help.
Elaborating on hackbod answer, you should probably have as last method statement return super.onTouchEvent(event);.
I guess that if you don't process the event, and if you don't invoke the default View behavior, than no one will do anything, and nothing will happen.
The point of return value might be for example to invoke some ancestor' default behavior, and let the derived class know if the ancestor processed the event or not.
After doing some search on Android Developers, referring to the topic override an existing callback method for the View here it says :
This allows you to define the default behavior for each event inside your custom View and determine whether the event should be passed on to some other child View.
Hence the main idea behind the return value is to let Android know whether the event should be passed down to child Views or not.
HTH
Edit:
Regarding the "directions" you mention in your comment, generally speaking (i.e. not only on Android) the UI event handling process goes on something like this:
At some point your derived custom control receives the event. In your event hook implementation, it's up to you whether to involve your ancestor's behavior or not. That's all you got regarding the class inheritance direction.
Then, there's the other direction, the one related to the UI controls hierarchy. Your custom control might be contained in one larger control container, and your control might as well contain other inner controls (textboxes, buttons, ...). Regarding this direction, if you declare not to process the event (returning false) then the UI event handling mechanism will pass the bucket to the containing (?) control (think the one on the background of yours).
You could call, from your long click listener,
openContextMenu(View view)
http://developer.android.com/reference/android/app/Activity.html#openContextMenu(android.view.View)
Do not register for context menu in OnCreate(), do it in onTouch() before
return true;
registerForContextMenu(View v);
openContextMenu(View v);
return true;
Returning false tells the parent that you didn't consume the event. The default implementation of View implements touch handling for that view; if you want that to execute, you must call super.onTouchEvent(event);
I encounter similar problem recently. When I enable long clickable in RecyeclerView's child, the ACTION_DOWN event can't not be received in RecyclerView's onTouchEvent.
If I changed to RecyclerView's dispatchTouchEvent, I would works. The ACTION_DOWN event can be received.

Does onInterceptTouchEvent() really works as the sdk said?

ths sdk said:
3、For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
4、If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
But when i use this method,no matter what onInterceptTouchEvent() returns ,it does the same work! And never did MotionEvent.ACTION_MOVE or MotionEvent.ACTION_UP been captured by this method. Can anybody help me figure it out?
ths!
Normally the touch goes from most upper view to the lowest through onInterceptTouchEvent and then it goes back via onTouchEvent.
If you return true in onInterceptTouchEvent you forbid it to continue and the view where you returned true is the last one to receive the touch, you consume it
You can also disallow your parent view to consume the event by
requestDisallowInterceptTouchEvent(true);
When none of the children of your view return true in onTouchEvent, onInterceptTouchEvent will ONLY be called for MotionEvent.ACTION_DOWN.
Here is a complete description of the MotionEvent processing.

Categories

Resources