Reputation: 537
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.
Upvotes: 0
Views: 2834
Reputation: 309
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);
Upvotes: 2
Reputation: 897
Please use drawableStart() method of text view of showing icon on left of text view.but you can't use icon as a String Text.
Upvotes: 0
Reputation: 53
Sorry to say but it is not possible for a Image to be shown inside a Textview the proper way in calling an image from drawable is by the used of ImageView.
Like this one.
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="200dp"
android:layout_marginStart="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="25dp"
android:scaleType="centerInside"
android:layout_marginLeft="100dp"
android:layout_marginRight="8dp"
android:background="@drawable/ic_myimage">
</ImageView>
Upvotes: 0