How avoid the click transfer between brother views? - android

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.

Related

How to make area editable/not editable using Togglebutton?

I was trying to make an area so when ToggleButton is on, you are able to toggle buttons within this area and if ToggleButon is off, you won't be able to toggle the buttons within this area.
For this specific area I used a FrameLayout, so I can stack two LinearLayout on top of each other. So when I hit the Edit-Button, the one on top which is half transparent disappears with setVisibility(View.GONE) and you are now allowed to click the buttons now.
PROBLEM: The buttons in this area are always clickable
I was able to toggle them separately with button.setClickable(true/false) but is there a solution so you just can't click through the LinearLayout that is on top (like "not-through-clickable")?
Link to an image of the Layout: https://i.imgur.com/eTyhCDc.png
Desired behavior:
Hit EDIT -> half transparent Layout on top of blue Layout disappears -> TESTOFF1 and TESTOFF2 are now clickable.
-> Hit DONE -> the half transparent Layout appears on top again and the buttons below it are not clickable anymore (without using .setClickable for every single button).
The view on top gets first crack at any touch events. If the top view doesn't handle the touch then the view below get a chance on down to the bottom view. The top layer is a LinearLayout so, but default, it doesn't handle touches, so it allows the touch events to percolate down to the buttons which are happy to respond.
One way to resolve this is to place a touch listener on the semi-transparent view that just returns true. Thus, the LinearLayout will consume the events and not let the buttons see them.
When the view is gone, the buttons become the top view so they will see the clicks.
If you want to know more about how touch events are handled, read this Stack Overflow answer for an excellent explanation.
editButton.setOnClickListener {
button1.isEnabled = !button1.isEnabled
button2.isEnabled = !button2.isEnabled
editButton.setText(
if (button1.isEnabled) {
R.string.done
} else {
R.string.edit
}
)
transparentView.isInvisible = button1.isEnabled
}
This example is using kotlin and the Android KTX libraries
Java version
editButton.setOnClickListener(new OnClickListener {
button1.setEnabled(!button1.isEnabled());
button2.setEnabled(!button2.isEnabled());
editButton.setText(button1.isEnabled() ? R.string.done : R.string.edit);
transparentView.setVisibility(button1.isEnabled() ? View.INVISIBLE : View.VISIBLE);
});

Screen press (on mobile) not registering on intended element Unity 3d

I have a grid of buttons (using grid layout group). I created this by adding a UI element and duplicating it 9 times (10 buttons) and then just applying a grid layout group to the parent object while all of the buttons were still at 0,0,0. This laid out the buttons exactly as i wanted in the grid shape that i chose. When i run this on PC and click a button, it registers perfectly. When i build it down to an apk and put it on my android, the buttons don't click when you press on them, but rather when you press way off to the right in some empty field space. But the "clickable grid area" as i'll call it, is perfectly transposed into that open field space. So i have my 5x2 visible button elements on the screen. If i touch them, nothing happens. Randomly touch in empty space until one of the buttons activates, and the clickable grid is in the exact shape of the visible elements except not on top of the elements. I'm not 100% familiar with resolution settings yet but i don't think that has all too much to do with this. What am i missing and what settings can i change?
so the problem ended up being that the anchors were not set to center, they were set to top left. This was set automatically by the grid layout group component. Remove grid layout, set positions manually, all is well.
You need to define column and row for each button

Create a custom compund buttons with divided circle shape

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)

Drag and Drop feature to copy one buttons text to another buttons text

I want to make an app which will copy the text of one button to the text of another button when a button is dragged and dropped on other button. I am totally novice, I have Googled but not found anything easy to understand.
You should add onDragListener
to views. And use startDrag method on views. You can find more information in Drag and Drop | Android Developers.
Basically you attach a drag listener to view which you can set reponses up to action types. Drag listener is triggered from views you started dragging by startDrag(...) referenced above. You can find a detailed example in this link. Once you read & understand it, all you need to do is copying your button's text to other one in ACTION_DROP.
Set your onDragListener to all your buttons & in onLongClick() ( I prefer to start drag on long click rather than on click, when to start drag is totally up to your liking ) use startDrag(...). Button you are dragging will trigger onDragListener(...) which you attached to buttons.
I recommend you to read above example carefully, onDragListener(...) sounds like It works when you drag view that you attached It to, but It's actually works when views that you startDrag(...) enters bounds of view you attached It to.

Android Layout: How to get a particular ancestor of a view

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.

Categories

Resources