This is a problem I've been experiencing for quite some time and have already reached out about — other Android programmers found it intriguing.
Context: I have a custom EditText view that relies on XML for instantiation. I have two methods to create this view: either it is within the Layout file of the current Activity (1) or I dynamically add it to the layout at runtime using an Inflater (2). With both methods, the object is defined exactly the same in XML. However, there is a behavior with one that doesn't show in the other.
Behaviors: When using the first method, I get the correct and expected behavior, which is the following: The user types in the center of the EditText (restricted to one horizontal line). Once the user has typed enough to exceed the bounds of the EditText, the EditText will wrap_content to accommodate. When it changes width, the change is anchored to the middle of the view. Length is thus added to both the right AND the left of the box. When using the second method, I get the incorrect behavior: the width change is anchored to the left! Length is added to the right only.
Issue: I'd like to see the correct behavior with the second method and understand why the behavior could be different even though the XML is the same.
What I've Done: I've already reached out for help on the Android Studio Discord and have tried to tweak different aspects of the shared XML. The last thing I've done is to verify this problem still occurs with regular EditText and that my custom class isn't to blame.
Code Snippets: the shared XML (in activity layout OR "tag.xml")
<com.lab.guy.opener.TagView
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/TagStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent" />
And "TagStyle":
<style name="TagStyle">
<item name="android:textSize">14sp</item>
<item name="android:layout_height">21sp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:maxLines">1</item>
<item name="android:lines">1</item>
<item name="android:singleLine">true</item>
<item name="android:maxLength">20</item>
<item name="android:inputType">textNoSuggestions|textVisiblePassword</item>
<item name="android:imeOptions">actionDone</item>
<item name="android:hint">@string/tag</item>
<item name="android:textColor">@color/tag</item>
<item name="android:textCursorDrawable">@drawable/tag_cursor</item>
<item name="android:background">@drawable/tag_background</item>
</style>
The main layout is a ConstraintLayout.
If you need any other resources for testing, please do tell!
The only code I'm not willing to share is from TagView.java, since the problem occurs with standard EditTexts too.
Thank you very much from reading this far!
Edit: I've tried out more things to do, such as cross-testing on other devices and trying out other layout types (under Linear or Relative, I get the incorrect behavior regardless of which method I use). Currently, I'm trying to recreate the correct behavior on a default EditText that uses the second instantiation method (this time, trying out different XML).