Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import android.content.Context;
  2. import android.graphics.Rect;
  3. import android.support.design.widget.TextInputLayout;
  4. import android.util.AttributeSet;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8.  
  9. public class CustomTextInputLayout extends TextInputLayout {
  10. private Object collapsingTextHelper;
  11. private Rect bounds;
  12. private Method recalculateMethod;
  13.  
  14. public CustomTextInputLayout(Context context) {
  15. this(context, null);
  16. }
  17.  
  18. public CustomTextInputLayout(Context context, AttributeSet attrs) {
  19. this(context, attrs, 0);
  20. }
  21.  
  22. public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  23. super(context, attrs, defStyleAttr);
  24.  
  25. init();
  26. }
  27.  
  28. @Override
  29. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  30. super.onLayout(changed, left, top, right, bottom);
  31.  
  32. adjustBounds();
  33. }
  34.  
  35. private void init() {
  36. try {
  37. Field cthField = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
  38. cthField.setAccessible(true);
  39. collapsingTextHelper = cthField.get(this);
  40.  
  41. Field boundsField = collapsingTextHelper.getClass().getDeclaredField("mCollapsedBounds");
  42. boundsField.setAccessible(true);
  43. bounds = (Rect) boundsField.get(collapsingTextHelper);
  44.  
  45. recalculateMethod = collapsingTextHelper.getClass().getDeclaredMethod("recalculate");
  46. }
  47. catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
  48. collapsingTextHelper = null;
  49. bounds = null;
  50. recalculateMethod = null;
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. private void adjustBounds() {
  56. if (collapsingTextHelper == null) {
  57. return;
  58. }
  59.  
  60. try {
  61. bounds.left = getEditText().getLeft() + getEditText().getPaddingLeft();
  62. recalculateMethod.invoke(collapsingTextHelper);
  63. }
  64. catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69.  
  70. <com.mycompany.myapp.CustomTextInputLayout
  71. android:id="@+id/text_input_layout"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. android:hint="Model (Example i10, Swift, etc.)"
  75. app:hintTextAppearance="@style/TextLabel">
  76.  
  77. <android.support.design.widget.TextInputEditText
  78. android:id="@+id/edit_text"
  79. android:layout_width="match_parent"
  80. android:layout_height="wrap_content"
  81. android:drawableLeft="@drawable/bmw"
  82. android:text="M Series" />
  83.  
  84. </com.mycompany.myapp.CustomTextInputLayout>
  85.  
  86. editText.setCompoundDrawablePadding(your padding value);
Add Comment
Please, Sign In to add comment