Skip to content
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

Vertical alignment of text not consistent #16

Open
vpratfr opened this issue Oct 22, 2014 · 11 comments
Open

Vertical alignment of text not consistent #16

vpratfr opened this issue Oct 22, 2014 · 11 comments

Comments

@vpratfr
Copy link
Member

vpratfr commented Oct 22, 2014

It seems that the text in not positionned identically vertically in the various floating label widgets :

EditText vs Pickers for example

@AOrobator
Copy link

Could you find the total height of the hint text and the height of the view, then place the top of the hint half of its height above the middle of the view?

@vpratfr
Copy link
Member Author

vpratfr commented Dec 16, 2014

Hi,

main issue on my side is time, not finding where the problem is. If you feel like contributing, feel free to fork the project and submit a pull request that fixes the issue.

The idea is to have the hint text aligned to the bottom of the EditText text when that one is empty.

@demogorgorn
Copy link

Ok! I've solved the problem. First of all I thought that the problem is in difference of styles of TextView and EditText (here is all ok). Then i started to check the java classes. So the problem is here:

FloatingLabelTextViewBase:

@Override
    protected void afterLayoutInflated(Context context, AttributeSet attrs, int defStyle) {
        super.afterLayoutInflated(context, attrs, defStyle);

        // Load custom attributes
        final int drawableRightId;
        final int drawableLeftId;
        final int drawablePadding;
        final int inputWidgetTextAppearance;
        final int inputWidgetTextColor;
        final float inputWidgetTextSize;

        if (attrs == null) {
            inputWidgetTextAppearance = -1;
            inputWidgetTextColor = 0xaa000000;
            inputWidgetTextSize = getResources().getDimensionPixelSize(R.dimen.flw_defaultInputWidgetTextSize);
            drawableLeftId = getDefaultDrawableLeftResId();
            drawableRightId = getDefaultDrawableRightResId();
            drawablePadding = 0;
        } else {
            final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatingLabelTextViewBase, defStyle, 0);
            drawableRightId = a.getResourceId(R.styleable.FloatingLabelTextViewBase_android_drawableRight, getDefaultDrawableRightResId());
            drawableLeftId = a.getResourceId(R.styleable.FloatingLabelTextViewBase_android_drawableLeft, getDefaultDrawableLeftResId());
            drawablePadding = a.getDimensionPixelSize(R.styleable.FloatingLabelTextViewBase_android_drawablePadding, 0);
            inputWidgetTextAppearance = a.getResourceId(R.styleable.FloatingLabelTextViewBase_flw_inputWidgetTextAppearance, -1);
            inputWidgetTextColor = a.getColor(R.styleable.FloatingLabelTextViewBase_flw_inputWidgetTextColor, 0xaa000000);
            inputWidgetTextSize = a.getDimension(R.styleable.FloatingLabelTextViewBase_flw_inputWidgetTextSize, getResources().getDimensionPixelSize(R.dimen.flw_defaultInputWidgetTextSize));
            a.recycle();
        }

        final TextView inputWidget = getInputWidget();
        inputWidget.setCompoundDrawablesWithIntrinsicBounds(drawableLeftId, 0, drawableRightId, 0);
        inputWidget.setCompoundDrawablePadding(drawablePadding);
        if (inputWidgetTextAppearance != -1) {
            inputWidget.setTextAppearance(getContext(), inputWidgetTextAppearance);
        }
        inputWidget.setTextColor(inputWidgetTextColor);
        //inputWidget.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputWidgetTextSize);
        inputWidget.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!isFloatOnFocusEnabled()) return;

                if (hasFocus) {
                    floatLabel();
                } else {
                    if (getInputWidget().getText().length() == 0) {
                        anchorLabel();
                    }
                }
            }
        });
    }

if you comment this line

 //inputWidget.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputWidgetTextSize);

everything works fine!

@demogorgorn
Copy link

Also I've noticed that if I specify TypedValue.COMPLEX_UNIT_SP - everything works fine but the text size is big.

@vpratfr
Copy link
Member Author

vpratfr commented Jul 2, 2015

Ok. But we need to be able to set the text size. So there must be something else we can do to make both work.

@demogorgorn
Copy link

Yes I know, but this is the direction to continue searching of the solution

@demogorgorn
Copy link

Ok! Here is another solution without commenting code blocks. I've done comparison of styles, text widgets and some experiments. Why does widgets behave like this - I don't know. But there is more beautiful (in my point of view) solution:

FloatingLabelTextViewBase:

final TextView inputWidget = getInputWidget();
        inputWidget.setCompoundDrawablesWithIntrinsicBounds(drawableLeftId, 0, drawableRightId, 0);
        inputWidget.setCompoundDrawablePadding(drawablePadding);
        inputWidget.setTextColor(inputWidgetTextColor);
        inputWidget.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputWidgetTextSize);
        if (inputWidgetTextAppearance != -1) {
            inputWidget.setTextAppearance(getContext(), inputWidgetTextAppearance);
        }

Just replace the order of setTextSizу and setTextAppearance methods.

In your app layouts (where you use FloatingLabelItemPicker and for example FloatingLabelEditText) you must specify two attributes for the custom Fl... widgets:

app:flw_inputWidgetTextSize="14sp"
app:flw_inputWidgetTextAppearance="@android:style/TextAppearance.Medium.Inverse"

I thinks much better to add these attributes to the library's layout files.

P.S. These attrs is needed only by EditText and TextView (as I understand while was comparing their styles).

@demogorgorn
Copy link

Please note that instead of TextAppearance.Medium.Inverse we could use any TextAppearance styles. The main question is - to use the same appearance in EditText and TextView.

@demogorgorn
Copy link

But! Sorry for many messages. There is another problem - font size 14sp. When it uses 14sp - heights of widgets are not equal! I'll continue the searching

@demogorgorn
Copy link

Starting form the 18sp - all good

@demogorgorn
Copy link

Setting different drawable (with smaller size) - and the size of widget not increases.

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

No branches or pull requests

3 participants