How can I detect if someone clicks outside of a specific LinearLayout - android

I've got a LinearLayout with a bunch of buttons on it. This panel is always hidden unless a menu item is chosen.
When the LinearLayout is shown, I want to detect if the user clicks anywhere outside of the panel so that I can hide it again. Is there a way to do this, maybe with detecting focus changes?
I've tried to add an OnFocusChangeListener to the LinearLayout itself (and called setFocusable(true) on it), but the focus change listener never gets called. Furthermore, I'd have to be able to detect if the LinearLayout or any of its children lose focus.

Try this:
Step #1: Have the LinearLayout watch for touch events, and consume them
Step #2: Have the thing under the LinearLayout watch for touch events -- if it gets one, you know it wasn't from the LinearLayout, so you dismiss the LinearLayout and consume that event, then perhaps unregister for touch events (or just only pay attention to them if the LinearLayout is on-screen)
Step #3: There is no step #3...I think

Related

How to add a Layout

Hello I want to add a LAyout to my Activity.
I have a FrameLayout where I add a SurfaceView so far.
Now I want to add another Layout as a menu over it so when I click a Button on my Surface View I set it to be Visible.
This layout should contain one Textview and should be scrollable
1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Strings dynamically
2: How can I achieve this in Code?
It should be:
Scrollable.
Should be a certain Size: Half of the screen width and half of the screenHeight
So it also should be at the position screenWidth/4 and screenHeight/4
To achieve this I can set these parameters inside my mainThread that is attached to the surfaceview so I have the parameters needed allready in Pixel format that's no Problem.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
So it should go like this:
User taps the button to display the menu
Now the User can scroll through the Menu
When he presses the Backbutton or outside of the View It should close itself
When there is a new Text that should be displayed it should be attached to the layout to be ready to be displayed at the next time the user clicks the button again
Thank you
1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Textviews dynamically
That sounds good to me, you can go with either ListView or RecyclerView. You are basically not adding TextViews dynamically, you create an adapter that will do handle creating and recycling of views for you.
2: How can I achieve this in Code?
Create a DialogFragment, it's the best candidate for such dialogs with logic. Its lifecycle is handled via FragmentManager, so you won't have issues on screen rotations and so on. It allows you setting whatever layout you need just like any other Fragment. It will be placed in the center for you, so you don't have to handle that manually. Just set the size of the dialog you want and it will look perfect.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
I don't really get this one. When user touches outside, do you need to lose the focus but the dialog should not be closed? If yes, then this is already answered here:
Allow outside touch for DialogFragment
If the dialog should be closed, you don't have to implement anything, it works like that by default.

onTouchListener with a new dialog

I have an app that inside onItemLongClick opens a dialog. I want the user to have a better experience so I want the possibility to choose and item in the dialog without having to raise his finger but I can't understand how to make a onTouch listener that will get the MOVE MotionEvent. can anyone tell me how to do this?
Thanks,
Benny.
Any onTouchListener will get move events, unless the moves are stolen from it. Given you're in an onItemLongClick, are you part of a ListView? If so, you'll get ACTION_CANCEL whenever the listview decides to take the touch events away from its children to scroll with, and you won't receive further moves (or even an up).

process touch events across multiple scrollviews

I have two scrollviews side by side, I want the user to be able to drag list items back and forth from left to right scrollviews. However, I can't find a way to handle the touch events. I can't set a touch listener for each scrollview seperately as the drag gesture gets dropped when passing from one to another. I tried creating an absolute layout over the top of both, which works from the drag and drop perspective, but it stops me from being able to scroll the scrollviews. Is there a simple solution to this? can anyone help me out?
Generally, onTouchListener returns a boolean that indicates whether the touch has been handled. It's up to you to decide whether the touch was handled or not. When the user touches a View, Android will call it's touch listener. If the touch listener returns true, then it regards the touch as handled then moves on. If the touch listener returns false, then it will go up one to the parent view (in this case whatever your ScrollView is). Then the parent view's touch listener is called and must decide how to handle the touch. It will keep cascading up the parent views until a true is returned or until it reaches the end.
In your case, you may have to decide what the user has to do in order to drag & drop vs. scrolling. Perhaps the user must do a long press on an item before he/she can drag it or something.

Prevent onFocusChanged firing when ScrollView is scrolled

I have a problem where I have several TextView objects in my Activity. More than can fit on the screen view so I have placed them inside a ScrollView.
Now each TextView item is placed into a RelativeLayout before placing onto the ScrollView.
Each RelativeLayout object then has onClickListener and onFocusChangedListeners assigned.
Each Listener implements a method that pops up a dialog to enable the user to edit the value stored by the TextView.
The problem that is occuring is that when the screen is scrolled the onFocusChangedListener event is fired causing the dialog to appear for several items at a time.
Is it possible to detect if the Scrollview is scrolling and prevent the onFocusedChanged event firing?
I can post code if required but its quite large as the items added to the page are done so dynamically. This also means there is no xml for the layout.
I looked at using the ScrollView onTouchListener to add a flag ignoreFocusChange but it appears this would not work as the event was hit sveral times for one swipe/fling on the screen.
Please help
Regards,
Iain
After spending some time on this I found that the best solution for me was to remove the onFocusChangedListener and replace it with onTouchListener

FrameLayout buttons overlap, and can't be 'touched. - what's right way to access?

I'm using a FrameLayout and have two buttons on it, button A and button B (in addition to some other widgets). A is directly on top of B, overlapping completely.
In onCreate, I do a check and if A is not needed, I set it to invisible. Later in the app it may become visible again so I don't want to remove it completely.
When button B is pressed, it doesn't respond. I think button A is stopping the press even though it's invisible. Other buttons do respond so this is why I think this.
Is there a common way to make it so B accepts touches? Do I have to remove A? I don't really want to remove A as I have a relative layout in the frame layout and other controls depend on A for positioning.
Personally I think it would make a lot more sense to have a single button who's properties vary based on configuration. However, if you really want to have two, you could try a couple of things:
Set the invisible button to not be focusable or touchable.
Set the button to Gone, not invisible. This might mess up the layout though, since this basically says not to consider the button when doing the layout.

Categories

Resources