2

Need to add Quote as a drawable in multiline text like below mentioned screen. Like you can see quote is in a starting of multiline text and next line text is just starting from the below to the quote.

Quote(drawable) should be starting of text in Textview and enter image description here

I need to use TextView to achieve this and quote should be a drawable.

If I am using drawableLeft property of TextView Its showing like below mentioned image.

enter image description here

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!\nHello World!\nHello World!\nHello World!"
        android:drawableStart="@drawable/ic_quote"
        android:drawableLeft="@drawable/ic_quote"/>
Mubarak
  • 1,419
  • 15
  • 22
  • this link may help you: https://stackoverflow.com/questions/25603607/how-to-align-drawableleft-to-top-instead-center-in-android-textview – android May 04 '19 at 16:07
  • This is not what i required.If you see the image properly. Next line text should start exactly from starting of drawable.This all required with one textview with multi line text. – Mubarak May 05 '19 at 11:51

3 Answers3

3

You can create a SpannableString to add the drawable :

TextView myTextView = (TextView) findViewById(R.id.myTextView);

ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_quote);
SpannableString spannableString = new SpannableString("*" + myTextView.getText()); // "*" will be replaced by your drawable

int start = 0; // the start index, inclusive
int end = 1; // the end index, exclusive
int flag = 0;
spannableString.setSpan(imageSpan, start, end, flag);

myTextView.setText(spannableString);

And in your layout :

<TextView
   android:id="@+id/myTextView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hello World!\nHello World!\nHello World!\nHello World!"/>
jbertrand
  • 128
  • 9
0

Try using android:gravity="top"

if it didnt work then go with negative margin like this

android:drawablePadding="-20sp"

Another alternative way is to take an ImageView beside TextView inside LinearLayout so you can apply gravity

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • This is not what i required.If you see the image properly. Next line of text should start exactly from starting and same time below of the drawable.This all required with one textview with multi line text. – Mubarak May 05 '19 at 12:03
0

You can use SpannableStringBuilder.

 val spannableText = SpannableStringBuilder("*Your text!").apply {
            val imageSpan = ImageSpan(context, R.drawable.your_icon)
            setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
 }
 textView.setText(spannableText, TextView.BufferType.SPANNABLE)

The symbol * will be raplaced witn your icon.

PingForward
  • 313
  • 2
  • 6