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
Related
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.
I have a UX problem with my layout. I want that is possible that the user can scroll over the compleat layout without a focus change when the keyboard popes up.
This is what the Layout looks like when the activity starts
This is what happens when the user clicks to the Big Text EditText
This is what I want the layout to look like, the positions should stand in the same way as before, but it should be possible to scroll to the bottom with the keyboard open
The rootView of my layout is
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
I also try to add android:windowSoftInputMode="adjustResize" and android:windowSoftInputMode="adjustPan" the the activity in the Manifest.xml but it does not solve my problem.
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.
I have a an EditText with a drawable background (A rectangle shape with corners and a color, nothing fancy)
Case 1 When the EditText gets focus on click, view shifts above but the soft keyboard still partially overlaps it so that the hint is visible properly but the background is trimmed.
Case 2 (Inside a fragment, coz this issue does not occur in an Activity) If I press next from an edit text above the target EditText the background is again trimmed and the keyboard is just touching the bottom of the hint.
I have enclosed it inside a ScrollView, tried android:windowSoftInputMode="stateAlwaysHidden|adjustResize" (with different combinations too) but nothing seems to work.
Is there a way to override the y-axis by which the view is pushed when soft keyboard opens?
Half of the job is done by Scrollview itself,
Try to writing these line in you onFocusChanged method of edittext
editText.requestLayout();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
and your .xml would be like -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="1">
.... your editetexts here...
</ScrollView>
</LinearLayout>
I have a LinearLayout composed by a ListView(which loads from an AsynTask) and an EditText(so I can filter that list). Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Filtrar por nombre..." >
</EditText>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The problem is that when the activity starts, the keyboard is shown up and I only want to show it when the EditText is clicked for a search.
I've tried several methods such as writing the following in the manifest
android:windowSoftInputMode="stateHidden"
and including this in the class
InputMethodManager iMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The only thing I've achieved is to not show the keyboard but not showing the loaded stuff of the list(just the EditText and the rest of the screen plain black), and once I click on the EditText, they appear both keyboard and the stuff of the listView.
Any posible solution for this?
EDIT:
I've tried the following solutions(thanks to #Sush, #boztalay and #Agata Sworowska), but none did the trick.
-Placed in my layout:
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
-And placed in my onCreate() method:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
Any other posibility?
NEW EDIT:
I've noticed that when it doesn´t focus on the EditText, the listView doesn´t load until I focus back the EditText, llike if it was waiting for it. I've tried to set up the EditText after the code that loads the ListView but the problem persists.
This sounds like an issue with focus. What's happening is when the app is started, the EditText gets focus, and the keyboard pops up. You should try to make it so that the ListView gets focus. This way, the keyboard won't pop up until the user gives the EditText focus when they click on it.
In your Java, you should get an instance of the ListView, then call a couple of functions:
ListView listView = findViewById(R.id.list);
listView.setFocusableInTouchMode(true);
listView.requestFocus();
If you put that in your onCreate(), focus will be given to the ListView, and they keyboard won't come up until the user clicks the EditText.
See this SO question for more details.
You can just add below two attributes to your LinearLayout.
android:focusable="true"
android:focusableInTouchMode="true"
Try adding in Your layout, that contains EditText this:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
I hope it helps.
http://www.mysamplecode.com/2012/02/android-edittext-hide-soft-keyboard.html
This is a solution I found for the issue here. Do not understand why listivew should do this, but the workaround helps.