material-components-android
material-components-android copied to clipboard
[TextInputLayout] Setting end icon gravity
Is your feature request related to a problem? Please describe.
In a multi line TextInputEditText the end icon is always centered, but in some cases we need it to be set to top or at the bottom of the layout.
After some internet research I didn't find any suitable solution. I tried to explore the implementation class TextInputLayout.java and found out that the end icon is place as a drawable in CheckableImageButton (com.google.android.material.R.id.text_input_end_icon) and the button is place in a FrameLayout.
What I did is finding the button by its ID and then customize the layout params.
// using viewbinding
private ActivityCommentBinding mBinding;
// change the end icon gravity
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mBinding.commentTextLayout
.findViewById(com.google.android.material.R.id.text_input_end_icon)
.getLayoutParams();
params.gravity = Gravity.TOP;
mBinding.commentTextLayout
.findViewById(com.google.android.material.R.id.text_input_end_icon)
.setLayoutParams(params);
Result:
Describe the solution you'd like I think it would be useful to have a TextInputLayout XML attribute to set the end icon gravity.
An additional concern here comes with having a dynamically sized layout that resizes when the # of lines of text entered changes. Using TOP
or BOTTOM
gravity as @davidmak2709 did above works, but the icon will be off-centered when the TextInputLayout
is only one line high at the time.