Issue
I am trying to add a drawable (ic_myimage) in a string (my_string_name) that I have in the resources string.xml
<resources>
<string name="my_string_name">Here is my image: @drawable/ic_myimage It looks great</string>
</resources>
Is this possible to do? I want the image with the rest of the string to be displayed in my TextView. (Code Below)
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_string_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Note: I tried the following which I found on another post but didn't work
<string name="my_string_name">Here is my image: [img src=ic_myimage/] It looks great</string>
I am using Android Studio and Kotlin.
Solution
You can do so by providing text using Spannable string just like that
//your drawable
ImageSpan is = new ImageSpan(context, R.drawable.arrow);
//your spannable text "Lorem.... amet"
SpannableString spannableText= new SpannableString("Lorem ipsum dolor sit amet");
//apply image to spannable text
//0 is the flag
//start and end are the index length where you wantg to put the image
spannableText.setSpan(is, startIndex, End index, 0);
//then, you can apply this tspannable text to textview
yourTextView.setText(spannableText);
Answered By - Asad Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.