I have a layout similar to the capture. The green rectangles are placed in horizontal scrollers and when the user touches one, another activity is launched. The blue rectangles are other layers, like the horizontal scrollers.
My problem: when the user touches a green rectangle, I need to know in which RED rectangle the touched green rectangle is placed in.
Is there any easy way to get a particular ancestor of a view? I would like to be able to add and remove more "blue rectangles", so I don't want something like view.getParent().getParent().getParent() .
Thanks!
Two ways,
Simpler one:
Set the red ones as tag to all the child it contains, now when a child receive Touch you can easily figure out who is the parent among red, by getTag method.
Little Complex but cooler:
a. Maintain a list of all the red view
b. Implement on Touch on green one
c. Get the x,y when you receive touch
d. Once you receive Touch, loop through list of red view, getting Hit Rect for each red view, once you get the hit rect, just check if it contains ur received x,y. Once you found match return. Wollah you have your parent view.
Hope it help.
Related
I have three layout layers and over the blue one, two views (EditText and a simply view), so let's put some context:
When I set on click listener only to yellow layer and touch inside the blue one for example, the click is listened only in the yellow layer, the click is transfer. ClickTransferBetweenChilds
When I set on Click listener in all layers the click listener is triggered when I touch anywhere inside each layer. ClickNotTransferWhenSetClickListener
This is the default android behavior and I call this layer stack, parent-child views, there's the following schema:
Yellow - is parent of -> Red - is parent of -> Blue.
Now focus on the blue layer, over the blue layer are super posed two elements, the EditText and the View, the EditText is over the View, and initially with the default android behavior when I touch the upper left side, the keyboard is called (because the EditText is below) but I want to avoid this behavior. Please call it brother propagation.
I make a simply solution please refer to line 71 Gist in MainActivity.java, but we can't implement in production because I can only touch the Layout things (the yellow one - Parent - at possible).
TL/DR: I want to avoid this behavior ClickTransferBetweenBrothers how to avoid transfer click between brothers keeping the click transfer between parent-child views.
How would you go on implementing such a compound view :
note that it contains 2 buttons, A and B.
If you want to do this in a single view, then you could draw filled path (arc) for A and B. For touch detection, you might override the onTouch to know if the user tapped on A region or B region. You could expose this outside the view via an Interface. The advantage here is that you could easily dynamically change the A and B region. The partition can be easily modified.
But if you are not after dynamic change then you could simply have a linearLayout with vertical orientation and have two buttons with the background of a sector. (just like Frank N. Stein suggested in the comment)
When covering a view with an overlay I am aware that I need to use a FrameLayout, and the child views stack on top of each other. Setting a view to having a background with opacity will create the overlay effect. However, in my situation, I am required to create a hole in my overlay to display the contents of a tab item / other views beneath it. This is where I am stuck.
I am not sure how to perform the intersection on the view so that it is transparent in a specific area of the view (which I need to determine based upon the position of a specific View element (Tab, ImageView, etc).
The image above shows the result I want to achieve. With the entire view having an overlay on it, besides one of the Tabs and the Info box (which needs to move based on the view displayed through the overlay).
I believe I need an intersection of the views to achieve the result I need...this is something I am not sure about. Can someone point me in the right direction of a solution please, I do not expect to be just given a solution, its all about learning!
Here is the scenario; I have 3 PNG photos i wanna use as background of buttons or ImageViews and they are overlapping in a relative layout. kinda like this:
so the red button will be the biggest and go under all of them, then i will add green button on top of red and then yellow button on top of green. so that's how it looks like. each button has a PNG background as i said at the beginning.
Problem is I cant make the only visible area of each Button/ImageView clickable! Android kinda considers each at rectangle button/ImageView.
Any solution for this?
In your onTouchListener you should check whether the event (MotionEvent) is in the transparent area of the background or not.
Either you can make a separate onTouchListener for each view/button and return false if the event is in the transparent area (of the View argument) or you can make a single listener for all of the buttons, ignore the View argument and check all of your three views to determine in which one the event is.
I have recently encountered a very specific issue/requirement regarding an Android Button and ListView item. The requirement is something like this:
we need a button that can display a glow/aura of a specific dimension and opacity when clicked, however this glow/aura must not be part of the button itself and needs to be displayed over the neighbouring views.
same requirement for list view items - they need to glow over their neighbouring items and the list view margin.
Any idea/suggestion would be highly appreciated.
Thanks.
A solution could be in drawing a round rectangle with stroke around the button. You can get the button position using View getTop(), getLeft(), getRight() and getBottom() methods.
The round rectangle could be a custom view that has the onDraw() method overridden. You can also have a paint attribute, in this view, if you what to add the gradient and opacity, to look more like an aura. When you first create the view it should be invisible or transparent and only on demand it should appear.
From your main activity you have to add this new view, by using the layout.addView() method and depending on your demands you can make it visible and/or change their properties.
An example can be found here: https://github.com/codesorcerers/auraview
Hope this helps!
Bogdan Popa