Sunday, October 29, 2017

SpannableString


SpannableString:

SpannableString is a class its extends object and implements harSequence,GetChars,Spannable.This is the class for text whose content is immutable but to which markup objects can be attached and detached.
Android Spannable textview is used to change color, size, style and adding click event for particular word.The SpannableString class allows you to customize characters in text, without changing view style itself.
This class uses different types of Span are as follows.
  • ForegroundColorSpan
  • BackgroundColorSpan
  • RelativeSizeSpan
  • StrikethroughSpan
  • AbsoluteSizeSpan
  • UnderlineSpan
  • StyleSpan
  • TypefaceSpan
  • SuperscriptSpan
  • SubscriptSpan
  • URLSpan
  • ClickableSpan

ForegroundColorSpan:

ForegroundColorSpan allows you to set a foreground color on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("ForeGround text");
spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

BackgroundColorSpan:

BackgroundColorSpan allows you to set a background color on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Background text");
spString.setSpan(new BackgroundColorSpan(Color.GREEN), 5, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

RelativeSizeSpan:

RelativeSizeSpan allows you to set an relative text size on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Relative span");
spString.setSpan(new RelativeSizeSpan(0.5f), 5, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

StrikethroughSpan:

StrikethroughSpan allows you to strikethrough a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("StrikeThrough span");
spString.setSpan(new StrikethroughSpan(), 0, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

AbsoluteSizeSpan:

AbsoluteSizeSpan allows you to set an absolute text size on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
int diffTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 66, getResources().getDisplayMetrics());
SpannableString spString = new SpannableString("AbsoluteSize span");
spString.setSpan(new AbsoluteSizeSpan(diffTextSize), 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


UnderlineSpan:

UnderlineSpan allows you to underline a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Underlined text");
spString.setSpan(new UnderlineSpan(), 0, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


StyleSpan:

StyleSpan allows you to set a style (bold, italic, normal) on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text style Span");
spString.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


TypefaceSpan:

TypefaceSpan allows you to set a font family (monospace, serif etc) on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString.setSpan(new TypefaceSpan("sans-serif-light"), 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

SuperscriptSpan:

SuperScriptSpan allows you to set a text as superscript.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString.setSpan(new SuperscriptSpan(), 6, 9, 0);
// make the superscript text smaller
spString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
textView.setText(spString);


URLSpan :

URLSpan allows you to set a text as Url,

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString .setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
textView.setText(spString);

ClickableSpan :

ClickableSpan allows you to click a particular part of a text,


Example : 1

ClickableSpan clickableSpan = new ClickableSpan() {
   @Override
   public void onClick(View widget) {
    // We display a Toast. You could do anything you want here.
    Toast.makeText(SpanExample.this, "Clicked", Toast.LENGTH_SHORT).show();
   
   }
};
styledString.setSpan(clickableSpan, 103, 112, 0);


Example :2

String strName = "Android is a mobile Os";
        SpannableString ss = new SpannableString(applink);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Toast.makeText(Detail.this,"Super",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        int i1 = applink.indexOf("android");
        int i2 = applink.indexOf("is");
        ss.setSpan(clickableSpan, i1, i2+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        txtOne.setText(Html.fromHtml(String.valueOf(ss)), TextView.BufferType.SPANNABLE);
        txtOne.setMovementMethod(LinkMovementMethod.getInstance());


        txtOne.setHighlightColor(Color.TRANSPARENT);

No comments:

Post a Comment