Skip to content

[TextInputLayout] Add baseline alignment support #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

[TextInputLayout] Add baseline alignment support #56

wants to merge 5 commits into from

Conversation

SUPERCILEX
Copy link
Contributor

Fixes #14, fixes #13

Simpler version of those above issues.

@SUPERCILEX
Copy link
Contributor Author

@dsn5ft Mind taking a look at this one?

@SUPERCILEX SUPERCILEX mentioned this pull request Jul 9, 2018
@SUPERCILEX
Copy link
Contributor Author

@cketcham bump

@SUPERCILEX
Copy link
Contributor Author

@ymarian @wcshi mind taking a look?

Copy link
Contributor

@afohrman afohrman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!

@@ -795,6 +795,12 @@ public EditText getEditText() {
return editText;
}

@Override
public int getBaseline() {
EditText text = getEditText();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextInputLayout already has an editText field. That field should be used instead.

@Override
public int getBaseline() {
EditText text = getEditText();
return text == null ? super.getBaseline() : text.getPaddingTop() + text.getBaseline();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns statements in the codebase generally follow the opposite pattern for ordering null checks in ternary operations: editText != null ? ____ : ____ ;

@Override
public int getBaseline() {
EditText text = getEditText();
return text == null ? super.getBaseline() : text.getPaddingTop() + text.getBaseline();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified that this works? getPaddingTop() alone won't provide the correct baseline, since TextInputLayout handles top spacing differently based in its boxBackgroundMode. calculateLabelMarginTop() is probably closer to what you want, although I don't think that will be enough to provide the correct baseline, either. You should also be aware that TextInputLayout adds spacing to the bottom of the box background, (boxBottomOffsetPx) which would throw off the calculation unless it's accounted for.

You should be able to get the correct baseline by offsetting the EditText's baseline by calculateLabelMarginTop(), getPaddingTop(), and the bottom offset.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure all that is necessary? We've been using it for several months now in FirebaseUI without any problems. Also, both of these Stack Overflow questions found the same resolution.

If you think it won't work, would you mind explaining what boxBackgroundMode is and how I could activate it?

Example screenshot:
screenshot_20180904-105605

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, all that is necessary. This solution would help with aligning legacy text fields with other legacy text fields, because legacy text fields only add top padding to make room for the label. The text field boxes do other calculations in order to align properly, so these calculations need to be taken into account.

The currently proposed getBaseline() produces this effect when put in a LinearLayout with baselineAligned set to "true":

image

And this is the effect of a getBaseline() method that takes those factors into account:

image

The getBaseline() method in the screenshot above uses the following code:

  @Override
  public int getBaseline() {
    return editText != null
        ? editText.getBaseline() + calculateLabelMarginTop() + getPaddingTop() +
        : super.getBaseline();
  }

boxBottomOffsetPx should only be added if boxBackgroundMode != BOX_BACKGROUND_NONE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, I see. I haven't used the Material theme yet so I didn't notice that. Thanks for the great catch!

@SUPERCILEX
Copy link
Contributor Author

@afohrman Left a comment here.

@SUPERCILEX
Copy link
Contributor Author

@afohrman Addressed all your comments 👍

@@ -798,7 +798,7 @@ public EditText getEditText() {
@Override
public int getBaseline() {
if (editText != null) {
return editText.getPaddingTop() + editText.getBaseline();
return editText.getBaseline() + getPaddingTop() + calculateLabelMarginTop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, but boxBottomOffsetPx still needs to be accounted for. It should be added to the calculation only if boxBackgroundMode != BOX_BACKGROUND_NONE. You may want to split that logic out into a separate method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that's right. Here's a screenshot without the extra padding:

screenshot_20180904-153231

And here is with the extra padding:

screenshot_20180904-153407

BTW, here's my code:

int padding = getPaddingTop();
if (boxBackgroundMode != BOX_BACKGROUND_NONE) {
  padding += boxCollapsedPaddingTopPx;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra offset to account for is boxBottomOffsetPx, not boxCollapsedPaddingTopPx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that will move the legacy widget down. The first screenshot was fine... what is there to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't move the legacy widget down if the offset is only being added when boxBackgroundMode != BOX_BACKGROUND_NONE.

From what I can see in the first screenshot, the legacy text field is a little off. Can you add a screenshot that includes a TextView, EditText, and the TextInputLayout legacy and updated styles side by side?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, this looks great. I'll merge the change.

Thanks for contributing!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the baseline does still appear to still be a little off:

image

Could you look around in TextInputLayout and see what could be causing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aw, dang! Can you share the XML you used so I can repro?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh never mind, that screenshot was using an earlier version of the code. I've verified that it works and I'll merge the changes.

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phew! 😌 Can't wait to see this finally go through!!! 😁👏🎉

@SUPERCILEX
Copy link
Contributor Author

@afohrman anything holding you back from merging?

@dsn5ft
Copy link
Contributor

dsn5ft commented Sep 11, 2018

@SUPERCILEX this has been submitted internally and will be synced out to GitHub soon.

@SUPERCILEX
Copy link
Contributor Author

@dsn5ft Awesome! ❤️

@SUPERCILEX
Copy link
Contributor Author

@dsn5ft would you mind also closing https://issuetracker.google.com/issues/113439293? Thanks!

@samtstern
Copy link

I think there is some issue with this implementation and using ConstraintLayout's baseline alignment:
firebase/FirebaseUI-Android#1555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request - Add baseline alignment support to TextInputLayout
4 participants