Android Question How to insert image to EditText?

Discussion in 'Android Questions' started by abhishek007p, Apr 11, 2015.

  1. abhishek007p

    abhishek007p Active Member Licensed User

    Anyone here can help me use this code?

    Code:
    private void addImageBetweentext(Drawable drawable, EditText editText1) {
            drawable .setBounds(
    00, 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.
     
  2. Beja

    Beja Expert Licensed User

  3. DonManfred

    DonManfred Expert Licensed 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.
     
    Peter Simpson likes this.
  4. abhishek007p

    abhishek007p Active Member Licensed User

    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.

    [​IMG]



    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.

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

          
    public void insertImageToCurrentSelection(Bitmap Bitmap) {
            
    BitmapDrawable drawable = new BitmapDrawable(BA.applicationContext.getResources(), Bitmap); 
            drawable.setBounds(
    00, 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);
           
        
    }
    }
     
  5. DonManfred

    DonManfred Expert Licensed User

    nice

    Will you share the library later?
     
  6. abhishek007p

    abhishek007p Active Member Licensed User

    well, i already posted the code.

    Once i learned on how to correctly extend an existing classes :( then will share it as a library then ;)
     
    Holger Brauer and Kwame Twum like this.
  7. Beja

    Beja Expert Licensed User

    Thanks DonManfred for the clarification.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice