4

I am working on an app for deaf-mute people, so created a specific keyboard for the app. Now, I want to set images in an EditText, the images are symbols used by the deaf-mute people I want something like this : deaf-muet keyboard + EditText

in that editText, I was able to set only the first sign which it refers to the first letter, but I want to set many images for each letter. I used the .setCompoundDrawablesWithIntrinsicBounds() but it only gave me the first letter. how can I do to set multiple images in an EditText or in ViewText?

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
Bilel
  • 85
  • 7

2 Answers2

0

Try this, i Hope it will help you

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/icon">
</EditText>

The same thing you can try for:

android:drawableRight
android:drawableTop
android:drawableBottom
android:drawablePadding
Milad Bahmanabadi
  • 946
  • 11
  • 27
  • this will let me just to add an icon to the EditText, but in my case the images should inserted when the user is writing in the editText – Bilel Oct 03 '18 at 12:28
0

this class is custom editText that you can use to put a drawable inside it.

public class WithIconEditText extends android.support.v7.widget.AppCompatEditText {
int actionX, actionY;
private Drawable drawableRight;
private Drawable drawableLeft;
private Drawable drawableTop;
private Drawable drawableBottom;
private DrawableClickListener clickListener;

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

public WithIconEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    if (left != null)
        drawableLeft = left;
    if (right != null)
        drawableRight = right;
    if (top != null)
        drawableTop = top;
    if (bottom != null)
        drawableBottom = bottom;
    super.setCompoundDrawables(left, top, right, bottom);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
    Rect bounds;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        actionX = (int) event.getX();
        actionY = (int) event.getY();
        if (drawableBottom != null && drawableBottom.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.BOTTOM);
            return super.onTouchEvent(event);
        }
        if (drawableTop != null && drawableTop.getBounds().contains(actionX, actionY)) {
            clickListener.onClick(DrawableClickListener.DrawablePosition.TOP);
            return super.onTouchEvent(event);
        }
        if (drawableLeft != null) {
            bounds = drawableLeft.getBounds();
            int x, y;
            int extraTapArea = (int) (13 * getResources().getDisplayMetrics().density + 0.5);
            x = actionX;
            y = actionY;
            if (!bounds.contains(actionX, actionY)) {
                x = actionX - extraTapArea;
                y = actionY - extraTapArea;
                if (x <= 0)
                    x = actionX;
                if (y <= 0)
                    y = actionY;
                if (x < y)
                    y = x;
            }
            if (bounds.contains(x, y) && clickListener != null) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.LEFT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
        }
        if (drawableRight != null) {
            bounds = drawableRight.getBounds();
            int x, y;
            int extraTapArea = 13;
            x = actionX + extraTapArea;
            y = actionY - extraTapArea;
            x = getWidth() - x;
            if (x <= 0) {
                x += extraTapArea;
            }
            if (y <= 0)
                y = actionY;
            if (bounds.contains(x, y) && clickListener != null) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.RIGHT);
                event.setAction(MotionEvent.ACTION_CANCEL);
                return false;
            }
            return super.onTouchEvent(event);
        }
    }
    return super.onTouchEvent(event);
}

@Override
protected void finalize() throws Throwable {
    drawableRight = null;
    drawableBottom = null;
    drawableLeft = null;
    drawableTop = null;
    super.finalize();
}

public void setDrawableClickListener(DrawableClickListener listener) {
    this.clickListener = listener;
}

public interface DrawableClickListener {
    void onClick(DrawablePosition target);

    enum DrawablePosition {TOP, BOTTOM, LEFT, RIGHT}
}
}
SinaMN75
  • 6,742
  • 5
  • 28
  • 56