Mubarak
Mubarak

Reputation: 1449

Add drawable starting of multiline text view

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"/>

Upvotes: 3

Views: 2948

Answers (3)

jbertrand
jbertrand

Reputation: 148

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!"/>

Upvotes: 5

PingForward
PingForward

Reputation: 313

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.

Upvotes: 0

ismail alaoui
ismail alaoui

Reputation: 6073

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

Upvotes: 0

Related Questions