RTL support for custom editext for available on the left

I have an edittext that has an image that can be pulled out on the left, with an editablext not editable, but now I want it to support rtl. Despite my efforts, I cannot support rtl.

My custom class is as follows:

public class PrefixedEditText extends TextInputEditText {

private String mPrefix = "+"; // can be hardcoded for demo purposes
private Rect mPrefixRect = new Rect(); // actual prefix size

public PrefixedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect);
    mPrefixRect.right += getPaint().measureText(" "); // add some offset

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), getPaint());
}

@Override
public int getCompoundPaddingLeft() {
    return super.getCompoundPaddingLeft() + mPrefixRect.width();
}

}

My xml call to this class is as follows:

<cl.dd.ui.PrefixedEditText
                    style="@style/edittext"
                    android:id="@+id/etCode"
                    android:maxLength="3"
                    android:drawableLeft="@drawable/icon_phone_number"
                    android:drawableStart="@drawable/icon_phone_number"
                    android:minWidth="@dimen/dim_img_width"
                    android:hint="@string/s_login_code"
                    android:tag="@string/s_login_country_code"
                    android:inputType="number"/>
+4
source share
3 answers

You need to make sure that supportsRtlset to true inAndroidManifest.xml

<application
    ...
    android:supportsRtl="true">

layoutDirection locale, inherit rtl xml, SDK 17 +

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layoutDirection="locale">

SDK 17, res, layout-ldrtl values-ldrtl, , , rtl .

+3

, .

:

    /**
     * Custom EditText that displays a fixed prefix in line with the text.
     * The trick here is to draw the prefix as a drawable and attach it via
     * setCompoundDrawables().
     */

    public class PrefixEditText extends AppCompatEditText {

        private ColorStateList mPrefixTextColor;

        public PrefixEditText(Context context) {
            this(context, null);
        }

        public PrefixEditText(Context context, AttributeSet attrs) {
            this(context, attrs, android.R.attr.editTextStyle);
        }

        public PrefixEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mPrefixTextColor = getTextColors();
        }

        public void setPrefix(String prefix) {
            if (Locale.getDefault().getLanguage().equalsIgnoreCase("en"))
                setCompoundDrawables(new TextDrawable(prefix + " "), null, null, null);
            else if (Locale.getDefault().getLanguage().equalsIgnoreCase("ar"))
                setCompoundDrawables(null, null, new TextDrawable(" " + prefix), null);
        }

        public void setPrefixTextColor(int color) {
            mPrefixTextColor = ColorStateList.valueOf(color);
        }

        private class TextDrawable extends Drawable {
            private String mText = "";

            TextDrawable(String text) {
                mText = text;
                setBounds(0, 0, (int) getPaint().measureText(mText) + 3, (int) getTextSize());
            }

            @Override
            public void draw(Canvas canvas) {
                Paint paint = getPaint();
                paint.setColor(mPrefixTextColor.getColorForState(getDrawableState(), 0));
                int lineBaseline = getLineBounds(0, null);
                canvas.drawText(mText, 0, canvas.getClipBounds().top + lineBaseline, paint);
            }

            @Override
            public void setAlpha(int alpha) {/* Not supported */}

            @Override
            public void setColorFilter(ColorFilter colorFilter) {/* Not supported */}

            @Override
            public int getOpacity() {
                return 1;
}
}
}
+1

.

TextInputEditText .

:

<RelativeLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">

     <TextView
         android:id="@+id/prefixTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentStart="true />

     <EditText
         android:id="@+id/editText"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"     
         android:layout_toEndOf="@id/prefixTextView" />

</RelativeLayout>

, RelativeLayout . .

There are several drawbacks: the resulting view will not be in the class hierarchy EditText, and using it in many places can lead to poor user interface performance because it introduces nested layouts, but at least part of the RTL will be fine.

0
source

Source: https://habr.com/ru/post/1648642/


All Articles