ClearFocus sets focus on the first view in LinearLayout - android

When clearFocus() on a LinearLayout is called, it sets focus on the first view in the Linearlayout. I am reading the android View document and it says
Note: When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.
enter link description here
So, I checked IsInTouchMode on the LinearLayout before calling clearFocus, but IsInTouchMode is true.
I am wondering what is affecting this behaviour.

Use android:descendantFocusability="beforeDescendants" so that focus will be first on parent layout and then on their child/s.
Like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" />
Let me know if it worked or not.

Related

Android Searchview gets refocused when returning from another fragment

Currently using jetpack navigation and want to use a single activity. However, the searchview keeps regaining focus whenever I return from the card details back to the search, even though I have removed focus after clicking search. See here.
What I'm currently doing to remove focus is I have a dummy view above my toolbar
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/database_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
style="#style/Widget.MaterialComponents.Toolbar.Primary" />
<!-- Dummy view for receiving focus after SearchView clears focus -->
<View
android:id="#+id/focus_dummy_view"
android:focusableInTouchMode="true"
android:focusable="true"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintBottom_toTopOf="#id/database_toolbar" />
</com.google.android.material.appbar.MaterialToolbar>
and after I perform a search, I do
searchView.clearFocus()
binding.focusDummyView.requestFocus()
My current workaround is to host the details in an activity, but I'd rather not. I made a new project and still see this issue happening.
Some additional context is that this only occurred after updating to "androidx.fragment:fragment-ktx:1.4.0" from 1.2.5. I raised this issue in the bug tracker but they have not responded to me yet. Wondering if anybody knows a fix?
Why don't you try
In Kotlin
override fun onResume(){
super.onResume()
// Only runs if there is a view that is currently focused
searchView.currentFocus?.let { view ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
searchView.clearFocus()
}
}
You have to add this two lines of code in your root layout xml.
To Clear focus from a View you can call getCurrentFocus().clearFocus() But remember that this will Simply move focus from current view to the first element in your view, not Completely removing focus from views; So if the first element in your layout is a EditText it's going to gain focus again.
A way to make sure your view doesn't get focus again is to add this attributes to your root view in your xml, enabling root view to capture focus.
To Clear focus from a View you can call getCurrentFocus().clearFocus() But remember that this will Simply move focus from current view to the first element in your view, not Completely removing focus from views; So if the first element in your layout is a EditText it's going to gain focus again.
A way to make sure your view doesn't get focus again is to add this attributes to your root view in your xml, enabling root view to capture focus
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
After that you need to clear focus from onResume()
I think it will help.

How to focus in a RecyclerView with TalkBack (TTS - Acessibility)

I have the following xml (some attributes were omitted for simplicity):
<FrameLayout>
<RecyclerView
android:id="#+id/list"
android:focusable="true"
android:contentDescription="Some content"/>
</FrameLayout>
When I activate TalkBack (TTS) to test accessibility in my app, I want to be able to click in the RecyclerView (in any item on it) so the whole RecyclerView gets focused and its content description gets read.
I've tryed:
removed any android:focusable="true" from the items in the list, alsto there is no TextView in them, so TTS cannot read them.
added android:focusable="true" to the RecyclerView. Even by making the view larger than its children and being able to click directly on it, it was not receiving any focus at all.
was able to "focus" on the RecyclerView by wrapping it with a FrameLayout and adding android:focusable="true" to it, so the FrameLayout was being focused giving the impression the whole list was focused (but this is more of a workaround).

How to click a view that is behind a scrollview

I have to an EditText behind a ScrollView
I cannot click the EditText anymore. The ScrollView is intercepting it I think. I tried the code below but it doesn't fix it.
Any suggestions?
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:layout_alignParentTop="true" >
Things to try:
The parent view can influence the position of the views. For example
in a FrameLayout all of the views get stacked in the order they are
placed in the layout file (so your issue could be fixed by
rearranging the order of the EditText in relation to the
ScrollView).
You might try to call editText.bringToFront() to force the
EditText in front of the ScrollView.
If you disable the touch-based listeners (OnClickListeners, etc) on
the ScrollView or the container it's in then the action should "pass
through" the ScrollView and allow you to interact with the EditText
behind it. I found this out the hard way when I was trying to make
the opposite scenario work for me (I wanted the top level view to
consume the actions and not fall through to the view behind it).
These solutions worked for me in the past in different scenarios.
In my case I replaced android:layout_height="match_parent" to android:layout_height="wrap_content" and views inside ScrollView become clickable. Hope this helps.
My code:
v<ScrollView
android:layout_width="match_parent"
android:layout_height="WRAP_CONTENT"
android:id="#+id/svTabActions">
<View>...</View>
</ScrollView>
Try placing the editText inside the scrollView.

Android: Prevent focus from jumping when hiding view

I have an Android activity that contains multiple EditText views.
If I programmatically hide the currently focused EditText, then the focus will automatically jump to the next EditText.
Is there anyway to disable this behavior so that the focus is cleared when the EditText is hidden rather than automatically jumping to the next EditText?
Thanks.
You need to provide Focus to a parent view above your EditText just like the sample code below
<RelativeLayout
android:id="#+id/search_edit_text_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText...
Do try this to avoid providing focus directly to the next EditText

How to disable any event on a View in Android?

My question is simple: How to disable any event on a View in Android? (including removing its focussability, like I just want it to be there visually but be inexistant on everything else)
And does it work on a whole view tree? (like if I disable events on the root, all the events will be disabled for its children?).
Now, before you say anything I have tried all the following:
setEnabled
setFocusable
setSelected
setClickable
setActivated
And none of these methods appear to work, seriously.
I have tried them directly on a WebView, as well as on the parent layout on everything but I am still able to interact with it.
Any idea?
Thanks!
EDIT#1
The solution that consists in adding a view on top of the view that needs to be disabled doesn't work. Actually, it's still possible to click on the inner view, I have tried with a simple example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff0000">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Click Me!"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
/>
</FrameLayout>
Here it's still possible to click on the button.
EDIT#2
The reason why I want to do this is related to the following question that I asked weeks ago.
What I have is a ListViewacting as a navigation bar which is underneath a View that holds the content of my app. The problem with this implementation is that when I try to scroll through the ListView when there is a focusable view in the layer on top of it, well the ListView doesn't scroll and instead it's the top view that takes focus (That's the case when there is a
Webview or an EditText etc.).
So yes as mentioned in one of the answers, I can disable any click events on a WebView by overriding setOnTouchListener but the view remains focussed and I think this is the reason why I am still having the same issue with my navigation bar.
Simply put a view on top of your view. You can toggle it on off by setting view.visibility = gone/visible.
<FrameLayout>
<WebView/>
<FrameLayout This view will be on top/>
</FrameLayout>
Edit: Just stumpled upon this link: https://stackoverflow.com/a/3856199/969325
Basically disables all touch event for the webview. Tryed that?
Edit 2 reedit: Try to set the visibility to gone for the the top view below your listview.

Categories

Resources