package
org.geeksforgeeks.gfgspannablestring;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.text.SpannableString;
import
android.text.SpannableStringBuilder;
import
android.text.Spanned;
import
android.text.style.BackgroundColorSpan;
import
android.text.style.ForegroundColorSpan;
import
android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
TextView textView2 = findViewById(R.id.text_view2);
String text = "GeeksForGeeks A Computer
Science Portal
for
Geeks";
String text2 =
"Learn Algorithm."
;
SpannableString spannableString =
new
SpannableString(text);
SpannableStringBuilder spannableStringBuilder =
new
SpannableStringBuilder(text2);
ForegroundColorSpan green =
new
ForegroundColorSpan(Color.GREEN);
ForegroundColorSpan cyan =
new
ForegroundColorSpan(Color.CYAN);
BackgroundColorSpan yellow =
new
BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(green,
0
,
13
, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(cyan,
40
,
43
, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(yellow,
0
,
16
, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
textView2.setText(spannableStringBuilder);
}
}