Android Question How to insert image to EditText?

abhishek007p

Active Member
Licensed User
Longtime User
Anyone here can help me use this code?

B4X:
private void addImageBetweentext(Drawable drawable, EditText editText1) {
        drawable .setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        int selectionCursor = editText1.getSelectionStart();
        editText1.getText().insert(selectionCursor, ".");
        selectionCursor = editText1.getSelectionStart();

        SpannableStringBuilder builder = new SpannableStringBuilder(editText1.getText());
        builder.setSpan(new ImageSpan(drawable), selectionCursor - ".".length(), selectionCursor,                                                  
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText1.setText(builder);
        editText1.setSelection(selectionCursor);
}

Maybe using inline java codes, javaObject, or maybe Extending EditText view.

I got the code here: http://tutorialsface.blogspot.com/2014/05/how-to-add-imagessmileyemojis-in.html

I want to insert image on the edit text view, like what other textview do.
 

DonManfred

Expert
Licensed User
Longtime User
I have no experience with Java, but a little of googling I found this, and I thought it might be of some help to you.
It will be no help as it is using a simple edittext here, not a spanable textview. Due to this you can only add text to the editfield.
 
Upvote 0

abhishek007p

Active Member
Licensed User
Longtime User
It will be no help as it is using a simple edittext here, not a spanable textview. Due to this you can only add text to the editfield.

Already got it working, by hacking out the edittext view codes, adding this piece of code that i modified, and recompiling it as an extended edit text view library using eclipse.

and here is the result and proof that we can add image on the current edit text view we have.

This is my first attempt, as i only hijack the edit text view and added the insert image code., it was also themed correctly.

8mpWX4k.png




You should not post a question to just one person @abhishek007p. If you post a question and that person responds then that's okay, but singling out just one person is not good practice.

yes, i know, sorry, but he made the core library so i think he got the source codes that is why im asking on how can i extend the EditText view. hehe..

This is the second attempt i made on extending the EditTextWrapper, it can add the image but it was not themed correctly compare to the screenshot above.

B4X:
public class EditTextViewExtended extends anywheresoftware.b4a.objects.EditTextWrapper {       

      public void insertImageToCurrentSelection(Bitmap Bitmap) {
        BitmapDrawable drawable = new BitmapDrawable(BA.applicationContext.getResources(), Bitmap); 
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        int selectionCursor = ((EditText)getObject()).getSelectionStart();
        ((EditText)getObject()).getText().insert(selectionCursor, ".");
        selectionCursor = ((EditText)getObject()).getSelectionStart();
       
        SpannableStringBuilder builder = new SpannableStringBuilder(((EditText)getObject()).getText());
        builder.setSpan(new ImageSpan(drawable), selectionCursor - ".".length(), selectionCursor,                                                  
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((EditText)getObject()).setText(builder);
        ((EditText)getObject()).setSelection(selectionCursor);
       
    }
}
 
Upvote 0
Top