Search code examples
androidxmltextviewandroid-drawablexml-drawable

drawableEnd textview property not working in mobile android


Hello friends I want to my drawable image to end of my textview so i set my layout as below

xml file

<TextView
            android:id="@+id/text_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_two"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="Go ahead and get to the area you want then"
            android:drawableEnd="@drawable/alogo_small"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_15"
            android:visibility="visible" />

when i run above code it shoe me output like below

enter image description here

I want my drawable after then so any idea how can I solve this problem ? Your all suggestion are appreciable

EDIT

SpannableString ss = new SpannableString("Go ahead and get to the area you want then"); 
     Drawable d = getResources().getDrawable(R.drawable.alogo_small); 
     d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
     ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
     ss.setSpan(span, ss.length(), ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     mTextViewEmailTwo.setText(ss); 

when I run above code it give me output like below

enter image description here


Solution

  • Try to use Html.ImageGetter:

    Html.ImageGetter getter = new Html.ImageGetter(){
         public Drawable getDrawable(String source){   // source is the resource name
         Drawable d = null;
         Integer id =  new Integer(0);
         id = getApplicationContext().getResources().getIdentifier(source, "drawable",getPackageName());
    
         d = getApplicationContext().getResources().getDrawable(id);
    
         if (d != null)
             d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
             return d;
         }
    };
    
    String imgString = "Go ahead and get to the area you want then " + " <img src=\"alogo_small\"/>";
    mTextViewEmailTwo.setText(Html.fromHtml(imgString, getter, null));