Skip to content

Commit d41b92d

Browse files
committed
added android:gravity attribute support for floating label.
1 parent ea0f255 commit d41b92d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.text.method.TransformationMethod;
2121
import android.util.AttributeSet;
2222
import android.util.TypedValue;
23+
import android.view.Gravity;
2324
import android.view.MotionEvent;
2425
import android.view.View;
2526
import android.widget.AutoCompleteTextView;
@@ -865,17 +866,28 @@ protected void onDraw(@NonNull Canvas canvas) {
865866
// calculate the text color
866867
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction, getCurrentHintTextColor(), primaryColor));
867868

869+
// calculate the horizontal position
870+
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
871+
int floatingLabelStartX = getScrollX();
872+
if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
873+
floatingLabelStartX += getPaddingLeft();
874+
} else if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT) {
875+
floatingLabelStartX += getWidth() - getPaddingRight() - floatingLabelWidth;
876+
} else {
877+
floatingLabelStartX += (int) (getPaddingLeft() + (getWidth() - getPaddingLeft() - getPaddingRight() - floatingLabelWidth) / 2);
878+
}
879+
868880
// calculate the vertical position
869-
int start = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
881+
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
870882
int distance = floatingLabelSpacing;
871-
int position = (int) (start - distance * floatingLabelFraction);
883+
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);
872884

873885
// calculate the alpha
874886
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
875887
textPaint.setAlpha(alpha);
876888

877889
// draw the floating label
878-
canvas.drawText(floatingLabelText.toString(), getPaddingLeft() + getScrollX(), position, textPaint);
890+
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, position, textPaint);
879891
}
880892

881893
// draw the bottom ellipsis

library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.text.method.TransformationMethod;
2222
import android.util.AttributeSet;
2323
import android.util.TypedValue;
24+
import android.view.Gravity;
2425
import android.view.MotionEvent;
2526
import android.view.View;
2627
import android.widget.EditText;
@@ -865,17 +866,28 @@ protected void onDraw(@NonNull Canvas canvas) {
865866
// calculate the text color
866867
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction, getCurrentHintTextColor(), primaryColor));
867868

869+
// calculate the horizontal position
870+
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
871+
int floatingLabelStartX = getScrollX();
872+
if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
873+
floatingLabelStartX += getPaddingLeft();
874+
} else if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT) {
875+
floatingLabelStartX += getWidth() - getPaddingRight() - floatingLabelWidth;
876+
} else {
877+
floatingLabelStartX += (int) (getPaddingLeft() + (getWidth() - getPaddingLeft() - getPaddingRight() - floatingLabelWidth) / 2);
878+
}
879+
868880
// calculate the vertical position
869-
int start = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
881+
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
870882
int distance = floatingLabelSpacing;
871-
int position = (int) (start - distance * floatingLabelFraction);
883+
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);
872884

873885
// calculate the alpha
874886
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
875887
textPaint.setAlpha(alpha);
876888

877889
// draw the floating label
878-
canvas.drawText(floatingLabelText.toString(), getPaddingLeft() + getScrollX(), position, textPaint);
890+
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, position, textPaint);
879891
}
880892

881893
// draw the bottom ellipsis

sample/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
android:layout_width="match_parent"
235235
android:layout_height="wrap_content"
236236
android:hint="Max Characters"
237-
app:minCharacters="8"/>
237+
app:maxCharacters="10"/>
238238

239239
<TextView
240240
android:layout_width="match_parent"

0 commit comments

Comments
 (0)