spannable on android for textView

I want to make the font bold and ıtalic with spannable for this u will need to make o.content text as SpannableString then set it to TextView as : SpannableString spannablecontent=new SpannableString(o.content.toString()); spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0,spannablecontent.length(), 0); // set Text here tt.setText(spannablecontent); EDIT : you can also use Html.fromHtml for making text Bold and Italic in … Read more

Android SpannableString set background behind part of text

If anyone’s having difficulty with Roosevelt’s code sample (I sure was, maybe because it’s Xamarin.Android?), here’s a translation into a more basic Android java version: public class RoundedBackgroundSpan extends ReplacementSpan { private static int CORNER_RADIUS = 8; private int backgroundColor = 0; private int textColor = 0; public RoundedBackgroundSpan(Context context) { super(); backgroundColor = context.getResources().getColor(R.color.gray); … Read more

select a word on a tap in TextView/EditText

UPDATE: Another better approach is to use BreakIterator: private void init() { String definition = “Clickable words in text view “.trim(); TextView definitionView = (TextView) findViewById(R.id.text); definitionView.setMovementMethod(LinkMovementMethod.getInstance()); definitionView.setText(definition, BufferType.SPANNABLE); Spannable spans = (Spannable) definitionView.getText(); BreakIterator iterator = BreakIterator.getWordInstance(Locale.US); iterator.setText(definition); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end … Read more

SpannableString with Image example

Found the following and it seems to do the job: public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView = (TextView) findViewById(R.id.textview); SpannableString ss = new SpannableString(“abc”); Drawable d = ContextCompat.getDrawable(this, R.drawable.icon32); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(ss); }

How set Spannable object font with custom font

This is a late answer but will help others to solve the issue. Use the following code:(I’m using Bangla and Tamil font) TextView txt = (TextView) findViewById(R.id.custom_fonts); txt.setTextSize(30); Typeface font = Typeface.createFromAsset(getAssets(), “Akshar.ttf”); Typeface font2 = Typeface.createFromAsset(getAssets(), “bangla.ttf”); SpannableStringBuilder SS = new SpannableStringBuilder(“আমারநல்வரவு”); SS.setSpan (new CustomTypefaceSpan(“”, font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); SS.setSpan (new CustomTypefaceSpan(“”, font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); … Read more