ActionBarSherlock: Collapsible item crash on orientation change ICS - android

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"/>

Related

Dismissing Android Spinner with ArrayAdapter loses item padding

I have an issue where if I'm using a basic ArrayAdapter to display items in a spinner, all the spacing on those items is lost if I open the spinner, dismiss it by tapping outside of it, then open it again. It appears other attributes like color and such are still kept intact.
Here is how I am creating the spinner:
binding.fragmentTypeSpinner.setAdapter(new ArrayAdapter<>(mContext,
R.layout.item_simple_spinner,
typeArray));
item_simple_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_simple_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorTransparent"
android:textSize="17sp"
android:padding="10dp" />
How it looks at first:
How it looks after:
I tried with just the basic android spinner item layout as well and still no dice. Has this happened to anyone else before? I searched but couldn't come up with much.

AutoCompleteTextView - Crash When Rotating Device While Popup Window is Visible

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.

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.

No Android status bar with relativeLayout

I am developing an app with Android Studio 2.2.3 and I'm facing a problem I did not find before: if I add an Activity from a template such as Basic Activity, the newly created activity will let the device status bar display
If I add instead an activity from the Empty Activity template, no status bar shows up any more.
Reading through the layout files, I see that the Basic activity uses a android.support.design.widget.CoordinatorLayout whereas the Empty Activity uses a RelativeLayout. I cannot find any other differences, neither in the manifest. I programmed some apps with Eclipse some time ago and the status bar was there, even with the RelativeLayout, as far as I remember.
Any suggestion?
Thanks in advance, regards
Edit:
Status bar does not relate with layout. It is about the theme defined in the styles.xml. Can you check which theme is applied your activity?
Thanks steve for your suggestion: the behavior is due to
Yet another question: keeping this no-actionbar theme, why is my textbox not aligned with the top of the screen, like this:
My activity layout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.pgsoftware.firebasepublisher.Main4Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test string"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"/>
</RelativeLayout>
and with the Layout inspector I get the following UI structure:
The LinearLayout at the top has a 72dp top-padding which I can't find digging into the themes: any suggestion?
Thanks in advance, regards
Please check the android:appTheme="#style/AppTheme" option in the manifest file . Check the theme option in res>style.xml file . Copy the stylesheet and paste in comment to see.

Id tag with red underline

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mylistview"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
I've this layout in my project and AndroidStudio underlines in red the part (#+id/mylistview) but everything works fine,
I can inflate the XML and also findViewById(R.id.mylistview);
Can't figure out the problem, by placing the mouse over I get this message
"Top level element is not completed".
By changing ListView with ExpandableListView the error disappear.
There is a bug report at the link below that reports a very similar problem. Perhaps it's an Android Studio bug? Does it give the same error in Eclipse? If not, probably it's the same bug reported below.
https://code.google.com/p/android/issues/detail?id=55633

Categories

Resources