material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[TextInputLayout] Setting end icon gravity

Open davidmak2709 opened this issue 2 years ago • 1 comments

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. Screenshot from 2022-06-25 11-20-13

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:

Screenshot from 2022-06-25 11-33-40

Describe the solution you'd like I think it would be useful to have a TextInputLayout XML attribute to set the end icon gravity.

davidmak2709 avatar Jun 25 '22 09:06 davidmak2709

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.

jsoberg avatar Sep 14 '22 15:09 jsoberg