Adding drawableLeft to EditText shifts the tooltip to the right if the edittext is inside a TextInputlayout

enter image description here I created an EditText inside a TextInputLayout. I set drawableLeft to EditText at runtime in my code, but as soon as I add drawableLeft, the floating tooltip inside the TextInputLayout shifts to the right, leaving the space equal to the width. But I do not want this place in the tooltip, so help me solve this problem.

+6
source share
3 answers

TextInputLayout uses a helper class - CollapsingTextHelper - to control tooltip text. The instance of this helper is private, and none of the attributes associated with its layout are displayed, so we will need to think a bit to get access to it. In addition, its properties are set and recalculated every time TextInputLayout laid out, so it makes sense to subclass TextInputLayout , redefine its onLayout() method and make its own settings there.

 import android.content.Context; import android.graphics.Rect; import android.support.design.widget.TextInputLayout; import android.util.AttributeSet; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class CustomTextInputLayout extends TextInputLayout { private Object collapsingTextHelper; private Rect bounds; private Method recalculateMethod; public CustomTextInputLayout(Context context) { this(context, null); } public CustomTextInputLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); adjustBounds(); } private void init() { try { Field cthField = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper"); cthField.setAccessible(true); collapsingTextHelper = cthField.get(this); Field boundsField = collapsingTextHelper.getClass().getDeclaredField("mCollapsedBounds"); boundsField.setAccessible(true); bounds = (Rect) boundsField.get(collapsingTextHelper); recalculateMethod = collapsingTextHelper.getClass().getDeclaredMethod("recalculate"); } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { collapsingTextHelper = null; bounds = null; recalculateMethod = null; e.printStackTrace(); } } private void adjustBounds() { if (collapsingTextHelper == null) { return; } try { bounds.left = getEditText().getLeft() + getEditText().getPaddingLeft(); recalculateMethod.invoke(collapsingTextHelper); } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); } } } 

This custom class is a replacement for the regular TextInputLayout , and you will use it in the same way. For instance:

 <com.mycompany.myapp.CustomTextInputLayout android:id="@+id/text_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Model (Example i10, Swift, etc.)" app:hintTextAppearance="@style/TextLabel"> <android.support.design.widget.TextInputEditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/bmw" android:text="M Series" /> </com.mycompany.myapp.CustomTextInputLayout> 

screenshot

+19
source

Yes, this is a common problem that I have recently encountered. I solved it with a simple one line code: drawbleleft between the tooltip and drawbleleft with drawble padding . If you add drawble at runtime, just add drawblepadding to xml before hand or you can add pending spaces dynamically.

-one
source
  editText.setCompoundDrawablePadding(your padding value); 

Give it a try and let me know. It worked for me.

-one
source

All Articles