Search code examples
androidandroid-webviewandroid-imageviewtextviewmultiline

How to put a imagebutton/imageview between a multiline textview in android


I am having an issue of placing a imagebutton/imageview between a multiline textview in Android.

Eg.:

enter image description here

The text is dynamic. I am having many views like this in my application(nearly 50) with different texts and I want to achieve this with re-usability of code. So kindly suggest me some dynamic solution. On clicking the imagebutton/imageview, it should redirect to calling screen.

I am open to ideas of performing this with the help of WebViews unless and until they handle the dynamicity of text throughout the application(I don't want to make Html file for each and every view).

Looking for the sincere co-operation. Thank You.


Solution

  • Try this

            SpannableString string1=new SpannableString(yourdynamictext);
    
    //Get Drawable
                 Drawable d=getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
    
    // image span
            ImageSpan span2= new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
                string1.setSpan(span2, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           yourtextview.setText(string1);
    

    Update:

    set this for your textview

    yourtexview.setMovementMethod(LinkMovementMethod.getInstance());
    

    Use ClickableSpan

    Here is the example

     ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View textView) {
                       Toast.makeText(MainActivity.this, "Hello",Toast.LENGTH_LONG).show();
                    }
                };
    
    
               string1.setSpan(clickableSpan , 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
               yourtextview.setText(string1);