Android pass OnClick Event to underlying View in FrameLayout - android

Idea: I want to display an image as background. On this image, somewhere on the screen there is a little clickable View. On top of this all, a black draggabke image with a little transparent whole (displaying the underlying Views).
This is solved with a FrameLayout, whereas the image is the first frame, the View is the second frame and the ''Viewport'' is the third frame.
Now the goal is, that the user can click that second frame, if it is visible in the viewport. The problem is, that this transparency is only ''in the png'', not in the frame itself. So the solution would be to propagate the OnClick coordinates to the underlying frame. How is this possible? Is there a functionality for a FrameLayout in such cases?

Making the upper layer as clickable false, i think the click event should be ignored by it and passed to the second frame

Create a custom class that extends FrameLayout
Add the following overridden methods
#Override public boolean onTouchEvent(MotionEvent event) {
return false;
}
#Override public boolean onInterceptTouchEvent(MotionEvent event) {
return true;
}
Then add touch listener to frame layout like
framelayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
underLyingView.onTouchEvent(motionEvent);
return false;
}
});
This will pass onclick event to the view underLyingView which lies under FrameLayout

Related

Android - Drag and Drop Between 2 Activities

Let's say:
Activity1 contains some items which will be dragged
Activity2 contains some "boxes" which will be filled with Activity1's item. The boxes already have drag listener.
I want to drag item from Activity1 into Activity2
I tried these method:
I implement onLongClickListener() on Activity1 items and use startDrag() there. Then I open Activity2. I could make the drag shadow appear but the boxes cannot receive the item. In fact, they doesn't respond to any DragEvent.
I implement onLongClickListener() on Activity1 items, but only to pass the data into Activity2. Then I use startDrag() when Activity2 start(specifically in onResume()). Here, the shadow not appear and the boxes doesn't respond to any DragEvent.
Is there any way to make this possible?
its possible using two fragment of view pager instead of 2 activity and you can set logic of drag and drop in onTouch() or dispatchTouch().
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
using dispatchTouch() in activity of fragment or
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
using touch event of view pager you can set drag and drop logic within inside.
I hope this idea can help you.

Disable onClicklistner while other touch listner is active

I am trying to implement UI like google keep.
In the main layout there is frame layout in which there is a FAM(Floating Actions Menu) and a blackshadow view which is visible when FAM is expanded.
What i want is that when i touch the shadow view it should Collapse the FAM. To do that i have implemented onTouchlistner on shadowview.
shadowview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(fam.isExpanded()){
fam.collapse();
}
return false;
}
});
But what happens is that when i touch the area with MyCardGridView's card (Which open's another activity) open's another activity. Which should not happen.
You should try to use return true; instead of return false;, because as the documentation says :
Returns:
True if the listener has consumed the event, false otherwise.
Maybe the issue here is caused by return false

How to trigger onClickListener for HorizontalScrollView?

The question explains everything I want.
HorizontalScrollView seems to be the only widget for which setOnClickListener() does'nt trigger.
NOTE: I can't use onTouch event because on every touch it is triggered 4-5 times.
I also can't use onTouch on its parent view because parent view has many buttons with different functionality.
The following description is not important:
But still, these are the existing links I've searched(none helped):
horizontalscrollview onclicklistener doesn't get called
How to implement on click listener for horizontalscrollview
Click event of HorizontalScrollView
Android click event of items in HorizontalScrollView not respond after scroll
These are the links which I posted(none got answered completely):
Insert views in HorizontalScrollView programatically
One of the ImageButton not clicking & make HorizontalScrollView clickable
I asked multiple questions in these links, 1 of which was "onClickListener for HorizontalScrollView". That part of my question was never answered.
Hence an all exclusive question.
I came up with two ways of doing it.
The simpler (but less ideal) one:
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// Do stuff
}
return false;
}
});
This will get called only once when you click, drag or whatever you do with your finger. However, it will also react to all kinds of gestures, so if you want to detect only tap/click events, you need to further investigate the "event" object and filter out events you don't need. This may be more work than you would like, so you should better use a GestureDetector to do it for you.
This leads to method 2:
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
final GestureDetector detector = new GestureDetector(this, new OnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
// Do stuff.
return false;
}
// Note that there are more methods which will appear here
// (which you probably don't need).
});
scrollView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return false;
}
});
Well it happened to me now and I came here for solution, but in my case it was just easier to listen to onClick to its child view instead...

SeekBar inside a draggable view (SlideHolder)

I use a 3rd party component (AndroidSideMenu) which allows the whole activity to be dragged to the right to expose a side menu.
In the main (non-menu) section of the layout I have a horizontal SeekBar.
The problem is that when I start dragging the SeekBar button, the whole layout drags along with it because the SlideHolder component is also responding to the drag events.
I'm not sure how touch events are managed in Android, but is there a way to stop the events after they are processed by the SeekBar so that they don't reach the parent container?
So when you drag the Seekbar, the whole screen (parent) must stay put, but when you drag outside the SeekBar it must still work as required for the side menu?
Thanks
In the container of the SeekBar, create and apply (to the seekBar) an OnTouchListener like the following:
private final OnTouchListener onTouchListener = new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
// If the View in question isn't a child of the seekBar and
// isn't the seekBar itself, then return and don't consume the event.
if ( (seekBar.findViewById(v.getId()) == null) &&
(v.getId() != seekBar.getId()) )
{
return false;
}
// The View is either a child of the seekBar or is the seekBar itself,
// so we pass the event along to the seekBar and return true, signifying
// that the event has been consumed.
seekBar.onTouchEvent(event);
return true;
}
}
Try this out and see if it works; you may need to create a more robust condition for the if statement.

two views ontouchlistener same area how to?

I have a two ImageViews.
1.ImageView zoomImageMoveable : should zoom in,out and moveable.
2.ImageView zoomImageFixed : show zoom this is guide line.
I want run different functions from one touch.
{
FrameLayout myFrame = new FrameLayout(this.getContext());
myFrame.addView(zoomImageMoveable);
myFrame.addView(zoomImageFixed);
}
Case 1. I tried to attach a listener to each of the view, as they are overlapping only one works.
Case 2. I also tried to wrap the two view with a FrameLayout and added an OnTouchListener to that layout to have it forward the calls to each of the assigned methods. Yet this method also didn't work.
myFrame.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
zoomImageMoveable.onTouchEvent(event);
zoomImageFixed.onTouchEvent(event);
return false;
}
});
How should I approach this issue?

Categories

Resources