AutoCompleteTextView - Crash When Rotating Device While Popup Window is Visible - android

I am experiencing a crash whenever I rotate a device while the AutoCompleteTextView dropdown is displayed.
I can recreate this every time by:
Tap the AutoCompleteTextView to bring up a dropdown of suggestions
Do not make a selection
Rotate the device
Stacktrace:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/criteria_text_input_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:errorEnabled="true">
<AutoCompleteTextView
android:id="#+id/criteria_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
I have searched for that exception and it appears this can happen when you use getApplicationContext(), but all my adapters use getContext(). I have also tried dismissing the dropdown on configuration changes, but to no avail. Any ideas?

BadTokenException happens when your popup or dialog is showing and activity is destroyed. That's why we usually close popups and dialogs in onDestroy(). e.g.
if(dialog.isShowing()) {
dialog.dismiss();
}
So Add below line in your onDestroy() for AutoCompleteTextView.
yourAutoCompleteTextView.dismissDropDown();

Turns out this is a problem with Samsung devices (which is what I was using to test). I could not find out exactly where the problem was in the code, but I did notice an error in the logs:
sendUserActionEvent() mView == null
So I did another search and found that it's a known bug on some Android devices where mView can be null so one of the suggested solutions was to add the following to the manifest:
android:configChanges="orientation|screenSize"
This seems to have fixed my issue.
EDIT:
Using this introduces another problem as rotating the device no longer allows me to restore state properly. So I am now back at square one.
EDIT #2:
Seems this problem happens during restoring of state of the TextInputLayout and AutoCompleteTextView. Another solution is to disable saving of state by adding the following to the view(s):
android:saveEnabled="false"
By adding this and removing the configChanges above allows me to restore state as I was expecting.

Related

Drag/Drop components are not visible in preview in android studio

I am a beginner in android development. I have created a android project. But in the preview section, I am not able to see the components. But components are visible when i run the same app on emulator i am able to see the components. I am missing some setting here? XML is given below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've had have also this problem and solved it by only selecting another "Theme" cause
the default theme appTheme doesn't show added items.
If you i.e. select Transulcent, any item becomes visible ;-)
cheers
Oliwan
Maybe sound a weird answer, but self-tested this.
Change the XML file's (maybe activity_x.xml) layout's root tag android.support.constraint.ConstraintLayout to LinearLayout, then preview will start working again (as happened in my case). [If not, then perform force refresh (R)]
Then switch back to old one, I mean: Change the XML file's (maybe activity_x.xml) layout's root tag LinearLayout to android.support.constraint.ConstraintLayout. And force a refresh.
This does seem to be a bug in Android Studio. When it showed up for me, I fixed it by making a trivial change in the app build.gradle (change version code), do a gradle sync, then change back and sync again.
After that, the design view worked again.

Popup of AutoCompleteTextView is showing behind keyboard

I have an AutoCompleteTextView in my app, at the bottom of the layout.
When user inputs data, suggestion popup with items should appear.
All works as expected on Android Samsung device with OS 6.0.1:
But for Android 8.0.0 (LG device and also an emulator 8.1.0), I don't see such popup, I suppose it showing behind keyboard for some reason (Because when I clicked back button nothing happened - popup handled that event, and only on second back click keyboard disappeared):
My AutocompleteTextview:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout ...
<android.support.v7.widget.AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#drawable/thin_square_border"
android:maxLines="3"
android:minHeight="60sp"
android:padding="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:textColor="#color/charcoalgray"
/>
</LinearLayout>
</ScrollView>
I've tried dropDownAnchor on view above, and android:dropDownHeight="wrap_content" but that didn't help.
I found this solution that works for me in Oreo and Pie.
I added this to my fragment:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
This seems like a bug introduced in Oreo. Hope this helps for your case.
you have to set android:dropDownAnchor="#id/container_comment" in your autocomplete widget

Stop EditText from bringing up the keyboard on Activity start?

I have an Edit Text in one of my App layouts, and I want this EditText to only open the keyboard (I believe this is called being focused on?) when it is actually touched.
As of now, the keyboard opens with the EditText whenever the app opens, which isn't what I want.
I have tried many different XML tags to fix this:
android:focusable="false" <--- Prevents keyboard from opening at all.
android:focusable="true"
android:focusableInTouchMode = "true" <--- These tags give me the same result as no tags (keybaord will open on activity start)
android:focusedByDefault = "true" <--- Only available in API >= 23
What I am asking is, why is it so hard to disable default focus on an EditText? Surely I am missing an easy way to do this.
EDIT: Adding this line to my AndroidManifest fixed the issue:
android:windowSoftInputMode="stateHidden"
However, I don't like this solution. It seems like since this is in the Manifest, it will affect more UI elements than the single EditText I need to change.
Alternatively you can set the focus to the root layout element:
android:focusable="true"
android:focusableInTouchMode="true"
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Use android:windowSoftInputMode="stateHidden"
If you dig deep into the Theme you are using for your Activity, you will find that the default value of windowSoftInputMode is stateUnspecified|adjustPan. And from the documentation:
stateUnspecified: Not specified, use what the system thinks is best. This is the default.
So depending on the the android device you run, your results will vary. I tried reproducing your case in API-26 emulator and the keyboard doesn't show up.You can use stateHidden to ensure that when an activity starts, the soft keyboard doesn't show up when the EditText gets focused on itself.
The other way to solve this is to requestFocus to some other element in the UI, making sure the EditText is not the first UI element to get focused. In my experience this is kind of a hack and it messes up the accessibility. The safest and clean way to accomplish is actually to use stateHidden.
stateHidden: Make the soft input area hidden when normally appropriate (when the user is navigating forward to your window).
Note that this will not affect any other UI elements. You can use adjustPan also to this, based on the screen background.

Disable SoftKeyboard while keeping Cursor

I have an EditText and a custom keyboard, so i want to avoid softkeyboard to popping up. It must never pop up, but i need also to leave EditText focusable to use cursor. Simple as it is but so hard to accomplish.
I already tried a lot of solutions but they aren't working and since they are outdated i'm asking now for an actual working way. Solutions targetted as API 11 are such useless now since they don't work, already tried. I'd love to listen for suggestions from people who already tried the method on recent versions, such as Lollipop (API 21-22) or Marshmallow (23). I can't believe that there is no workaround, and i'm getting mad
This will help you
editText.setInputType(InputType.TYPE_NULL);
In your layout file add these line
android:descendantFocusability="beforeDescendants" and
android:focusableInTouchMode="true"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</EditText>
</RelativeLayout>
In your Activity class add this line
this.getWindow().
setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

ActionBarSherlock: Collapsible item crash on orientation change ICS

I have a collapsible menu item that is defined in XML like this:
<item
android:id="#+id/searchMenu"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="withText|always|collapseActionView"
android:actionLayout="#layout/collapsible_edittext"/>
Here's the collapsible_edittext.xml file:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text"
android:hint="#string/search"/>
When the item is expanded, and you change the orientation of the phone, it crashes giving this error in logcat:
07-24 08:59:19.353: E/AndroidRuntime(1284): Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.TextView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/searchMenu. Make sure other views do not use the same id.
If I remove the android:id="#+id/searchMenu", everything works fine but I need the item id in my code. This error only happens on ICS (It even works in the jelly bean emulator) and I'm using ActionBarSherlock 4.1.0
I finally found the problem, in fact it was not even linked with ActionBarSherlock (I removed action bar sherlock from my project and it was still crashing).
I only needed to add an id to my EditText and that's it.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/collapsibleEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text"
android:hint="#string/search"/>

Categories

Resources