My layout contains ListView
, SurfaceView
and EditText
. When I click on the EditText
, it receives focus and the on-screen keyboard pops up. When I click somewhere outside of the EditText
, it still has the focus (it shouldn't).
I guess I could set up OnTouchListener
's on the other views in layout and manually clear the EditText
's focus. But seems too hackish...
I also have the same situation in the other layout - list view with different types of items, some of which have EditText
's inside. They act just like I wrote above.
The task is to make EditText
lose focus when user touches something outside of it.
I've seen similar questions here, but haven't found any solution...
I really think it's a more robust way to use
getLocationOnScreen
thangetGlobalVisibleRect
. Because I meet a problem. There is a Listview which contain some Edittext and and setajustpan
in the activity. I findgetGlobalVisibleRect
return a value that looks like including the scrollY in it, but the event.getRawY is always by the screen. The below code works well.For the EditText's parent view, let the following 3 attributes be "true":
clickable, focusable, focusableInTouchMode.
If a view want to receive focus, it must satisfy these 3 conditions.
See android.view :
Hope it helps.
I have a
ListView
comprised ofEditText
views. The scenario says that after editing text in one or more row(s) we should click on a button called "finish". I usedonFocusChanged
on theEditText
view inside oflistView
but after clicking on finish the data is not being saved. The problem was solved by addinginside the
onClickListener
for the "finish" button and the data was saved successfully.For Me Below things Worked -
1.Adding
android:clickable="true"
andandroid:focusableInTouchMode="true"
to theparentLayout
ofEditText
i.eandroid.support.design.widget.TextInputLayout
2.Overriding
dispatchTouchEvent
in Activity class and insertinghideKeyboard()
function3.Adding
setOnFocusChangeListener
for EditTextTo lose the focus when other view is touched , both views should be set as view.focusableInTouchMode(true).
But it seems that use focuses in touch mode are not recommended. Please take a look here: http://android-developers.blogspot.com/2008/12/touch-mode.html
Simply define two properties of parent of that
EditText
as :So when user will touch outside of
EditText
area, focus will be removed because focus will be transferred to parent view.