I've searched around on Google and came across this site where I found a question similar to mine in which how to include a image in a TextView
text, for example "hello my name is [image]", and the answer was this:
ImageSpan is = new ImageSpan(context, resId); text.setSpan(is, index, index + strLength, 0);
I would like to know in this code,
text.setSpan()
like import or reference or leave it text?If someone can break this down for me that would be much appreciated.
Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.
In Android all layout can be nested one another.
Try this ..
txtview.setCompoundDrawablesWithIntrinsicBounds( R.drawable.image, 0, 0, 0);
Also see this.. http://developer.android.com/reference/android/widget/TextView.html
Try this in xml file
<TextView android:id="@+id/txtStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/image" android:drawablePadding="5dp" android:maxLines="1" android:text="@string/name"/>
com/xyz/customandroid/ TextViewWithImages .java:
import java.util.regex.Matcher; import java.util.regex.Pattern; import android.content.Context; import android.text.Spannable; import android.text.style.ImageSpan; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; public class TextViewWithImages extends TextView { public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public TextViewWithImages(Context context, AttributeSet attrs) { super(context, attrs); } public TextViewWithImages(Context context) { super(context); } @Override public void setText(CharSequence text, BufferType type) { Spannable s = getTextWithImages(getContext(), text); super.setText(s, BufferType.SPANNABLE); } private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance(); private static boolean addImages(Context context, Spannable spannable) { Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E"); boolean hasChanges = false; Matcher matcher = refImg.matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) { if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end() ) { spannable.removeSpan(span); } else { set = false; break; } } String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim(); int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName()); if (set) { hasChanges = true; spannable.setSpan( new ImageSpan(context, id), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } return hasChanges; } private static Spannable getTextWithImages(Context context, CharSequence text) { Spannable spannable = spannableFactory.newSpannable(text); addImages(context, spannable); return spannable; } }
Use:
in res/layout/mylayout.xml:
<com.xyz.customandroid.TextViewWithImages android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF00" android:text="@string/can_try_again" android:textSize="12dip" style=... />
Note that if you place TextViewWithImages.java in some location other than com/xyz/customandroid/, you also must change the package name, com.xyz.customandroid
above.
in res/values/strings.xml:
<string name="can_try_again">Press [img src=ok16/] to accept or [img src=retry16/] to retry</string>
where ok16.png and retry16.png are icons in the res/drawable/ folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With